Topic 3 of 5 · 4 min read
Attributing spend to agents and tasks
Goal: Read a per-agent usage report and find the agent or loop responsible for a cost spike.
You can only fix cost you can attribute: every model call should be logged with the run, the agent, the task and the customer that caused it. A provider invoice shows one total. It cannot tell you that one worker role caused 70% of last Tuesday's spike.
Check yourself · 1 of 3
Where should the token counts in your usage log come from?
Why it matters
Multi-agent systems fail expensively and quietly. A loop that retries a broken tool looks like normal traffic in the monthly total. Per-agent numbers make it visible within a day.
What to record on every call
- Run id and agent id, plus a parent id so you can rebuild the tree of who spawned whom.
- Role (planner, searcher, reviewer) so you can compare across runs.
- Task or customer id, for cost per task and per account.
- Model name.
- Token counts from the API response: input, output, and cached input read and written, kept separate because they are priced differently.
- Turn number, tool called, latency and outcome (ok, error, limit hit).
Copy token counts from the provider's usage field. Do not estimate them from text length.
A record
{
"run_id": "r_8f21", "agent_id": "w3", "parent_id": "lead",
"role": "searcher", "customer": "acme", "model": "small-tier",
"turn": 14, "tool": "web_fetch", "outcome": "error",
"input_tokens": 41200, "cache_read_tokens": 38000,
"output_tokens": 310, "cost_usd": 0.0087
}
Reading a report
Group by role, then by agent, then by turn. Look for three patterns:
- A role that dominates. One role is 60% of spend. Is that expected for its job?
- A long tail of turns. One agent at turn 80 when the median is 9 is a loop. Check its tool outcomes: repeated errors on the same tool is the usual cause.
- Cost without value. A reviewer that reads everything and approves 99% of the time.
Before and after
Weak: "Spend doubled this week; the model must have gotten pricier." With only the invoice, you guess.
Better: the report shows the searcher role's turns per run rose from 9 to 31 after a site started returning errors. Fix the retry rule, and the spend returns.
What goes wrong
- Logging only the run total. You see the spike but not its cause.
- Merging cached and uncached tokens. Cost looks wrong and caching problems stay hidden.
- No parent id. You cannot tell which agent spawned the cheap flood of children.
- Keeping prompt text in the logs. It bloats storage and may include customer data. Log ids and counts, and store content separately with access controls.
Examples
A loop found in the report
| role | agents | median turns | max turns | cost |
|---|---|---|---|---|
| planner | 40 | 4 | 6 | $3.10 |
| searcher | 160 | 9 | 84 | $41.60 |
| reviewer | 40 | 3 | 5 | $2.20 |
The searcher role dominates and one agent ran 84 turns against a median of 9. Filter that agent's calls: 71 are web_fetch errors on the same URL. The fix is a retry limit on that tool.
Cost per customer
Grouping by customer shows account acme at $210 for the week and the median account at $9. The per-agent view shows acme triggers runs with 12 workers instead of 4 because their prompts are very broad. That points to a product decision, not a bug.
Key points
- Log every model call with run, agent, parent, role, task or customer, model and turn number.
- Take token counts from the provider's usage field and keep input, output and cached tokens separate.
- Group usage by role, then agent, then turn to find dominant roles and long-tailed loops.
- A parent id lets you rebuild the spawn tree and find which agent caused a flood of children.
- Log ids and counts, not prompt text, to keep logs small and free of customer data.
Common mistakes
- Logging only the run total, which shows that cost rose but not why.
- Estimating tokens from text length instead of reading the API's usage numbers.
- Mixing cached and uncached tokens in one counter.
- Storing full prompts in the usage log instead of ids and counts.
Check yourself · 2 of 3
A usage report shows one searcher agent at turn 84 while the median is turn 9, with 71 repeated errors from one tool. What is the most likely cause?
Check yourself · 3 of 3
Which extra field lets you find which agent spawned a flood of child agents?
Practice items, not counted towards your mastery.
Start here
- How we built our multi-agent research system (Anthropic) (opens in a new tab)ArticleFree
Reports that agents use about 4x and multi-agent systems about 15x the tokens of chat, and that value must justify it.
- Prompt caching (Claude docs) (opens in a new tab)DocsFree
How cache writes and reads are priced, and how a shared prefix is reused across calls.
- Batch API (OpenAI) (opens in a new tab)DocsFree
Asynchronous batch jobs for work that can wait, at a lower price than live calls.
More for this topic
- Manage costs effectively (Claude Code docs) (opens in a new tab)DocsFree
Covers how to see and track token usage in a working agent tool.
- Claude API pricing (opens in a new tab)DocsFree
Lets you convert logged input, output and cache tokens into money.