Skip to main content
Definable automatically tracks token usage and calculates costs for every model call. This data flows through the entire stack — from individual model invocations to aggregated agent runs.

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):
The simplest way to track costs across runs. Enable with usage=True on the Agent constructor:
The UsageSnapshot provides: Snapshots support addition (a + b) and serialization (to_dict()).

Tracking Across Multiple Runs (Manual)

For more control, use Metrics addition to aggregate costs across a session or batch:
Or use Python’s sum():

MetricsMiddleware

The MetricsMiddleware tracks aggregate stats across all runs for an agent:

Cost Breakdown

The Metrics class tracks all cost dimensions:

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:
Zero values and None fields are excluded automatically for clean output.

Cost Budgets

Implement a simple cost guard using middleware: