Harness Notes

Compaction

Updated 2026-11-22,

On this page

What each harness does when the conversation outgrows the context window: when it compacts, what it keeps, and how you can steer it.

When it happens

QuestionClaude Codechecked at 2.1.299
Automatic compaction, and when

Yes. Without a configured window, Claude Code compacts as the conversation reaches the model's context limit; models running a native 1M window (Fable, Sonnet 5+, Haiku 5.5, Opus 4.7+) compact at about 967K tokens, and Sonnet 4.6 / Opus 4.6 without [1m] (or any model with CLAUDE_CODE_DISABLE_1M_CONTEXT=1) compact at the 200K boundary. Context is measured from the latest API response's input tokens (input plus cache reads and writes), and a reserve for output is held back, so raising CLAUDE_CODE_MAX_OUTPUT_TOKENS makes it compact sooner.

Since v2.1.290 it also compacts a terminal conversation of 200K+ tokens left idle about 54 minutes on the one-hour prompt cache, just before the cache expires (idleCompaction).

On a model ID it doesn't recognise (such as a gateway alias) it compacts at the window it assumes for that ID; correct it with CLAUDE_CODE_MAX_CONTEXT_TOKENS.

Subagents compact independently as their own context fills.

2.1.314 publisher adds idle compaction in VS Code and Claude Desktop Code tabs for idle conversations over 200K tokens; the prepared summary is used on the next message if prompt cache expired. Telemetry-off subscribers and third-party providers are included once one-hour prompt caching is on. This is a host-specific idle path, not a replacement automatic token threshold. 2.1.321 fixes tool-definition overcounting and background-subagent reports lost around compaction. Documentation only.

2.1.326 publisher notes repair summary generation on Bedrock/Vertex/Foundry and with telemetry off. 2.1.327 repairs teammate compaction using the lead model/window and preservation of a tool call/result after an output-limited reply. 2.1.328 rebuilds the system prompt for automatic compaction as manual /compact does. No runtime probe.

Change the trigger or turn it off

Set the auto-compact window (100K to 1M tokens, capped at the model's window) with /autocompact 500k (saved per model under modelSettings), "autoCompactWindow": 500000 in settings for every model, claude --autocompact <size> for one launch, or CLAUDE_CODE_AUTO_COMPACT_WINDOW=500000, which overrides all the others; /autocompact auto returns to the default. CLAUDE_AUTOCOMPACT_PCT_OVERRIDE (1-100) can only lower the trigger percentage. Turn automatic compaction off with the Auto-compact toggle in /config (autoCompactEnabled: false) or DISABLE_AUTO_COMPACT=1, and all compaction including /compact with DISABLE_COMPACT=1.

A subagent can compact earlier with autoCompactWindow in its frontmatter (v2.1.296+).

Turning auto-compact off also turns off idle compaction; set idleCompaction: false to disable only the idle pass.

2.1.302 adds an Idle compaction row in /config to turn the idle pass off or back on, alongside the existing idleCompaction setting. Published changes also fix stopping a turn during compaction causing Prompt is too long.

Before 2.1.304, suffixes could be parsed as a small integer and clamped to 100K. The 2.1.304 changelog says k and M suffixes now work, with warnings for out-of-range or ignored values.

2.1.311 publisher notes: settings-file and mod $.env.set DISABLE_COMPACT values can disable compaction but cannot re-enable it; teammates and rewind summary actions now honor DISABLE_COMPACT. No runtime probe.

2.1.312 publisher notes: a subagent's autoCompactWindow now lowers a window set by CLAUDE_CODE_AUTO_COMPACT_WINDOW; before, the environment variable overrode it. It still never raises that window. No runtime probe.

Compact by hand

Run /compact, optionally with focus instructions such as /compact focus on the auth bug fix. To compact only part of the conversation, open /rewind, pick a message and choose Summarize from here or Summarize up to here, optionally typing guidance for the summary.

See how full the context is

Run /context for a live breakdown of what fills the window (system prompt, tools, memory files, messages), including a warning when you are past the limit. For a constant readout, configure a status line that shows context_window.used_percentage; /usage also warns as you approach the auto-compact window.

The status line percentage is measured against the model's full window, so it no longer shows when compaction will run once you set a smaller auto-compact window.

What it does

QuestionClaude Codechecked at 2.1.299
What replaces the conversation

Claude Code sends a separate request to the session's model with the same system prompt, tools and history plus a summarisation instruction, and replaces the message history with the structured summary it returns (you see "Conversation compacted"). The request inherits the session's extended-thinking setting and uses your fallback model chain, but never falls back to a model with a smaller context window. Startup context such as CLAUDE.md and memory is then reloaded from disk around the summary.

In 2.1.301, switching to a smaller model window can first summarize the conversation with the previous model. The release also fixes repeated compaction when a gateway reports a different model name. These are published changes, not a new measured threshold.

What survives

The system prompt and output style still apply; project-root CLAUDE.md, unscoped rules, auto memory and the plan-mode plan are re-injected from disk, and a fresh git status is read. Claude Code re-reads up to five recently modified files it read or edited (files over 5,000 tokens come back as a path only), re-injects invoked skill bodies capped at 5,000 tokens each and 25,000 total, keeps the task list, and reminds Claude of running background commands and subagents. Path-scoped rules and nested CLAUDE.md files are summarised away until a matching file is touched again, and the skill listing is not re-sent.

Recent turns are folded into the summary rather than kept verbatim, except in overflow recovery, which can keep the newest exchanges or your newest prompt word for word.

Before 2.1.304, /reload-skills was the documented way to restore the catalog after compaction; the 2.1.304 release changes this behavior as described below.

2.1.304 resends the skill list after compaction; /reload-skills sends only new or changed skills. This listing is separate from restoring invoked skill bodies.

2.1.309 loads only the beginning of oversized nested CLAUDE.md files to avoid repeated compaction. Exact prefix size is unverified.

2.1.311 publisher notes fix stale MEMORY.md and CLAUDE.md immediately after automatic compaction when edited earlier in the session. This corrects freshness, not the independently documented reinjection budgets.

Trimming without summarising

Yes. As the context fills, Claude Code clears older tool outputs first and only summarises if that isn't enough, and when a request would exceed the image or PDF limits it drops a batch of the oldest images and PDFs. There is no setting to tune or disable this clearing.

The full history afterwards

Yes. The session JSONL in ~/.claude/projects/<project>/<session-id>.jsonl keeps the earlier messages, with a compact_boundary entry recording the trigger and pre-compaction token count, and fullscreen mode keeps every earlier message in scrollback. To read the summary itself, use a PostCompact hook (compact_summary) or the Agent SDK's compact_boundary message stream.

With CLAUDE_CODE_TRANSCRIPT_LOCAL_GC=1, long -p and Agent SDK sessions drop pre-compaction history from transcripts over 5 MB.

Transcripts are deleted after cleanupPeriodDays (30 days by default).

Shaping it

QuestionClaude Codechecked at 2.1.299
Customise the summary or hook in

Pass instructions to /compact, or add a # Compact instructions section to the project CLAUDE.md to steer every summary. Hooks: PreCompact (matcher manual or auto) receives the /compact instructions and can block compaction with exit code 2 or {"decision":"block"}; PostCompact receives the compact_summary; and a SessionStart hook with matcher compact adds its output to the compacted context. The built-in summary prompt itself cannot be replaced.

Blocking an automatic compaction that was triggered to recover from an overflow makes the request fail with the original error.

When a request overflows anyway

If the API rejects a request as too long (Prompt is too long), Claude Code compacts the oldest exchanges and retries, compacting again with less recent history if needed, and as a last resort keeps only your newest prompt verbatim. If that fails, or auto-compact is off, you see Context limit reached ยท /compact or /clear to continue. If the context refills to the limit right after compacting several times, it stops with an Autocompact is thrashing error.

A single-exchange conversation can't be compacted; the error explains whether your content or the system prompt, tools and attachments fill the request.

CLAUDE_CODE_DISABLE_UNKNOWN_MODEL_WINDOW_ENFORCEMENT=1 makes unrecognised model IDs rely only on this overflow recovery.

2.1.304 fixes prompt/context-limit errors after interrupting compaction of a conversation rejected as too long, according to the release notes.

2.1.305 reports truncated summary coverage to cloud sessions/teammates; 2.1.306 fixes unrelated API errors being replaced by prompt-too-long and unnecessary compaction; 2.1.307 fixes some large PDF/image/tool-result thrashing. These are publisher fixes, not newly measured thresholds.

2.1.311 usually retries a missing summary once before replacing history. 2.1.310 fixes dense-text overflow recovery retaining too much; 2.1.311 compacts and continues standard-context requests exceeding 200K instead of stopping with a 1M-credit error. Publisher evidence, not a new universal trigger.

Notes and sources

Claude Code, 33 sources
Automatic compaction, and when, 10
Change the trigger or turn it off, 8
Compact by hand, 3
See how full the context is, 3
What replaces the conversation, 4
What survives, 6
Trimming without summarising, 3
The full history afterwards, 5
Customise the summary or hook in, 3
When a request overflows anyway, 10