AI Agent Cost Tracking 2026
The bill arrives and it is over budget, but you cannot tell which user, feature or retry burned the money. Cost tracking is mostly about capturing tokens, model and tenant from day one.
📜 Table of Contents
📖 Keep reading
Cost Tracking for AI Agents: Every Call Accounted For
Agent costs differ from plain API usage: one user request may trigger five tool rounds, each resending the full history, so input tokens accumulate. Without records, the bill is a single number you cannot explain.
1. Capture usage properly
resp = client.chat.completions.create(model="gpt-4o-mini", messages=messages)
u = resp.usage
record = {"prompt_tokens": u.prompt_tokens,
"completion_tokens": u.completion_tokens,
"cached_tokens": getattr(getattr(u, "prompt_tokens_details", None), "cached_tokens", 0)}
For streaming, usage is omitted unless you pass stream_options={"include_usage": True}. Track cached tokens separately; they are priced differently and blending them overstates cost.
2. Price table in config
Keep a versioned price file with effective dates and fill numbers from official pricing pages. Never hardcode prices in application code, and reconcile against the provider invoice rather than your own table.
3. Attribution labels
| Dimension | Example | Use |
|---|---|---|
| Tenant/user | user_id, org_id | Billing, quotas |
| Feature | chat, summary, agent_plan | Find the expensive feature |
| Trace | trace_id | Tie multi-round calls together |
| Model | name + version | Compare cost per task |
Inject labels with contextvars at the request boundary so tool code does not change.
4. Schema and queries
One wide table: timestamp, trace_id, user_id, feature, model, three token counts, price snapshot, cost.
SELECT date(ts) d, feature, SUM(cost) c
FROM llm_calls GROUP BY d, feature ORDER BY d DESC, c DESC;
Track cost per request, cost per user per day, and cost per business outcome. Averages hide the long sessions that dominate spend.
5. Budgets and anomaly detection
- Hard daily and monthly caps per user; degrade to a cheaper model at threshold, refuse at limit.
- Alert when a single call's input tokens exceed three times the historical p99.
- Record retry counts; retry rate spikes signal upstream instability and multiply cost.
- Watch prefix-cache hit rate; a drop usually means the prompt structure changed.
6. Common mistakes
- Counting input but not output, which is usually several times more expensive.
- Ignoring multi-round accumulation.
- Forgetting embeddings, reranking and transcription.
- Logging to files that rotate away before reconciliation.
FAQ
No invoice detail? Keep local records with model, tokens, trace id and time; use them for attribution.
What is a reasonable cost? There is no universal number. Establish your own baseline and explain week-over-week deltas.
Open-source options? Langfuse, Helicone and OpenTelemetry GenAI conventions all work; one table plus a dashboard is the cheapest start.
Preventing abuse? Rate limits, input length caps and deduplication work better together than alone.
Related
Written by our editorial team; tools listed here are tested or verified against public sources. Links point to official sites or GitHub repos for reference only — no paid placements.