Usage Tokens

The easiest way to get usage for non streaming requests is to access the raw response.

import instructor

from openai import OpenAI
from pydantic import BaseModel

client = instructor.from_openai(OpenAI())


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


user, completion = client.chat.completions.create_with_completion(
    model="gpt-3.5-turbo",
    response_model=UserExtract,
    messages=[
        {"role": "user", "content": "Extract jason is 25 years old"},
    ],
)

print(completion.usage)
#> CompletionUsage(completion_tokens=9, prompt_tokens=82, total_tokens=91)