Direct answer
The model window covers more than visible user text. System instructions, history, MCP tools, repository files, images, and planned output all consume the same budget.
What this guide helps you solve
Resolve context_length_exceeded and token-window errors while preserving essential task information.
Confirm the symptom and blast radius
A request is rejected for exceeding the model context window, often after a long session or large repository scan.
A new session with the same model works, while restoring history, tools, generated directories, or large files recreates the error.
- Capture the status or exception, response body, request ID, UTC time, client version, and final host before changing configuration.
- Compare one minimal request with the failing workflow to determine whether the issue is global, model-specific, or feature-specific.
- Keep credentials, full prompts, customer data, and private file contents out of screenshots and support bundles.
Common causes and ownership boundaries
Treat the error as evidence from one layer, not as proof that every downstream component failed. These are the highest-value causes to test first.
- Conversation history and system or project instructions have accumulated.
- Large files, build output, vendored code, or generated artifacts are included.
- MCP tool definitions and JSON schemas consume substantial hidden context.
- The input leaves insufficient room for the requested output and reasoning budget.
Run a layered minimal diagnosis
Start with a request that has the fewest moving parts. Preserve the same account, API host, and target model while removing optional features.
Change one variable at a time and retain the raw status, headers, response body, and timing. This separates client serialization from gateway and upstream behavior.
- 01Establish the boundaryConfirm the current model window from the actual provider and client state.
- 02Create a baselineAccount separately for instructions, history, tools, files, and output reserve.
- 03Compare one variableCreate a clean session with one minimal task and the same model.
- 04Record decisive evidenceRestore one context source at a time to identify the dominant contributor.
find . -type f -size +1M -not -path './.git/*' -print | head -50
git status --short
# Claude Code 会话中使用 /context。
# Codex 任务只指定当前问题需要的目录和文件。Apply the fix at the confirmed root cause
Make the smallest change that addresses the proven layer. Avoid hiding deterministic configuration failures behind broad retries or disabled validation.
- 01Correct the failing layerExclude generated, vendored, binary, and unrelated files from the task context.
- 02Restore required behaviorSummarize completed work into a reviewed handoff and start a focused session.
- 03Remove temporary workaroundsLoad only required tools and reserve explicit space for the expected output.
目标:<当前唯一目标>
已确认事实:<3-5 条>
涉及文件:<精确路径>
未解决风险:<仍需验证的假设>
下一步命令:<可执行验证>
不要保留:<旧日志、无关工具输出、已否定方案>Verify the repair, not just one successful call
Repeat the original path after the minimal check succeeds, then verify stable behavior under normal streaming, concurrency, and timeout conditions.
- Estimated input plus output reserve remains below the actual model limit.
- Enough output space remains for the requested result.
- Only task-relevant MCP tools and schemas are loaded.
- Long work is divided into stages with explicit artifacts instead of unlimited history.
Security boundaries and escalation evidence
Collect enough evidence to reproduce and escalate the failure without exposing credentials or weakening transport, permission, and validation controls.
- Context reduction must not remove security constraints or approval requirements.
- Review summaries before using them as authoritative handoffs.
- Do not upload private files merely to estimate token size.
- Escalate with model ID, context sources, approximate sizes, and a sanitized minimal task.
Official sources and verification scope
This guide is grounded in protocol specifications and official client documentation. Error text, retry headers, and configuration fields may change by service or client version; verify the sources and redact logs and request samples before sharing.
Read the verification methodology