Skip to main content

Why cost attribution matters

When agents make LLM calls, every token has a price. In multi-agent systems where one agent can spawn or delegate to others, costs accumulate across multiple providers and chains. Without proper attribution you cannot answer basic questions: which agent is responsible for a $200 spike? Which tool is burning the most budget? Did the delegation chain for last night’s job exceed its allocation? Cost attribution gives you per-agent, per-tool, and per-delegation-chain cost records. It integrates with budget policies so you can fire alerts before spend becomes a problem, not after.

Setup

Configuration options

string
default:"'USD'"
ISO 4217 currency code for cost records.
{ warn: number; critical: number }
default:"undefined (no threshold alerts)"
Dollar amounts for 24-hour rolling spend that trigger alerts.
default:"undefined"
Called when a threshold is crossed or a budget policy is exceeded.
number
default:"90"
How many days of cost events to keep. Older rows are deleted on cleanup().

Recording costs

Call recordCost() after each LLM response or API call. Pass the raw token counts and the exact dollar amount from the provider’s response.

RecordCostInput

string
The agent that incurred this cost.
string
Provider and model identifier, e.g. ‘openai:gpt-4o’, ‘anthropic:claude-3-5-sonnet’, ‘mcp:github’.
number
default:"undefined"
Prompt tokens consumed.
number
default:"undefined"
Completion tokens generated.
number
Exact cost in the configured currency.
default:"undefined"
Any additional data to store alongside this event (request IDs, model version, etc.).
string
default:"undefined"
When set, this event is also attributed to the given delegation chain.
Costs are stored internally as integer microdollars (value × 1,000,000) to avoid floating-point drift across aggregations.

Provider helpers

Generating cost reports

Per-agent report

Pass a custom period to narrow the query:

CostReport

string
Agent (or owner/chain) this report covers.
{ start: Date; end: Date }
The time window the report covers.
number
Total cost across all events in the period.
Cost breakdown per tool, sorted by cost descending.
Daily spend as YYYY-MM-DD strings, sorted ascending.

Owner report

Aggregate cost across all agents owned by a user:

Top agents by cost

Find the most expensive agents in any period:

Delegation chain report

When agents delegate to sub-agents, you can attribute all costs back to the originating chain by passing delegationChainId in recordCost():

Setting up alerts

Alerts fire automatically when recordCost() is called. There are three alert types:

CostAlert

'warn' | 'critical' | 'budget_exceeded'
Severity of the alert.
string
The agent that triggered the alert.
number
The actual spend that triggered the alert.
number
The limit that was crossed.
string
Time window for this alert, e.g. ‘24h’ or ‘monthly’.

Integration with budget policies

checkBudget() reads budget policies created via kavach.policies and compares them against actual spend from the cost events table. This gives you a real-time answer before authorizing an operation.
To set a budget limit, create a policy with maxTokensCostPerMonth:
The budget_exceeded alert fires automatically on recordCost() when spend crosses this limit, so you do not need to poll.

Maintenance

Cost events accumulate. Run cleanup() periodically to remove events older than the retention window:
If you configured retentionDays on the module, you can call cleanup() with no arguments and it uses that value.

Return types

All methods return a Result<T> union:
Check result.success before accessing result.data. Error codes:
Last modified on April 18, 2026