Skip to content
AgentsTrack

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_usage is a running total. Read the last one and never sum them. If a file has token_usage_record lines, sum those instead, deduplicated by response_id, and never add them to the snapshot.
  • cached_input_tokens sits inside input_tokens, and reasoning_output_tokens sits inside output_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.

RecordWhat it carriesHow to use it
session_metacwd, cli_versionIdentifies the working directory and CLI build
turn_contextmodel, effortShows which models the session used
event_msg with payload.type = token_countinfo.total_token_usage (running total), rate_limits with plan_typeWith 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:

  1. If the file has token_usage_record lines, sum payload.usage across them, deduplicated by payload.response_id. Decide on whether records exist, not on the CLI version.
  2. If there are no records, take the last info.total_token_usage in the file.
  3. Never sum the snapshots, and never add records on top of the snapshot. Records and snapshots count the same tokens.
  4. 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"
done

Measured 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_records and last_snapshot match, either number is usable for that file.
  • When from_records is higher than last_snapshot, check the response_id values for duplicates before you trust the sum. If there are none, treat the gap as unexplained.
  • When from_records is lower than last_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_records is null, the file has no records, so use last_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/sessions and ignores CODEX_HOME. If you set that variable, point the search at the sessions folder under it.
  • -mtime -30 keeps only files modified in the last 30 days.
  • It misses archived_sessions/ and any compressed rollouts.
  • It reports total_tokens only, 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.

FieldAlready part ofWhat not to do
cached_input_tokensinput_tokensDo not add it on top of input_tokens
reasoning_output_tokensoutput_tokensDo 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_context shows 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.