Skip to main content

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

The CostTracker captures all token types returned by the API: All token types are tracked per-message, per-run, and per-session. The getSummary() method aggregates totals across all tracked dimensions.

Raw Provider Metrics

Every RunOutput.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:
Example providerMetrics by provider:

Cost Breakdown

Each cost entry includes a 6-category breakdown:
Access per-run or aggregated:

Built-in Pricing

Pricing is included for 50+ models: Override or extend pricing:

Budget Enforcement

Budgets are checked before each LLM call and mid-run during tool-calling loops:
See Cost Auto-Stop for mid-run enforcement details.

Works Across All Agent Types

The same CostTracker instance can be shared across different agent types:

Cost Summary

Events


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

If a model has no pricing entry (built-in or custom), the cost is recorded as $0 but token counts are still tracked.

Budget Enforcement in Practice

With onBudgetExceeded: "warn", the run continues but emits a cost.budget.exceeded event instead of throwing.

Token Accuracy

Agentium verifies 100% accuracy between CostTracker 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