Claude Code Compaction Error: Input Exceeds the Context Window

Why Claude Code /compact can fail with a 400 context-window error, how to fork from an earlier session point, and how to recover safely.

Short answer

The usual fix is not re-authentication or repeating the same request. Fork from an earlier, smaller point in the session, then run /compact in the new branch. A fork from the final message may still inherit nearly the entire oversized history.

What the error means

errortext
Error during compaction: API Error: 400 Input exceeds the model's context window.

The server rejected the request because its input exceeded the model's context window. This refers to the accumulated conversation, tool calls, file contents, patches, logs, or images, not to one line of code or an API key.

Why /compact can fail

Compaction still needs to send the history to the model before the model can create a shorter summary. If the input is already too large, the compaction request itself is rejected before summarisation starts.

Fast recovery

recoveryshell
1. Stop adding logs, full files, or repeated copies of the same error
2. Check the working tree:
   git status --short
   git diff --stat
3. Open the session history:
   claude --resume
4. Choose an earlier point before the largest tool outputs appeared
5. Fork or branch from that point
6. Run /compact in the new session
7. Re-read the current files and continue the task

A session branch is not automatically a Git branch. Check the actual files and worktree with Git before assuming that code was reverted or discarded.

Choose an earlier fork point

Good fork points include the moment after the task and file scope were confirmed, before the first large repository search, or immediately after a completed phase whose result was already verified. Avoid the final message if it contains long logs or repeated file contents.

If the fork still fails

diagnosistext
/context

# If the branch is still too large:
/resume -> choose session -> Fork / Branch
/compact

# If the task boundary has changed:
/clear
  1. Fork further back if the new branch still contains too much history.
  2. Use /clear when most of the old context is no longer needed.
  3. Inspect /context for oversized tool output, MCP results, logs, images, or repeated files.
  4. Check model routing and proxy context limits if a short new session fails too.
  5. Check Claude Code, proxy, and model configuration compatibility.

/clear starts an empty context. Write a short handoff before using it:

handofftext
Task: continue one concrete feature

Completed:
- files changed
- checks already passed

Current state:
- active work
- unresolved issue

Next:
- one to three highest-priority actions

Constraints:
- files not to touch
- required validation commands

Prevent another overflow

  • Separate exploration, implementation, testing, and release into phases or sessions.
  • Read file ranges and log summaries instead of repeatedly printing complete outputs.
  • Compact while the session is still comfortably below the limit.
  • Save decisions, completed checks, and next actions in a worklog.
  • Limit tool and MCP output, expanding only the relevant fragment.

FAQ

Is this usually an invalid API key?

No. Invalid credentials usually produce authentication errors. A 400 context error points to input size, though a proxy or model-routing mismatch should be checked if a fresh short session also fails.

Does restarting Claude Code fix it?

Restarting alone may not reduce saved session history. Resume the session and fork from an earlier point, or start a new session with a short handoff.

Does a fork delete the original session?

A normal fork preserves the original conversation. Verify code state with git status and the actual files.

See the Claude Code integration guide for API and routing issues, or Codex tool recovery for a different class of tool failure.