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
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: