codex icon indicating copy to clipboard operation
codex copied to clipboard

Stream error: “Your input exceeds the context window” when Codex streams large tool output (e.g., 300+ tests)

Open irfndi opened this issue 1 year ago • 4 comments

What feature would you like to see?

Summary

  • When Codex CLI streams large command output back to the model (e.g., running >300 tests), the session repeatedly fails with “Your input exceeds the context window” and disconnects after retries.

Steps To Reproduce

  1. Open Codex in a repo with a large test suite (e.g., >300 tests).
  2. Ask Codex to run tests, for example:
    • make test or
    • bun x vitest run
  3. Observe Codex CLI logs during the run.

Expected Behavior

  • Codex should handle large tool outputs gracefully:
    • Chunk or truncate output.
    • Summarize long output before sending to the model.
    • Continue the session without disconnecting.

Actual Behavior

  • Codex retries several times and then fails with a context window error.

Observed Logs

⚠️ stream error: stream disconnected before completion: Your input exceeds the context window of this model. Please adjust your input and try again.; retrying 1/5 in 202ms…

⚠️ stream error: stream disconnected before completion: Your input exceeds the context window of this model. Please adjust your input and try again.; retrying 2/5 in 418ms…

⚠️ stream error: stream disconnected before completion: Your input exceeds the context window of this model. Please adjust your input and try again.; retrying 3/5 in 810ms…

⚠️ stream error: stream disconnected before completion: Your input exceeds the context window of this model. Please adjust your input and try again.; retrying 4/5 in 1.621s…

⚠️ stream error: stream disconnected before completion: Your input exceeds the context window of this model. Please adjust your input and try again.; retrying 5/5 in 3.28s…

■ stream disconnected before completion: Your input exceeds the context window of this model. Please adjust your input and try again.

Minimal Repro (alternative)

  • Any large output triggers it, not only tests:
    1. Start Codex in any folder.
    2. Ask it to run a command that prints a lot, e.g.:
      • yes "x" | head -c 3000000
    3. Codex attempts to stream the entire output to the model and hits the context limit.

Environment

  • Codex CLI version: 0.42.0
  • OS: macOS 15.6.1
  • Node version: 24.2.0
  • Bun version (if applicable): 1.2.2
  • Model: gpt-5-high
  • Terminal/shell: zsh in ghostty

Impact

  • Prevents using Codex for CI-like tasks (tests, lint, builds) where output can exceed the model’s token budget.
  • Causes session interruptions and lost context.

Workarounds Tried

  • Use less verbose reporters: bun x vitest run --reporter=dot or --silent.
  • Pipe output to a file and ask Codex to summarize the file:
    • bun x vitest run --reporter=json > test-output.json and then “summarize test- output.json”.
  • Tail only the last N lines: bun x vitest run | tail -n 400.

Hypothesis

  • Tool result payloads are forwarded to the model without enforcing a token/byte budget or auto-summarization. Large outputs push the prompt beyond the model’s context window, causing the stream to disconnect.

Suggested Fixes

  • Add a hard cap on tool-result size sent to the model with:
    • Automatic chunking + iterative summarization.
    • Configurable truncation strategy (head/tail/middle) and max bytes/tokens.
  • Provide a setting to store full tool outputs as artifacts/files and send only summaries + links/paths into the chat context.
  • Consider auto-dropping or summarizing older conversation turns when approaching context limits, with a visible notice.

Additional Context

  • Reproduces consistently on large test runs (300+ tests) in our Next.js monorepo.
  • Happy to provide a minimal reproduction repo or additional debug logs if helpful.

If you’d like, I can open this issue for you using the GitHub CLI. Otherwise, paste the above into a new issue at https://github.com/openai/codex/issues/new and add the missing environment details.

Are you interested in implementing this feature?

No response

Additional information

No response

irfndi avatar Sep 28 '25 14:09 irfndi

+1

EthanDuan05 avatar Sep 28 '25 19:09 EthanDuan05

This started happening for me after I added the following to my AGENTS.md:

Before writing new code, thoroughly inspect the current package and related code, helpers, and utilities to confirm the functionality does not already exist. You can ignore example code to keep your search space If you find similar logic, prefer extending or refactoring what is there rather than duplicating it; note the gaps you see before proposing changes. When existing code requires significant modification, pause to consider the higher-level abstraction that will keep the solution maintainable and idiomatic Go—rethink package boundaries, interfaces, or composition patterns before diving into implementation.

The thoroughly inspect the current package and related code, helpers, and utilities threw it over the edge, seemed to fail to fit its own reasoning in context.

ivanvanderbyl avatar Sep 29 '25 07:09 ivanvanderbyl

If you’d like, I can open this issue for you using the GitHub CLI. Otherwise, paste the above into a new issue at https://github.com/openai/codex/issues/new and add the missing environment details.

Did you have codex summarize this issue? 😁

dylan-hurd-oai avatar Oct 05 '25 05:10 dylan-hurd-oai

This should be fixed by #4706 and is currently in the 0.45.0 alphas

dylan-hurd-oai avatar Oct 05 '25 05:10 dylan-hurd-oai