Agent Cost Attribution: Tracing Spend Back to a Cause
Cost & Pricing

Agent Cost Attribution: Tracing Spend Back to a Cause

A provider bill tells you the total and nothing else. How to attribute agent spend to a team, a task and a tool call so you can act on it.

The provider dashboard shows one number per month, possibly split by API key. That is enough to know spend went up and useless for knowing why, which is the only question anyone actually asks.

Attribution is the missing layer: a mapping from tokens billed to the thing that caused them. Without it, cost control degenerates into asking everyone to be careful, which does not work and cannot be measured.

Attribute at the call, not the key

The instinct is to issue an API key per team and read spend from the provider. This gives you one dimension, fixed at provisioning time, and stops being useful the moment anything is shared.

Record cost yourself at the point of the call instead. Every request returns a usage object with input, output and cached token counts; multiply by the rates for that model and store the result alongside the metadata you already have.

That metadata is what makes attribution work: run identifier, session, turn number, agent name, tool being served, initiating human, team, and the model and version used. You are logging most of it for other reasons already, so the marginal cost is one numeric field. Agent audit logging covers the surrounding record.

The dimensions that actually get queried

Cost per run tells you whether a workflow is viable. A task that costs more than the human alternative should not be automated, and you cannot know that without a per-run figure.

Cost per turn within a run tells you where a run went wrong. Agent cost is dominated by resent context, so the turn-by-turn curve is the diagnostic that matters: a smooth rise is normal, a step change means something large entered the transcript and is now being paid for repeatedly.

Cost per tool is the one teams skip and the one that pays. Tools do not spend tokens directly, but a tool returning enormous output charges you for that output on every subsequent turn. Attributing downstream context growth to the tool that produced it identifies the specific fix. Why agent costs are unpredictable covers the mechanism.

Cost per person and per team is what finance asks for. It is the least useful dimension for engineering and the one most likely to be requested first, so build it but do not stop there.

Cached tokens must be separated

Cached input is billed at a fraction of the standard input rate, and treating all input as one number both overstates spend and hides the lever that reduces it.

Store cached and uncached input separately and compute the effective cache hit rate per run. A long agent session with a low hit rate is nearly always a transcript being mutated near its start, which invalidates the prefix on every turn.

The fix is structural rather than a matter of spending less: keep the stable content at the front and append rather than editing. That is a change you can only justify with a measured hit rate. Prompt caching explained covers the mechanics.

Subagents break naive attribution

When an agent spawns children, their spend belongs to the parent task and nothing in the provider data says so.

Propagate the run identifier through every spawn so the whole tree rolls up to one root. Without it, delegated work appears as unattributed background spend, and delegation is exactly where cost concentrates because each child carries its own copy of the context.

Keep the parent-child edge in the record too. Rolling up gives you the total; the tree tells you which branch produced it. Delegation is worth the spend only when the branch total is smaller than doing the work in one context, and that comparison needs the tree.

Attribute the waste explicitly

Some spend produces no value at all, and it is worth a category of its own rather than being averaged into the total.

Retries after a failed call. Turns after a loop was detected but before it was stopped. Runs that terminated at a limit without producing output. Work discarded because a human rejected it at the gate.

Tagging these separately usually reveals that a meaningful share of the bill bought nothing, and each category has a specific fix rather than a general instruction to spend less. Hidden cost of retries covers the largest of them.

Budgets need the same identifiers

Attribution is retrospective; a budget is the same data enforced in advance, which is why the two should share a schema.

Track cumulative cost per run against a ceiling and stop the loop when it is reached. Per-run ceilings are far more effective than monthly ones, because a monthly budget is discovered to be exceeded after the damage and a per-run ceiling prevents it.

Set the ceiling from the observed distribution rather than intuition, and alert on the tail rather than the mean. The mean run is not the problem; the run that cost forty times the median is. Agent token budgets covers choosing the numbers.

What to build first

Add cost to the record you already write for each call, with run, turn, agent, tool and model. Split cached from uncached input. Propagate run identifiers into subagents.

Then build exactly two views: cost per run sorted descending, and cost per tool. The first finds the runaway sessions, the second finds the tool that made them expensive. Everything else is refinement of those two answers. Cost of running an agent fleet covers what the aggregate looks like at scale.

Common questions

Why is per-API-key spend not enough?

It gives one dimension fixed at provisioning time and breaks as soon as a key is shared. Recording usage at the call — with run, turn, agent, tool, model and initiating human — lets you slice the same spend every way a question can arrive.

What is the most useful cost dimension for engineering?

Cost per tool. Tools do not spend tokens directly, but one that returns huge output is charged for on every later turn. Attributing downstream context growth to the tool that produced it points at a specific fix.

Are monthly budgets useful for agents?

Barely. A monthly budget is discovered to be exceeded after the spend has happened. A per-run cost ceiling that stops the loop when reached prevents the runaway session that caused it, and should be set from the observed tail rather than the mean.

Similar articles

Setting an AI Budget for a Small Engineering Team
Cost & Pricing
Cost & Pricing·8 min read

Setting an AI Budget for a Small Engineering Team

A bottom-up method for budgeting model spend on a team of five to twenty, including the buffer to hold, the caps to set, and the alerts that matter.

Read
AI Cost Anomaly Detection That Catches Real Problems
Cost & Pricing
Cost & Pricing·11 min read

AI Cost Anomaly Detection That Catches Real Problems

How to spot abnormal LLM spend in token data: per-workload baselines, rate-of-change thresholds, and telling a runaway agent apart from real growth.

Read
Alerting on Token Spikes Before the Invoice Explains Them
Cost & Pricing
Cost & Pricing·8 min read

Alerting on Token Spikes Before the Invoice Explains Them

Runaway LLM spend is usually discovered at month end. What to alert on, what thresholds actually work, and how to avoid alarms nobody reads.

Read