How do you measure your AI coding agent's token usage?
You measure your AI coding agent's token usage by reading the input/output token counts that every model API already returns, attributing them to the four things that drive them — file reads, tool output, the MCP manifest, and transcript replay — and then converting tokens to dollars using the model's published per-million-token price. Everything else is decoration. If you can produce those numbers per session, you can find your waste in an afternoon. I spent years doing AI research before I started building token tooling, and the single most common mistake I see is people optimising a number they've never actually looked at. They feel the bill is too high, they "trim some context," and they have no idea whether it worked. So before any optimisation talk, let's get you a real meter. This is the measurement companion to How to reduce AI coding agent token usage — read that next once you can see your numbers.What exactly are you measuring?
You're measuring four distinct quantities, and conflating them is the first error: input tokens, output tokens, cached (cache-read) tokens, and the dollar cost that those map to. They are not interchangeable, because they're priced very differently. A token is the unit a model bills in — roughly 3–4 characters of English, produced by the model's tokenizer. For coding agents, three things matter:- Input tokens are everything you send the model on a given turn: the system prompt, the tool definitions, the whole conversation history, and any file or command output already in it. This is almost always the dominant cost for agents.
- Output tokens are what the model generates back. They're priced higher per token — sometimes 5x — but there are usually far fewer of them.
- Cache-read tokens are input tokens served from prompt caching. They bill at roughly 10% of the normal input rate, which is why a high cache-hit rate quietly changes your whole economics.
Where do the numbers actually come from?
The numbers come from the API response itself — you do not need to estimate. Every major provider returns a usage object on each completion. Anthropic's Messages API returnsusage with input_tokens, output_tokens, cache_creation_input_tokens and cache_read_input_tokens (API reference). OpenAI's API returns a usage object with prompt_tokens, completion_tokens and total_tokens (API reference). These are the ground truth — the same counts you're billed on.
There are three places to read them, in increasing order of effort and accuracy:
- The vendor dashboard. Anthropic Console and the OpenAI usage page both show token and dollar totals by day and by API key. Zero setup, but coarse: you get a daily aggregate, not "which session, which file, which tool."
- The agent's own counter. Claude Code shows a running token/cost figure per session; Cursor exposes usage in its settings. Good for a gut check, weak for attribution — they rarely tell you what spent the tokens.
- The
usagefield, logged per call. This is the real meter. If you can intercept each model call and append itsusageto a log with a label (which session, which step), you can answer every question that matters. This is exactly what a savings dashboard does for you.
How do you turn tokens into dollars?
You turn tokens into dollars by multiplying each token class by its per-million-token (MTok) price and summing. The formula is boring and exact:cost = (input_tokens × input_price_per_MTok / 1_000_000)
+ (cache_read_tokens × input_price_per_MTok × 0.10 / 1_000_000)
+ (output_tokens × output_price_per_MTok / 1_000_000)
+ (cache_read_tokens × input_price_per_MTok × 0.10 / 1_000_000)
+ (output_tokens × output_price_per_MTok / 1_000_000)
| Model | Input | Output |
|---|---|---|
| Claude Opus 4.8 | $5 | $25 |
| Claude Sonnet 4.6 | $3 | $15 |
| Claude Haiku 4.5 | $1 | $5 |
| GPT-5.5 | $5 | $30 |
- Non-cached input: 0.8M × $3 = $2.40
- Cached input: 1.6M × $3 × 0.10 = $0.48
- Output: 0.04M × $15 = $0.60
- Total: $3.48 for one task.
How do you attribute usage to the four cost drivers?
You attribute usage by labelling each logged call with what it did, then grouping. Tokens in aggregate tell you that you have a problem; tokens by category tell you which one. The four buckets, and how to spot each in your logs: File reads. Look at the input-token jump on turns where the agent called a read/file tool. A 500-line source file is roughly 5,000–7,000 tokens; if you see a dozen of those early in a session, that's your retrieval bill — and it gets re-billed on every later turn. The fix is to read less by searching first; see semantic code search. Tool output. Watch input growth right after shell commands. A failednpm test can dump 15,000 tokens of stack traces when the agent needed about 50 — the failing test name and the assertion. That delta is pure waste and the textbook case for output filtering.
The MCP manifest. This one is sneaky because it has no obvious trigger turn — it's a constant tax. Every connected MCP server re-advertises its full tool schema on every single turn, used or not. Measure it by counting input tokens on a turn where the agent did nothing but think: that floor is largely your manifest. Lazy-loading tool definitions removes most of it.
Transcript replay. Plot input tokens per turn across a session. If the line climbs steadily while the work doesn't, you're paying context window tax — re-billing old reads forever. This is also why context engineering and disciplined context compression matter more than any single trimmed prompt.
Once each turn carries a label, a five-line group-by gives you the percentage split. In practice it's lopsided: for most agents I've profiled, file reads plus unfiltered tool output dwarf everything else.
How to set up a real meter today
Pick the lightest option that answers your actual question, then escalate only if you need attribution:- Daily gut check (5 minutes). Open your vendor dashboard, note today's spend and token total. Do this for a week to learn your baseline. If the number doesn't bother you, stop here — premature optimisation is real.
- Per-session totals (30 minutes). Wrap your agent's API client and log the
usageobject after every call with a session ID. Sum by session. You'll immediately see which kinds of tasks are expensive. - Per-category attribution (an afternoon). Add the tool name / turn type to each log line and group by it. This is where you find the surprise — almost always either eager reads or unfiltered output.
- Convert to dollars. Apply the formula above so you're reasoning in money, not abstract tokens. Money makes the trade-offs obvious to you and to anyone you report to.
What goes wrong when people measure tokens
The most common failure is measuring totals without attribution and then "optimising" blind. Here are the traps, and how to avoid each:- Counting output, ignoring input. Output is the visible, satisfying number — the code the agent wrote. It's usually a rounding error next to input. If your dashboard leads with output tokens, it's measuring the wrong thing.
- Ignoring the cache entirely. Two identical-looking sessions can cost 9x apart on cache-hit rate alone. Any meter that doesn't break out
cache_read_input_tokenswill mislead you about where the money goes. Understand prompt caching before you trust a total. - Estimating with the wrong tokenizer. Character-count heuristics ("÷4") are fine for a ballpark but drift on code, JSON and non-English text. When the number has to be right, read the API's
usage, don't estimate from a different tokenizer. - Measuring one call instead of the session. The expensive thing about agents is replay across turns, not any single call. A per-call view hides the compounding. Always sum the session.
- Confusing tokens with rate limits. Hitting a throttle is a throughput problem, not a cost problem. They feel similar in the moment and need completely different fixes.
See also:
Ranked #1 on the Token-Harness Optimizer Leaderboard.
Tokenade ranks #1 in the Token-Harness Optimizer Leaderboard — an end-to-end benchmark of agent token optimizers measured on real coding sessions. Set it up once, it works on every prompt. Works with Claude Code, Cursor, Codex, Copilot & more.