Estimating Coding Agent Cost From Transcripts: How Close It Gets
A dollar budget for a group of coding agents needs a dollar figure while the agents are still running. The provider's invoice arrives at the end of the month. Neither Claude Code nor Codex hands its hooks a cost. What they do keep is a record of every model reply and the tokens it used.
Forkit reads that record, prices the tokens and adds them up per session and per fan-out (a group of agents started together). A fan-out started with a budget is warned at 80% and stopped when it reaches it. This post is an engineering account of how the estimate is made, the mistakes that make estimates like it wrong, how close it came where we could check, and where it is still off.
Where the tokens come from
Claude Code writes a transcript for each session: one JSON line per event, including each assistant reply with its model and a usage block (input, output, cache write and cache read tokens). Forkit's hook reads the new lines since its last read, at the end of each turn or session and during a turn at most every 20 seconds, and sends the totals per model to POST /api/v1/usage. Forkit treats the transcript format as internal to Claude Code and liable to change, so the hook reads it defensively and the figure is always labelled an estimate.
Codex keeps a session file under $CODEX_HOME/sessions/YYYY/MM/DD/, named after the session. It records running totals in token_count events and names the model in turn_context events.
Any other agent can report with forkit usage --model M --input N --output N, or give its own figure with --cost USD. A figure an agent gives is used as given.
Five mistakes that make the estimate wrong
1. Counting one reply several times
A Claude Code reply is often written on several transcript lines that share one message id, with counts that grow as the reply streams. Summing every line counts the same reply two or three times. The hook keeps each id once, at its largest count.
There is a second form of the same mistake. A reply can be split across two reads: half its lines arrive before one report, the rest before the next. The hook remembers the last reply of each read, and when the same id appears at the start of the next read, it counts only the growth.
2. Missing the subagents
When Claude Code starts subagents, their replies go into separate transcripts in a subagents folder beside the session's own. A reader that looks only at the main file misses all of them. The hook reads the session's transcript and every subagent transcript beside it.
3. Treating running totals as increments
Codex records totals for the whole session so far. Adding each token_count event to the last gives a figure that grows roughly with the square of the session's length. The hook takes the latest total and reports only its growth since the previous report.
A total can also go down: when a session is compacted or restarted, Codex starts counting again. The hook treats a total that went down as a new count and takes it whole. Codex's input count also includes cached input, so cached tokens are subtracted from input before pricing, to avoid charging them at the full rate.
4. Forgetting the model
Codex names the model at the start of a turn, before any count is written. If the hook reads the file just after a turn starts, that read has a model name and no counts. The first version kept the model only for reads that also had counts, so the next read had counts and no model, and was priced as an unknown model.
We found this on staging on 21 November 2026, in the first run with real Codex 0.160.1 sessions. The fix keeps the model from a read with no counts for the read that brings them, and a test now covers it.
5. Trusting a price list blindly
Tokens are only half the estimate. Forkit prices them from three sources, in order:
- Hand-checked Claude prices, checked against Anthropic's pricing page on 20 November 2026.
- A catalog of about 1,500 chat models from every large provider, built from LiteLLM's public
model_prices_and_context_window.json. A snapshot ships with Forkit, and each day at 04:17 UTC Forkit fetches the catalog again, so a new model is priced without a deploy. - A deliberately dear default for a model nobody knows: $10 per million input tokens and $50 per million output tokens, with the figure flagged as an estimate. A budget then errs towards stopping early.
The catalog is public data that anyone can change, so a fetched price is not trusted on its own. It replaces the shipped price only when it lies within a fifth to five times that price. A catalog that suddenly says a model is nearly free must not switch a budget off. A fetch that fails, or returns fewer than 500 models, changes nothing.
Names need care too. The same model is spelled openai/gpt-5, us.anthropic.claude-…-v1:0 or models/gemini-… depending on the provider or gateway, so Forkit reduces each to one name before pricing. It also never prices a longer name as a shorter one it merely starts with: gpt-50 is not gpt-5.
How close it gets
Codex: exact on tokens, in the check we ran. With real Codex 0.160.1 sessions on staging, the tokens Forkit recorded equalled Codex's own total, 31,731, priced at that model's catalog rate. That is one check, after the fix in mistake 4.
Claude Code: right in structure, not compared with an invoice. The tests cover the duplicate lines, split reads and subagent files. We have not compared a Claude Code estimate with an Anthropic invoice, so we cannot give a percentage error.
The recorded runs show what the figures look like in use, all single runs on staging: twelve Claude Code agents testing hypotheses on the Palmer Penguins dataset showed about $1.24 of a $10 budget (20 November 2026), twelve agents comparing models of the Mauna Loa CO2 record about $1.25 (21 November), and a two-agent shadow run about $0.21 (21 November).
Where the estimate is known to be off
Some differences are built in, and we know their direction.
- List prices. The figure ignores negotiated discounts and volume pricing. A team with a discount pays less than Forkit shows.
- Subscriptions. A person on a flat subscription does not pay per token at all. Forkit's figure is then what the same work would cost at list price, which is still useful for comparing runs, but it is not that person's bill. Braintrust gives the same caveat for its coding-agent cost view (7 October 2026): "Costs come from traced model calls and may differ from your provider's subscription bill."
- Cache writes. Anthropic charges 1.25 times the base input price for a 5-minute cache write and 2 times for a 1-hour write. A transcript's counts do not say which was used, so Forkit prices every cache write as a 5-minute write. Where 1-hour writes are used, the figure is low.
- Tiered prices. Claude Haiku 5.5 costs five times as much for prompts over 100,000 tokens. Forkit prices Haiku 5.5 at its lower tier, so long-prompt work on it is underestimated. Prices that vary by prompt length in the catalog are taken at their lowest tier too.
- Price modifiers. Anthropic's US-only inference (1.1 times) and fast mode are not applied.
- Timing. A report arrives during a turn at most every 20 seconds, so a fan-out can pass its budget by what its agents spend in about 20 seconds before it is stopped.
- Agents that report nothing are invisible to a dollar budget. For them, a fan-out's limit on tool calls (
max_tool_calls) is the guard.
Most of these make the figure low. For a budget that is meant to stop work, that is the wrong direction, so set a budget with a margin, and treat the 80% warning as the point to look.
How others do it
Braintrust's coding-agent insights (7 October 2026) show cost by developer, model, tool, branch or key for Claude Code, Codex, OpenCode and others, from traced model calls. LangSmith (18 November 2026) attributes each trace's spend to the pull requests the agent contributed to, for Claude Code, Codex, Cursor, Copilot in VS Code and Deep Agents. Neither post explains how cost is computed from the traces. Both are views of spend. Forkit's figure exists mainly to act on: to warn and stop a fan-out while it runs.
What to take from this
If you build your own estimate, the order of work that mattered for us:
- Count each reply once, and count growth across reads.
- Read every transcript a session writes, subagents included.
- Know whether a number is an increment or a running total.
- Keep state that arrives before the counts it belongs to.
- Do not let a fetched price list lower a known price without limit, and price unknown models high.
- Write down every known gap, and its direction.
Related: Codex hooks guide, Claude Code hooks vs MCP, how to stop a runaway agent and agent fan-out explained.
Where Forkit fits
Forkit is the coordination system for agent teams: plans, handoffs, file claims and decisions, shared by every agent and the people behind them. It does not run agents. It estimates what they cost so a group of them can be warned and stopped at a budget. It is free to try at forkit-mcp.com.
Sources
- Anthropic, Pricing (model prices and prompt caching multipliers), read 25 November 2026: https://platform.claude.com/docs/en/about-claude/pricing
- LiteLLM, model_prices_and_context_window.json: https://github.com/BerriAI/litellm/blob/main/model_prices_and_context_window.json
- Braintrust, "Optimizing coding agent usage and costs", 7 October 2026: https://www.braintrust.dev/blog/coding-agent-insights
- LangChain, "LangSmith for Coding Agents: Now track spend by outcome", Jacob Lee, 18 November 2026: https://www.langchain.com/blog/langsmith-coding-agents-roi
- Forkit source:
src/db/usage.ts,src/billing/model_prices.ts,src/billing/catalog.ts, the hook'susageSinceandcodexUsageSince, and engineering notes, "What agents cost" and "Cost for any model" (20 and 21 November 2026).