Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using instructor from_litellm with completion_cost returns cost truncated to 1 decimal place #1330

Closed
JohnPeng47 opened this issue Jan 31, 2025 · 2 comments
Labels
bug Something isn't working

Comments

@JohnPeng47
Copy link

**Bug
Excecuting the following code ...

import instructor
from litellm import completion
from litellm import completion, completion_cost, cost_per_token
from pydantic import BaseModel


class User(BaseModel):
    name: str
    age: int


client = instructor.from_litellm(completion)
instructor_resp, _ = client.chat.completions.create_with_completion(
    model="claude-3-5-sonnet-20240620",
    max_tokens=1024,
    messages=[
        {
            "role": "user",
            "content": "Extract Jason is 25 years old.",
        }
    ],
    response_model=User,
)
instructor_cost = completion_cost(completion_response=instructor_resp, model="claude-3-opus-20240229")
print("Instructor cost: ", instructor_cost)

litellm_resp = completion(
    model="claude-3-5-sonnet-20240620",
    messages=[
        {"role": "user", "content": "Extract Jason is 25 years old."}
    ],
    max_tokens=1024,
)
litellm_cost = completion_cost(completion_response=litellm_resp, model="claude-3-opus-20240229")
print("Litellm cost: ", litellm_cost)

Results in:

Instructor cost:  0.0
Litellm cost:  0.00036300000000000004

Would like to like more precision in instructor cost response when supporting litellm API

@github-actions github-actions bot added the bug Something isn't working label Jan 31, 2025
@JohnPeng47
Copy link
Author

Okay I fix:

Image

Referencing -> BerriAI/litellm#5285

Basically LiteLLM introduced two of their own custom fields to handle prompt token calculations

@ivanleomk
Copy link
Collaborator

I suggest using the following snippet

import instructor
from litellm import completion
from pydantic import BaseModel


class User(BaseModel):
    name: str
    age: int


client = instructor.from_litellm(completion)
instructor_resp, raw_completion = client.chat.completions.create_with_completion(
    model="claude-3-5-sonnet-20240620",
    max_tokens=1024,
    messages=[
        {
            "role": "user",
            "content": "Extract Jason is 25 years old.",
        }
    ],
    response_model=User,
)

print(raw_completion._hidden_params["response_cost"])
#> 0.00189

Additionally, the LiteLLM call and the instructor call will not cost the same since we need to make a function call in the first one and then we're just doing a normal chat completion call in the second.

Closing this issue for now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants