codex - 💡(How to fix) Fix Include per-API-call token usage (`last`) in turn.completed JSONL events [2 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
openai/codex#17539Fetched 2026-04-13 05:38:45
View on GitHub
Comments
2
Participants
2
Timeline
8
Reactions
0
Author
Timeline (top)
labeled ×4commented ×2unlabeled ×2

Root Cause

The interactive CLI doesn't have this problem because it uses the per-request data from ThreadTokenUsage.last internally. But that field is discarded when converting to the exec JSONL output ... only .total is emitted.

Code Example

{"type":"turn.completed","usage":{...},"last_usage":{"input_tokens":248301,...}}
RAW_BUFFERClick to expand / collapse

What variant of Codex are you using?

CLI

What feature would you like to see?

When using codex exec, the turn.completed JSONL event reports cumulative session token totals. This makes it impossible to determine current context window utilization ... the cumulative numbers grow across API calls and quickly exceed the context window size (e.g. 6.9M cumulative input_tokens against a 272K window).

The interactive CLI doesn't have this problem because it uses the per-request data from ThreadTokenUsage.last internally. But that field is discarded when converting to the exec JSONL output ... only .total is emitted.

Adding the most recent request's token usage to the turn.completed event would bring exec output to parity with the interactive experience for context window tracking. Something like:

{"type":"turn.completed","usage":{...},"last_usage":{"input_tokens":248301,...}}

The data is already available in ThreadTokenUsage.last ... it just needs to be carried through to the JSONL output in event_processor_with_jsonl_output.rs (usage_from_last_total).

Additional Information

This affects any programmatic use of codex exec that wants to display or act on context window utilization.

Additional information

No response

extent analysis

TL;DR

Modify the event_processor_with_jsonl_output.rs to include ThreadTokenUsage.last in the turn.completed JSONL event to accurately track context window utilization.

Guidance

  • Review the usage_from_last_total function in event_processor_with_jsonl_output.rs to understand how the total field is calculated and how ThreadTokenUsage.last can be incorporated.
  • Update the turn.completed event to include a new field, such as last_usage, that contains the token usage of the most recent request.
  • Verify that the updated event output includes the expected last_usage field with accurate token usage data.
  • Test the modified codex exec output to ensure it accurately tracks context window utilization.

Example

{"type":"turn.completed","usage":{...},"last_usage":{"input_tokens":248301,...}}

This example shows the proposed update to the turn.completed event, including the new last_usage field.

Notes

This solution assumes that the ThreadTokenUsage.last data is already available and accurate. If this data is not reliable, additional work may be needed to ensure its accuracy.

Recommendation

Apply workaround: Modify the event_processor_with_jsonl_output.rs to include ThreadTokenUsage.last in the turn.completed JSONL event, as this will bring the codex exec output to parity with the interactive experience for context window tracking.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING