Skip to main content
The Usage class tracks token consumption for AI requests.

Definition

Fields

int
Number of tokens in the input (prompt + messages).
int
Number of tokens in the AI’s response.
int
Input tokens read from a provider prompt or context cache. Zero when absent or unreported.
int
Input tokens written to a provider cache. Zero when absent or unreported.
int
Input tokens explicitly reported as cache misses. This is not inferred for providers that do not report it.
int
Total tokens used (prompt_tokens + completion_tokens).

Usage

From generate_text

From stream_text

Multi-Step Usage

When using tools, result.usage contains the final model step’s usage. Each model call’s usage is available on its corresponding step:

Cost Estimation

Use token counts to estimate costs: