Cost Tracking
In plain terms
Every time an AI agent answers, it costs a small amount of money (you pay the model provider per word processed). Cost tracking is the meter and the spending limit — it counts every penny and can automatically stop an agent before it overspends. Why this matters: without it, a runaway agent or a heavy user can quietly run up a large bill. With it, you set a cap (“no more than $0.50 per user per month”) and the framework enforces it — like a prepaid card that simply stops working when the limit is hit.The analogy: it’s the utility meter and the circuit breaker for your AI. You always know what you’re spending, and it cuts off before things get expensive.
Quick Start
Token Types Tracked
TheCostTracker captures all token types returned by the API:
| Token Type | Field | Description |
|---|---|---|
| Input | promptTokens | Tokens in the prompt / input |
| Output | completionTokens | Tokens generated in the response |
| Reasoning | reasoningTokens | Tokens used for chain-of-thought (o1, o3) |
| Cached | cachedTokens | Prompt tokens served from API cache |
| Audio Input | audioInputTokens | Tokens from audio input (Realtime API) |
| Audio Output | audioOutputTokens | Tokens for audio output (Realtime API) |
getSummary() method aggregates totals across all tracked dimensions.
Raw Provider Metrics
EveryRunOutput.usage object includes a providerMetrics field containing the raw, unmodified usage data returned by the provider API. This gives full transparency without any normalization loss:
providerMetrics by provider:
Cost Breakdown
Each cost entry includes a 6-category breakdown:Built-in Pricing
Pricing is included for 50+ models:| Model | Prompt / 1K | Completion / 1K |
|---|---|---|
| gpt-4o | $0.0025 | $0.01 |
| gpt-4o-mini | $0.00015 | $0.0006 |
| claude-3.5-sonnet | $0.003 | $0.015 |
| gemini-2.0-flash | $0.0001 | $0.0004 |
Budget Enforcement
Budgets are checked before each LLM call and mid-run during tool-calling loops:Works Across All Agent Types
The sameCostTracker instance can be shared across different agent types:
Cost Summary
Events
| Event | Payload |
|---|---|
cost.tracked | { runId, agentName, modelId, usage, cost } |
cost.budget.exceeded | { runId, agentName, budget, current, limit } |
Subscribing to Cost Events
Listen for cost events to build dashboards, alerts, or analytics:Per-Agent and Per-Model Breakdown
Custom Pricing for Non-Built-in Models
Budget Enforcement in Practice
onBudgetExceeded: "warn", the run continues but emits a cost.budget.exceeded event instead of throwing.
Token Accuracy
Agentium verifies 100% accuracy betweenCostTracker recorded tokens and raw API response tokens across all scenarios — simple completion, tool calling, multi-turn memory, and prompt caching. See benchmarks for detailed validation results.
Cross-References
- Agent Overview —
costTrackerin AgentConfig - Voice Agents —
costTrackerin VoiceAgentConfig - Browser Agents —
costTrackerin BrowserAgentConfig - Cost Auto-Stop — Mid-run budget enforcement
- Observability — Track costs alongside traces and metrics