← All posts

// The guide

Claude Code auto-compact: the threshold setting that actually exists.

2026-07-16 · updated 2026-08-01 · 6 min read · by Nabil BA-MOH

If you searched for a way to make Claude Code compact earlier, you probably found the same tip we did: add "autoCompactThreshold": 80 to your settings.json. We tried it. It does nothing. So we read the code Claude Code actually ships and found the knob that does work. Here it is, with the honest caveats.

Updated 2026-08-01. We warned that this knob could change without notice. It did. On current Claude Code the percentage override no longer works alone: it needs CLAUDE_CODE_AUTO_COMPACT_WINDOW set alongside it, and that window must match your model's real context window. The post below is updated accordingly.

Everything below was verified on Claude Code v2.1.211 by reading the shipped binary, not by quoting other blog posts — and re-checked on v2.1.220 on 2026-08-01. Version matters; re-check after big updates.

Auto-compact in one minute

Claude Code works inside a fixed context window. As a session grows, the window fills. When it gets close to full, auto-compact kicks in: the conversation is summarized, the summary replaces the history, and the session continues with room to breathe.

The catch is timing. By default, compaction fires near the ceiling. At that point the summarizer is working under pressure, with a nearly full window, and the summary loses more nuance than an earlier, deliberate one would. If you have ever watched Claude Code come back from a compact slightly dumber about your task, that is why.

So the question is legitimate: how do you move the trigger earlier?

The tip that doesn't work

A widely shared tip says to put this in settings.json:

{ "autoCompactThreshold": 80 }

That key does not exist as a setting. In the 2.1.211 binary, the string autoCompactThreshold appears only as an internal telemetry field — a name the app uses when reporting metrics, not a setting it reads. Nothing in the settings loader looks for it.

The trap is that it fails silently. settings.json accepts unknown keys without complaint, so you add the line, nothing breaks, and you assume it worked. It didn't. Your threshold never moved.

What's officially documented

The official settings docs give you exactly two switches, both on/off:

No threshold. Officially, auto-compact is all or nothing.

The knob that actually exists — it's a pair now

The binary reads two environment variables the settings docs never mention:

CLAUDE_CODE_AUTO_COMPACT_WINDOW=1000000   # your model's real context window
CLAUDE_AUTOCOMPACT_PCT_OVERRIDE=40        # trigger at 40% of it

The rule: the auto-compact threshold becomes window × percentage / 100 tokens. With a 1M-window model and 40, auto-compact triggers around 400K tokens, instead of near the top.

When we first published this post, the percentage override alone was enough on the version we read (2.1.211). On current versions it is not: the override only applies when compaction runs in proactive mode, and a normal local session is proactive only when CLAUDE_CODE_AUTO_COMPACT_WINDOW is set too. Set the percentage alone and it is read, then ignored — the same silent failure we called out above, which is exactly why this update exists.

Two traps in the window value:

To make the pair permanent, put both in the env block of ~/.claude/settings.json:

{
  "env": {
    "CLAUDE_CODE_AUTO_COMPACT_WINDOW": "1000000",
    "CLAUDE_AUTOCOMPACT_PCT_OVERRIDE": "40"
  }
}

Or export them in your shell profile. Either way they take effect on new sessions, not the one currently running.

How the engine decides: warn, compact, blocked

Under the hood, Claude Code computes a level from the current token count. Three levels matter:

The percentage in the status line and these levels are consistent with each other, so what you see is what the engine acts on. Lowering the override moves the whole ladder down: you get warned earlier and compacted earlier, far away from the blocked zone where summaries are at their worst.

The honest caveats

The better habit: /compact on your terms

The override is a safety net, not a strategy. The best compaction is the one you run yourself: type /compact at a natural breakpoint — a task finished, findings written to a file, state saved. A deliberate compact with everything persisted beats any automatic one that lands mid-task, whatever the threshold.

The pattern that works: finish a chunk of work, persist what matters to disk, then compact. The override exists for the sessions where you forget.

We hit all of this while building Klyr, which gives Claude Code a memory that lives outside the context window — so what your assistant knows about you survives every compact, every session, every machine. Compaction is exactly the moment a stock assistant forgets; it is the moment Klyr is built for. The two ways into Klyr are laid out on the homepage.

New to Claude Code entirely? Start with our non-developer guide first.

FAQ

Does autoCompactThreshold in settings.json work?

No. In Claude Code 2.1.211 that string exists only as an internal telemetry field name. The settings loader never reads it. It fails silently, which is why the tip keeps spreading.

How do I make Claude Code auto-compact earlier?

Set the pair: CLAUDE_CODE_AUTO_COMPACT_WINDOW to your model's real context window (for example 1000000) and CLAUDE_AUTOCOMPACT_PCT_OVERRIDE to a value from 1 to 100, for example 40. The threshold becomes window × percentage. Put both in the env block of ~/.claude/settings.json to make them persistent. New sessions only.

Why does CLAUDE_AUTOCOMPACT_PCT_OVERRIDE do nothing on its own?

On current versions the override only applies to proactive compaction. A normal local session compacts reactively at the context limit unless CLAUDE_CODE_AUTO_COMPACT_WINDOW is also set. Without it, the percentage is read and silently ignored.

What should CLAUDE_CODE_AUTO_COMPACT_WINDOW be?

Your model's real context window: 1,000,000 for the current Opus, Sonnet and Fable generation, 200,000 for Haiku 4.5. Set it larger than the real window and the computed threshold lands past the ceiling, which silently disables proactive compaction. There is no dynamic per-model value.

Is CLAUDE_AUTOCOMPACT_PCT_OVERRIDE official?

No. It is undocumented and internally marked as a test override. Verified on version 2.1.211 by reading the shipped binary, re-checked on 2.1.220 on 2026-08-01 — and its behavior already changed once between the two. It may change or disappear in any release.

What value should I use?

40 to 70. Lower means earlier, better-quality compaction but more frequent summarization, and every summary loses some nuance. We run 40. The percentage is clamped at roughly 83 — it can only pull the trigger earlier than the default, never later.

How do I disable auto-compact completely?

Set autoCompactEnabled to false in settings.json, or set the environment variable DISABLE_AUTO_COMPACT=1. Both are official. You will then hit the context ceiling with no safety net, so compact manually.

What do the warn, compact and blocked levels mean?

Internal levels Claude Code computes from your token count: warn means you are within 20,000 tokens of the auto-compact threshold, compact means the threshold is reached and auto-compact fires, blocked means you are near the hard ceiling of the window.

// Share this guide