input_tokens and output_tokens your provider returns, multiplied by a per-model rate. When litellm is installed it covers 1000+ models; otherwise a built-in fallback table prices the most common models.
Quick Start
1
Run an agent and read its token counts
A bare
Agent(llm="gpt-4o-mini") records usage automatically. Read the counts back with the global collector:2
Estimate the cost from those counts
Pass the counts and the model name to
calculate_llm_cost():How It Works
Cost isinput_tokens × input_rate + output_tokens × output_rate. The only question is which rate applies to a given model name.
How Pricing Is Resolved
The fallback matcher resolves a model name to a rate in three steps:- Strip the provider prefix.
openai/gpt-4o-minibecomesgpt-4o-mini, so provider-qualified names resolve the same as bare names. - Match longest-key-first, on exact match or prefix boundary. The table is searched from the longest key down, and a key only matches when the model name is exactly that key or starts with
<key>-…. Sogpt-4o-miniis billed at$0.15/1M, notgpt-4o’s$2.50/1M, ando1-minino longer matcheso1. - Fall back to
default. If no key matches, thedefaultrate applies.defaultis never matched by name — it is only the last resort.
Fallback Pricing Table
These are the built-in rates the SDK ships in_FALLBACK_PRICING (USD per 1M tokens). They apply when litellm is not installed.
Install litellm for accurate pricing across 1000+ models. Pass
use_litellm=True to calculate_llm_cost() to prefer it; without it, the fallback table above is used for speed.Common Patterns
Price a whole session
Price a whole session
Read the session totals, then price them once:
Provider-prefixed model names resolve correctly
Provider-prefixed model names resolve correctly
openai/gpt-4o-mini is stripped to gpt-4o-mini before matching, so it prices at the mini rate:Best Practices
Use realistic model names
Use realistic model names
Pass the exact model string you run with (e.g.
gpt-4o-mini, claude-3-5-sonnet). The longest-key-first matcher relies on the full name to avoid billing a -mini model at its larger sibling’s rate.Install litellm for uncommon models
Install litellm for uncommon models
The fallback table covers common models only. For anything outside it, install litellm and call
calculate_llm_cost(..., use_litellm=True) for accurate, up-to-date pricing.Cost is an estimate, not a bill
Cost is an estimate, not a bill
These numbers estimate spend from token counts and published rates. Use your provider’s dashboard for authoritative billing.
Related
Token Tracking
Read real token spend from any agent.
Telemetry
Anonymous usage metrics for agent runs.

