---
title: "Claude Code auto-compact — set the window for a team · Selan"
description: "When Claude Code auto-compacts, the four places that set the window, why a gateway or non-Claude model can get the wrong one, and how a team sets it once."
url: https://selan.ai/guides/claude-code-auto-compact
updated: 2026-11-20T09:52:49+02:00
---

Guide · Claude Code context

# When does Claude Code auto-compact, and how do you set the window?

Claude Code auto-compacts when the conversation reaches its auto-compact window, which by default is the model's context limit, or about 967K tokens on a model with a native 1M window. You set the window with `/autocompact`, the `autoCompactWindow` setting, the `--autocompact` flag or `CLAUDE_CODE_AUTO_COMPACT_WINDOW`, and the variable beats the other three. Behind a gateway, Claude Code cannot see a lower limit, so someone has to give it the right window. Through Selan, the launch sets it from what the gateway knows.

## When does Claude Code compact by default?

With no window set, Claude Code compacts when the conversation reaches the model's context limit. Anthropic lists the exceptions:

-   **Models with a native 1M window compact at about 967K tokens.** These are the Fable models, Sonnet 5 and later, Haiku 5.5, and Opus 4.7 and later.
-   **Opus 4.6 and Sonnet 4.6 compact at 200K** unless you picked their `[1m]` variant.
-   **`CLAUDE_CODE_DISABLE_1M_CONTEXT=1` holds native 1M models at 200K.** A larger auto-compact window does not lift that hold.
-   **A model id Claude Code does not recognize**, such as a gateway's alias, compacts at the window Claude Code assumes for it, and a warning at startup names that window.
-   **Cloud sessions compact as the conversation approaches the model's limit.**

Compaction clears older tool output, then summarises the conversation. Claude Code reloads the project-root CLAUDE.md and auto memory after it, and re-reads up to five recent files.

## How do you set the auto-compact window?

Claude Code reads the window from four places:

| Where | What it covers | What overrides it |
| --- | --- | --- |
| `/autocompact 500k` | The current model, saved to your user settings under `modelSettings` | The flag, the variable, or a window in a higher settings scope such as managed settings |
| `"autoCompactWindow": 500000` | Every model, in any settings file | The flag, the variable, or a window `/autocompact` saved for a model in the same file |
| `claude --autocompact 500k` | One launch, without changing what is saved | Only the environment variable |
| `CLAUDE_CODE_AUTO_COMPACT_WINDOW` | Every launch started with it set | Nothing |

Each takes a window from 100K to 1M tokens. The command and the flag take `500k`, `500000` or a bare `500`, meaning thousands. Claude Code caps whatever you set at the model's context window.

-   **In the variable, a bare number is tokens.** `CLAUDE_CODE_AUTO_COMPACT_WINDOW=200` reads as 200 tokens, so Claude Code warns and treats it as the 100K minimum.
-   **`CLAUDE_AUTOCOMPACT_PCT_OVERRIDE` compacts at a share of the window.** Set from 1 to 100, it can only lower the point.
-   **`DISABLE_AUTO_COMPACT=1` turns automatic compaction off.** `/compact` still works by hand.

**Good to know:** while the variable is set, the status line's `used_percentage` still measures against the model's full window, so it no longer tells you when compaction will run. `/context` shows what fills the window.

## Why does a gateway or another model get the wrong window?

Behind `ANTHROPIC_BASE_URL`, Claude Code gives each model it recognizes the window that model has on the Anthropic API. It cannot see a lower limit that the gateway, or the provider behind it, enforces.

If your gateway refuses requests above 200K tokens, Anthropic's advice is to set `CLAUDE_CODE_AUTO_COMPACT_WINDOW=200000` where Claude Code starts. Otherwise a long session outgrows what the gateway accepts before Claude Code compacts.

For a model id Claude Code does not recognize, Anthropic lists four fixes:

-   **A model newer than your Claude Code:** run `claude update`.
-   **A gateway's name for a Claude model:** map it with `modelOverrides`, or give a `modelPicker` row `behavesAs` that model.
-   **A model with a 1M window:** append `[1m]` to the id where you set the model.
-   **Any other window:** set `CLAUDE_CODE_MAX_CONTEXT_TOKENS`, such as `400000`, where Claude Code starts.

Claude Code knows each Claude model's window. It does not know your gateway's.

## Should a team compact earlier to spend less?

Often, yes. Each turn sends the whole conversation again, so a session held at 600K sends less on every turn than one that runs close to 1M.

On the Anthropic API, the 1M window has no premium for tokens beyond 200K. Haiku 5.5 is the exception: its prompts over 100K tokens cost more per token.

For a whole team, put `autoCompactWindow` in managed settings. A developer's `/autocompact` then saves its value but keeps the managed window. The `--autocompact` flag and the environment variable still override managed settings for a launch.

**Compacting earlier has a cost too.** A summary keeps what the automatic pass judges important, and path-scoped rules and nested CLAUDE.md files are summarised away with the rest. `/compact` with a focus, such as `/compact focus on the auth bug fix`, keeps what you choose.

## How does Selan set the window for a team?

Selan is a hosted gateway that runs Claude Code and Codex on the accounts your company already pays for. `selan claude` can set `CLAUDE_CODE_AUTO_COMPACT_WINDOW` when it launches Claude Code, the variable that overrides every other source.

-   **An owner can set a company context limit.** In the control app's Settings, under Behaviour, _Context limit_ offers Early (600K), Balanced (800K), Full or Let Claude decide.
-   **A model named with `--model` gets its real window.** Selan tells Claude Code how big it is, so a 262K model compacts at 262K and not at an assumed 200K. If the company limit is smaller, the session compacts there instead.
-   **A model picked in `/model` compacts against one figure per launch:** the company's context limit if an owner set one, otherwise Claude Code's 1M ceiling. When a mid-size window matters, name the model with `--model`.
-   **Your own `--autocompact` wins.** Selan then sets no window for that launch and says so when Claude Code starts.

How a picked model compacts is in [the docs on the picker](https://selan.ai/docs#picker), and naming any model is in [run any model](https://selan.ai/docs#any-model). What a repository's sessions cost, and what to cut, is in [usage practices](https://selan.ai/docs/practices). Limits per person are on [spend limits per developer](https://selan.ai/solutions/spend).

[Start for free](https://auth.selan.ai/r)

Free for up to three seats, and the Free plan needs no card.

## When is Claude Code's own setting the better answer?

When you run Anthropic's own Claude models straight on the Anthropic API. Claude Code knows their windows, and `/autocompact` or `autoCompactWindow` is all a developer needs to compact earlier.

A gateway earns its place once a team runs models from more than one provider, or behind a limit Claude Code cannot see. [Which Claude Code gateway a team needs](https://selan.ai/guides/claude-code-gateway) compares the kinds, and [prompt caching](https://selan.ai/guides/claude-code-prompt-caching) covers the other half of what a long session costs.

### The short version

-   By default Claude Code compacts at the model's context limit, or about 967K tokens on a native 1M model.
-   `CLAUDE_CODE_AUTO_COMPACT_WINDOW` beats `--autocompact`, `/autocompact` and `autoCompactWindow`; in the variable, a bare number is tokens.
-   Behind a gateway, Claude Code uses Anthropic's window for a Claude model and cannot see a lower limit.
-   An unrecognized id needs `modelOverrides`, `behavesAs`, `[1m]` or `CLAUDE_CODE_MAX_CONTEXT_TOKENS`.
-   Through Selan, a model named with `--model` compacts at its real window, or at the company's context limit when that is smaller.

When Claude Code compacts, and how its settings, flag and variables set the window, is as Anthropic's model configuration page, context window page, environment variable reference and settings reference (code.claude.com/docs/en/model-config, /context-window, /env-vars and /settings-reference) stated it on 15 November 2026. It changes with nearly every release: check the date before relying on it. What Selan does is as its CLI, control app and docs stated it on that date.
