If you leave a long Claude Code session open and come back after almost an hour, you may see this message:Compacted while idle, before the prompt cache expired. This is Claude Code's new Idle Compaction (automatic compaction when idle): when a session has been idle for about 54 minutes and the context has reached 200K tokens, Claude Code compresses the conversation into a summary before the 1-hour prompt cache expires. When you come back and send your next message, it only has to reprocess the summary instead of recomputing the entire history.
This feature is most useful for subscription users. If you use an API key or a third-party API (including AICodeWith), the default cache lasts only 5 minutes, so you'll rarely trigger it unless you change the cache duration to 1 hour. Below, we cover how it works, how to turn it off, how to change the threshold, and how third-party API users should configure it.
The short version
| Question | Answer |
|---|---|
| What it is | Idle long sessions are automatically compacted into a summary before the cache expires |
| When it triggers | An interactive terminal session on a 1-hour cache, with context ≥ 200,000 tokens, idle for about 54 minutes, and the computer not asleep |
| What you see when you return | Compacted while idle, before the prompt cache expired |
| How to turn it off | Set "idleCompaction": false in settings.json, or set the environment variable DISABLE_AUTO_COMPACT=1 |
| How to change the threshold | Environment variable CLAUDE_CODE_IDLE_COMPACT_MIN_TOKENS, default 200000, minimum 100000 |
| Version | The changelog lists the feature in 2.1.299; the related settings require v2.1.290 or later |
What you'll notice
Anthropic's Lydia Hallie explained on X that Claude Code now compacts sessions that have been idle for a long time before the prompt cache expires (about 1 hour). When you return, it only has to reprocess the summary rather than the whole conversation, which saves usage (original post).
SemiAnalysis also posted that the feature shipped with Claude Code 2.1.299: sessions with at least 200k of context are automatically compacted after about 54 minutes of idle time (original post).
So the behavior is simple: the session was long when you left, the conversation history has become a summary when you return, and you'll see the message above in the UI.
How it works
Claude Code resends the full context to the model on every turn. The prompt cache lets the unchanged prefix be billed at a lower cached rate instead of full price each time. But the cache has a time to live (TTL). If no request arrives before it runs out, it expires, and the next turn has to reprocess the whole history as uncached input. That's why the first message after an hour away is expensive and slow (official prompt caching docs).
Idle Compaction targets the window just before expiry:
- After your last request, the session sits idle for about 54 minutes;
- At that point the 1-hour cache hasn't expired yet, so Claude Code starts compaction, and the request that generates the summary can read from the cache, which is cheaper than a cold start;
- When you return and send your next message, it sends the summary instead of the full history.
According to the official docs, all of these conditions must be met: an interactive terminal session; a session on the 1-hour cache TTL; Claude Code can fetch feature flags from Anthropic; the context has reached the threshold (default 200,000); and the session has been idle for about 54 minutes with the computer awake at that time (settings docs: idleCompaction).
Who actually benefits: subscription vs. API
What matters is the cache duration. Here are the defaults from the official docs:
| Request type | Claude subscription (within plan limits) | Usage-based credits, API key, or cloud provider |
|---|---|---|
| Main conversation | 1 hour | 5 minutes |
| Other requests (subagents, compaction, etc.) | 5 minutes (1 hour for a few server-controlled auxiliary requests) | 5 minutes |
Source: Which TTL each request gets
- Subscribers: The main conversation uses a 1-hour cache by default, so Idle Compaction works right away.
- API key and third-party API users: The main conversation only gets a 5-minute cache by default. The cache has already expired after 5 minutes, so it never lasts until minute 54, and by default there is basically no benefit.
To make it work for API users, change the main conversation cache to 1 hour: set "promptCacheTtl": "1h" in settings.json, or set the environment variable CLAUDE_CODE_PROMPT_CACHE_TTL=1h (requires v2.1.242 or later). Note that the 1-hour cache has a higher write price than the 5-minute cache. If your work rarely sits idle for more than 5 minutes, extending the cache may actually cost you more.
When you go through a gateway via ANTHROPIC_BASE_URL, the official docs say that part of the request information for the 1-hour cache is carried in the anthropic-beta request header, so the gateway needs to forward that header unchanged.
Doing the math: recomputing a 200K context vs. recomputing a summary
The figures below are estimates based on public list prices and only show the order of magnitude. The unit price comes from the AICodeWith model page, which showed an input price for Claude Sonnet 5.5 of ¥2.736 / million tokens (CNY) as of 14:13 on October 15, 2026. The calculation uses the regular input price and excludes output and the cache write premium:
| Scenario | Input to reprocess | Estimated cost (CNY) |
|---|---|---|
| Recompute the full context after the cache expires | 200,000 tokens | About ¥0.55 |
| Process only the summary after compaction | About 5,000 tokens | About ¥0.014 |
That is roughly a 40x difference. A single instance doesn't amount to much money, but if you step away and come back several times a day, the gap grows with every long session.
See live AICodeWith pricing → View all models →
Check your version first
claude --version
The official changelog added idle compaction in 2.1.299, and it also states that CLAUDE_CODE_IDLE_COMPACT_MIN_TOKENS and "idleCompaction": false both require v2.1.290 or later (Claude Code CHANGELOG). If your version is too old, run:
claude update
The official docs also note that in the first session after an update, features that depend on feature flags may not show up yet and will only take effect in the next session.
How to turn it off and change the threshold
Turn it off: settings.json
In ~/.claude/settings.json (or your project's settings file), add:
{
"idleCompaction": false
}
Turn it off: environment variable
export DISABLE_AUTO_COMPACT=1
DISABLE_AUTO_COMPACT disables both the auto-compaction that triggers near the context limit and idle compaction, while manual /compact still works. If you only want to turn off idle compaction and keep auto-compaction, use the idleCompaction setting above.
Change the threshold
Use CLAUDE_CODE_IDLE_COMPACT_MIN_TOKENS to set the context size that triggers compaction. The default is 200000. Enter a positive integer; values below 100000 are raised to 100000. Put it in the env section of settings.json:
{
"env": {
"CLAUDE_CODE_IDLE_COMPACT_MIN_TOKENS": "150000"
}
}
When you should turn it off
Compaction replaces the original conversation with a summary, so details get lost. In the following cases, turn it off or handle things yourself before you step away:
- Halfway through a complex debugging session: The original error text, the fixes you tried, and the hypotheses you ruled out may be reduced to a single line in the summary;
- When you need to quote earlier output exactly: log snippets, long command output, API responses;
- Long refactoring chains: many conventions get established along the way, and a summary can easily drop boundary conditions.
A more controllable approach is to run /compact manually before you step away, compacting at a natural breakpoint in the task instead of letting it happen automatically at the 54-minute mark. The official docs also recommend running /compact manually at natural breakpoints between tasks. Put important project rules in CLAUDE.md, which is reloaded from disk after compaction. See "How to Write CLAUDE.md for Claude Code".
How to set it up with AICodeWith
Using Claude Code through AICodeWith counts as a custom ANTHROPIC_BASE_URL setup, where the main conversation uses a 5-minute cache by default. To use idle compaction, configure the Base URL, Key, and 1-hour cache together in ~/.claude/settings.json:
{
"env": {
"ANTHROPIC_BASE_URL": "https://api.aicodewith.ai",
"ANTHROPIC_AUTH_TOKEN": "替换成你的 AICodeWith API Key",
"ANTHROPIC_API_KEY": ""
},
"promptCacheTtl": "1h"
}
Do not add /v1 to the Base URL. For the full setup steps and error handling, see "How to Connect Claude Code to a Third-Party API". For choosing a primary model, see "Which Model to Use with Claude Code".
Sign up for AICodeWith → Create an API Key →
Common errors and self-check
| Symptom | Possible cause | How to check |
|---|---|---|
| Never seen that prompt | Version is too old, or the main conversation is not using a 1-hour cache | claude --version; for API users, check promptCacheTtl |
| Set to 1h but it still doesn't trigger | Context hasn't reached the threshold, or the computer went to sleep at minute 54 | Use /usage to check the Prompt cache line and confirm the cache is still warm; keep the computer awake |
| Want to confirm the cache duration took effect | — | Run claude -p "hello" --output-format json and look at whether usage.cache_creation shows ephemeral_1h_input_tokens or ephemeral_5m_input_tokens |
| Set idleCompaction: false but it has no effect | Version is below 2.1.290 | Upgrade; on older versions you can turn it off with autoCompactEnabled |
| Set DISABLE_TELEMETRY and similar variables, and it no longer triggers | These variables disable feature-flag fetching, which idle compaction depends on | See the "requires feature flags" notes in the official env-vars docs |
FAQ
Does Idle Compaction cost extra?
Compaction itself sends one request to generate a summary, but it fires before the cache expires, so it can read from the cache. According to the official docs, when the cache is still warm, a single /compact costs only a small fraction of what the context size would suggest.
Does it work for API Key users?
Yes, but you need to set the main conversation cache to 1 hour: "promptCacheTtl": "1h" or CLAUDE_CODE_PROMPT_CACHE_TTL=1h. With the default 5-minute cache, it basically never triggers.
Does turning it off affect manual /compact?
No.idleCompaction: false and DISABLE_AUTO_COMPACT=1 both leave manual /compact unaffected.
Can I set the threshold to 50,000?
No. Values below 100000 are raised to 100000.
Does it trigger in non-interactive -p mode?
No. One of the trigger conditions listed in the official docs is an interactive terminal session.
Sources
- Lydia Hallie (Anthropic) post on X: x.com/lydiahallie/status/2110484840256819380
- SemiAnalysis post on X: x.com/SemiAnalysis_/status/2110444144233480309
- Claude Code changelog: github.com/anthropics/claude-code/blob/main/CHANGELOG.md
- Claude Code prompt caching docs: code.claude.com/docs/en/prompt-caching
- Claude Code settings docs (idleCompaction): code.claude.com/docs/en/settings-reference
- Claude Code environment variables docs: code.claude.com/docs/en/env-vars



