How Codex token usage is calculated from rollout files
You want to know how many tokens a Codex session used, so you open its rollout file and add up the running totals. That sum is wrong, because each snapshot counts earlier tokens again. This article explains how Codex usage is calculated from the token counts in local rollout files: which record to read, how to total one session, and where the totals break. Plan limits and credits are a separate question. It is for engineers who script over session files or want to check a dashboard's numbers.
Last updated
Key points
total_token_usageis a running total. Read the last one and never sum them. If a file hastoken_usage_recordlines, sum those instead, deduplicated byresponse_id, and never add them to the snapshot.cached_input_tokenssits insideinput_tokens, andreasoning_output_tokenssits insideoutput_tokens. Adding either one on top double-counts.- A rollout can contain no usage at all. The recipe returns null for it, which means unknown, not zero.
Where Codex rollout files are stored
Codex writes session rollout files to ~/.codex/sessions/YYYY/MM/DD/, named rollout-<ts>-<uuid>.jsonl. Each line is one JSON object. If you set CODEX_HOME, the sessions folder sits under that path instead of ~/.codex. The Codex source also defines an archived_sessions/ folder, and codex exec --ephemeral writes no rollout at all.
OpenAI documents CODEX_HOME, but we found no official page that documents the rollout format. Treat the schema as internal and recheck it after CLI upgrades. Plan limits, credits and rates are separate; see OpenAI's Codex pricing page.
Which rollout records carry Codex token usage
Four kinds of line matter. Older builds may not write every kind, and info can be absent from a token_count event, so test for a field before you read it.
| Record | What it carries | How to use it |
|---|---|---|
session_meta | cwd, cli_version | Identifies the working directory and CLI build |
turn_context | model, effort | Shows which models the session used |
event_msg with payload.type = token_count | info.total_token_usage (running total), rate_limits with plan_type | With no records, use the last total_token_usage; never sum |
Top-level token_usage_record (newer builds) | payload.response_id, payload.usage (one per model response) | Use as per-response usage when present, deduplicated by response_id |
How Codex usage is calculated: records first, then the last snapshot
What the file contains decides the method. Use per-response records when they exist, and fall back to the last running snapshot when they don't. Apply these rules in order:
- If the file has
token_usage_recordlines, sumpayload.usageacross them, deduplicated bypayload.response_id. Decide on whether records exist, not on the CLI version. - If there are no records, take the last
info.total_token_usagein the file. - Never sum the snapshots, and never add records on top of the snapshot. Records and snapshots count the same tokens.
- If there are no records and no snapshot, the usage is unknown.
Where a file has both, compare the two totals before you rely on either. The recipe below prints them side by side.
Check your own rollouts with jq
For each rollout file, this recipe prints one object with three numbers: the sum of the per-response records, the last running snapshot, and the sum of all snapshots. The third is there only to show you the size of the error.
find ~/.codex/sessions -name 'rollout-*.jsonl' -mtime -30 | while read -r f; do
jq -sR '[split("\n")[] | fromjson?] |
{records: map(select(.type == "token_usage_record").payload.usage.total_tokens),
snapshots: map(select(.type == "event_msg" and .payload.type == "token_count").payload.info.total_token_usage.total_tokens // empty)}
| {from_records: (.records | add), last_snapshot: (.snapshots | last), summed_snapshots: (.snapshots | add)}' "$f"
doneMeasured on one rollout on our machine: per-response records summed exactly to the last snapshot; summing the snapshots overstated the total 2.9x.
Read the output like this:
- When
from_recordsandlast_snapshotmatch, either number is usable for that file. - When
from_recordsis higher thanlast_snapshot, check theresponse_idvalues for duplicates before you trust the sum. If there are none, treat the gap as unexplained. - When
from_recordsis lower thanlast_snapshot, check whether the thread is a fork or part of a multi-agent run, where the running total starts at the parent's total. - When
from_recordsis null, the file has no records, so uselast_snapshot. - When both are null, the file has no usage. Report it as unknown.
- Never use
summed_snapshots.
What the recipe does not cover
It is a quick check on individual files. Close these gaps before you use it to total many sessions.
- It does not deduplicate by
response_id, within a file or across files. A script that totals many files must add that. - It searches only
~/.codex/sessionsand ignoresCODEX_HOME. If you set that variable, point the search at the sessions folder under it. -mtime -30keeps only files modified in the last 30 days.- It misses
archived_sessions/and any compressed rollouts. - It reports
total_tokensonly, so you don't get the input, cached and output split. - It prints one result per file without the file name. Keep the path alongside the output if you need to trace a number back.
Are cached input tokens included in input tokens?
Yes. cached_input_tokens is a subset of input_tokens, and reasoning_output_tokens is a subset of output_tokens. The Codex source computes non-cached input as input minus cached input. OpenAI's prompt caching guide describes cached tokens as part of the input, and its reasoning guide says reasoning tokens are billed as output tokens.
| Field | Already part of | What not to do |
|---|---|---|
cached_input_tokens | input_tokens | Do not add it on top of input_tokens |
reasoning_output_tokens | output_tokens | Do not add it on top of output_tokens |
The Codex source also defines a cache_write_input_tokens field; check how your build fills it before using it in a total.
Claude Code reports cache reads separately from input_tokens, so a parser that treats both tools the same will miscount one of them. The guide to how cache read tokens work explains the Claude Code side.
Cases where per-session totals mislead
You can apply the rules above correctly and still report a session wrong. Four cases to handle:
- Model switch mid-session. A running total cannot be split by model.
turn_contextshows which models appear, so report the session as mixed instead of assigning all its tokens to one model. Do not assume per-response records separate the models either. - Forked or multi-agent threads. The running total starts at the parent's total. Adding the parent's last snapshot to the child's counts the parent's tokens twice. Check the child's first snapshot before you add anything.
- Empty usage. The recipe returns null for a rollout with no usage. Keep it out of your averages: counted as zero, it drags them down.
- Turning tokens into money. This article gives no rates, because prices change. Any dollar figure needs a cost basis: REPORTED, ESTIMATED or SUBSCRIPTION. A SUBSCRIPTION figure is an API-equivalent estimate and was never invoiced. See the cost basis definitions.
What AgentsTrack automates
The AgentsTrack collector is open source under Apache-2.0. It reads the session files Codex and other coding agents write and uploads usage to the AgentsTrack dashboard. Its default analytics mode uploads a title, usually the prompt's first line, and secret-redacted commands; metadata mode drops the title and reduces each command to its binary name. Install it with npm i -g @agentstrack/collector, agentstrack login and agentstrack start (Node 24 or newer). Read more about Codex token usage, or start a 14-day Pro trial; the collector key comes from onboarding.
Read on
Check it against your own sessions
14 days of Pro, no card. The collector is open source, and a dry run prints exactly what it would upload.