Topic 1 of 5 · 5 min read
Measuring tokens and cost per agent task
Goal: Read an agent session's usage breakdown and identify the steps that consumed most of the tokens.
You cannot cut agent cost until you can say where the tokens went, and the answer is almost never "the prompt I typed". A coding agent such as Claude Code, Codex CLI, Cursor or Copilot agent mode sends the whole conversation back to the model on every turn. Most of that conversation is not your words. It is file contents, command output and the model's own earlier answers.
Check yourself · 1 of 3
Where do most tokens in a long coding-agent session usually come from?
Why it matters
A short request like "fix the failing test" can cost 20 cents or 20 dollars. The difference is how many files the agent opened, how long the test output was, and how many times it retried. Teams that only look at the monthly invoice learn about waste a month late. Teams that look at one session at a time can fix it the same afternoon.
How to measure
- Use the session readout. Most agents show tokens and cost for the current session (in Claude Code, the
/costand/contextcommands). Check it at the end of a task, not the end of the month. - Split input from output. Output tokens cost several times more per token than input, but input is usually far larger in volume. Both matter. See the vendor's pricing page for current rates.
- Split cached from uncached input. Cache reads are billed at a fraction of normal input. A session with mostly cache reads is cheap even when it is long.
- Find the heavy steps. List each step with its token count. Look for file reads of large files, commands that print thousands of lines (test runs,
git log, build output), and retry loops. - Use team-level data. Agents can export usage through OpenTelemetry, so a dashboard shows tokens, cost, model and user. That is how you spot the one repo or workflow that burns half the budget.
Before and after
Weak: "Last week cost 400 dollars, so we should use the agent less."
Better: "Of 40 sessions, 6 cost more than 5 dollars each. In all six, the agent read the full 9,000-line schema.sql three times and pasted a complete test log into context. We now point the agent at the table it needs and cap test output to the failures."
The first sentence leads to a policy nobody follows. The second leads to two small changes.
What goes wrong
- Averages hide the tail. Median cost per task looks fine while a few runaway sessions cause most of the bill. Look at the top ten sessions, not only the mean.
- Counting only your prompts. Tool output and file reads are invisible in the chat window but dominate the count.
- Treating cost as the only number. A cheap session that produced a wrong change costs more once you add the review and the redo. Track tokens next to outcome: did the change merge?
- Measuring once. Usage drifts when the repo grows or a new tool is added. Re-check on a schedule.
Examples
A usage breakdown read step by step
A session of 14 steps used 310k input tokens. The table showed three steps at the top:
- Step 3: read
schema.sql(9,000 lines), about 60k tokens, repeated in steps 7 and 11. - Step 9: ran the whole test suite, 4,000 lines of passing output, about 35k tokens.
- Steps 12-14: three retries of the same failing edit.
The fixes are specific: ask for the one table, run only the failing test file, and stop after two failed retries to re-plan. The prompt itself was 40 tokens and was never the problem.
Key points
- An agent resends the whole conversation each turn, so tokens come mostly from file reads, tool output and earlier answers, not from your prompt.
- Check the session readout at the end of each task, and look at the most expensive sessions rather than only the average.
- Separate input, output and cached input, because they are billed at different rates.
- Export usage with OpenTelemetry or the vendor dashboard to see tokens and cost per user, model and repo.
- Pair cost with outcome: a cheap session that is thrown away is not cheap.
Common mistakes
- Judging cost from the size of the request you typed instead of the tool output and file reads in the session.
- Looking only at the monthly invoice, so a runaway workflow is found weeks after it started.
- Comparing sessions without noting the model used, which changes the price per token.
Check yourself · 2 of 3
A session cost 12 dollars. The breakdown shows 70 percent of input tokens came from one test run that printed 5,000 lines. Best next step?
Check yourself · 3 of 3
Why look at the most expensive sessions and not only the average cost per task?
Practice items, not counted towards your mastery.
Start here
- Manage costs effectively (Claude Code docs) (opens in a new tab)DocsFree
Vendor guidance on checking usage, trimming context, choosing models and setting team spend limits.
- Prompt caching (Claude docs) (opens in a new tab)DocsFree
How cache prefixes work, what invalidates them, and which usage fields report cache reads and writes.
- Monitoring usage (Claude Code docs) (opens in a new tab)DocsFree
The metrics and events an agent exports so a team can see tokens and cost per user and per model.