Per-Call Metrics
Every model call returns usage metrics:Agent Run Metrics
Agent runs aggregate metrics across all model calls in the run (including tool execution loops):UsageTracker (Recommended)
The simplest way to track costs across runs. Enable withusage=True on the Agent constructor:
UsageSnapshot provides:
| Property | Type | Description |
|---|---|---|
input_tokens | int | Total input tokens |
output_tokens | int | Total output tokens |
total_tokens | int | Combined tokens |
estimated_cost | float | Estimated cost in USD |
runs | int | Number of runs in this snapshot |
a + b) and serialization (to_dict()).
Tracking Across Multiple Runs (Manual)
For more control, useMetrics addition to aggregate costs across a session or batch:
sum():
MetricsMiddleware
TheMetricsMiddleware tracks aggregate stats across all runs for an agent:
Cost Breakdown
TheMetrics class tracks all cost dimensions:
| Field | Description |
|---|---|
input_tokens | Tokens in the prompt |
output_tokens | Tokens generated |
cache_read_tokens | Tokens served from provider cache |
cache_write_tokens | Tokens written to provider cache |
reasoning_tokens | Tokens used for chain-of-thought |
audio_input_tokens | Audio input tokens |
audio_output_tokens | Audio output tokens |
cost | Total estimated cost in USD |
Pricing Registry
Definable includes a built-in pricing registry (model_pricing.json) with rates for all supported models. Cost is calculated automatically based on the model and token counts.
Prices are defined per million tokens:
Serializing Metrics
Export metrics for logging, dashboards, or billing systems:None fields are excluded automatically for clean output.