Skip to main content
Every model invocation automatically tracks token usage, timing, and cost. This data is available on every response and aggregated across agent runs.

Token Usage

Access usage metrics on any ModelResponse:

The Metrics Class

The Metrics dataclass tracks all usage dimensions:

Cost Calculation

Definable includes a built-in pricing registry with per-token rates for all supported models. Cost is calculated automatically when available:
The pricing registry loads from model_pricing.json and covers input, output, cached, reasoning, and audio token rates for each model.

Aggregating Metrics

Metrics objects can be added together, which is useful for tracking total usage across multiple calls:
The Metrics class also works with Python’s built-in sum():

Agent-Level Metrics

When using agents, metrics are aggregated across all model calls in a run:

Serialization

Convert metrics to a dictionary for logging or storage. Zero values and None fields are excluded automatically:

Timing Metrics

Track execution time with the built-in timer: