codex - 💡(How to fix) Fix Expose Codex SDK context window usage, matching CLI "Context XX% used" [1 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#21295Fetched 2026-05-07 03:42:31
View on GitHub
Comments
1
Participants
2
Timeline
5
Reactions
0
Author
Participants
Timeline (top)
labeled ×3closed ×1commented ×1

Error Message

This is useful for long-lived agent sessions, chat bots, and UI integrations that need to warn users before context gets too full or suggest compaction.

Code Example

Context 37.2% used (74,480 / 200,000 tokens)

---

{
  type: "turn.completed",
  usage: {
    input_tokens: number,
    cached_input_tokens: number,
    output_tokens: number,
    context_tokens?: number,
    context_window?: number,
    context_percentage?: number
  }
}

---

{
  type: "context.updated",
  context: {
    total_tokens: number,
    max_tokens: number,
    percentage: number,
    model?: string
  }
}

---

await thread.getContextUsage()
RAW_BUFFERClick to expand / collapse

What would you like?

Please expose current context window usage through the Codex SDK / JSON event stream, similar to the Context XX% used indicator shown in the Codex CLI.

Today, @openai/[email protected] exposes per-turn token usage on turn.completed:

  • input_tokens
  • cached_input_tokens
  • output_tokens

But it does not appear to expose the current thread context usage percentage, total context tokens, or effective/max context window size.

Why is this useful?

Applications embedding Codex SDK can show users the same operational signal available in the CLI, for example:

Context 37.2% used (74,480 / 200,000 tokens)

This is useful for long-lived agent sessions, chat bots, and UI integrations that need to warn users before context gets too full or suggest compaction.

Suggested API shape

Any of these would work:

  1. Include context usage in turn.completed:
{
  type: "turn.completed",
  usage: {
    input_tokens: number,
    cached_input_tokens: number,
    output_tokens: number,
    context_tokens?: number,
    context_window?: number,
    context_percentage?: number
  }
}
  1. Add a dedicated event:
{
  type: "context.updated",
  context: {
    total_tokens: number,
    max_tokens: number,
    percentage: number,
    model?: string
  }
}
  1. Add a thread/client method such as:
await thread.getContextUsage()

Related comparison

Claude Agent SDK exposes a dedicated get_context_usage() API returning percentage, totalTokens, maxTokens, category breakdowns, etc. A similar SDK-level capability in Codex would let integrations keep parity with the CLI without estimating from per-turn token usage.

Related existing issues

This is related to, but distinct from:

  • #16258, which asks to expose cumulative session cost in TokenCountEvent / event stream
  • #19464, which asks for 1M token context support

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