|
| 1 | +# Capping the agent's context window |
| 2 | + |
| 3 | +Analysis and design for `agent.context_window`: one harness-neutral field that caps the |
| 4 | +context window an agent works within, set per agent config or per experiment variant. |
| 5 | + |
| 6 | +## Problem and evidence |
| 7 | + |
| 8 | +A benchmark of Claude Code on Sonnet 5 (Bedrock, 1M-token window) let runs grow to |
| 9 | +250k-480k tokens of context. Above about 150k tokens each extended-thinking step got |
| 10 | +about 10x slower, so most runs hit the 2-hour task limit. The operator wants to measure |
| 11 | +pass rate and cost at several context caps, for example one variant at 200k and one at |
| 12 | +400k, on the same tasks. |
| 13 | + |
| 14 | +Before this change no harness-neutral way existed: |
| 15 | + |
| 16 | +- **claude-code**: `claude_settings` (`--settings`) could carry `autoCompactWindow`. That |
| 17 | + works for one harness only, and when `claude_settings` is a file path a variant cannot |
| 18 | + merge one key into it. |
| 19 | +- **codex**: `CodexAgentConfig` has no pass-through. `CodexAgent._build_thread_options` |
| 20 | + builds the thread `config` dict itself, so no value reached Codex. |
| 21 | +- **antigravity, opencode, pi, delegate**: no knob is wired. |
| 22 | + |
| 23 | +## How each harness controls its window |
| 24 | + |
| 25 | +### claude-code |
| 26 | + |
| 27 | +Verified in the CLI the SDK spawns. `claude-agent-sdk` runs its **bundled** CLI before it |
| 28 | +looks for `claude` on `PATH` (`SubprocessCLITransport._find_cli`), so the CLI that runs is |
| 29 | +the one the SDK wheel bundles, not the one the agent image installs with npm: |
| 30 | + |
| 31 | +| coder_eval | claude-agent-sdk (uv.lock) | bundled CLI | image npm CLI | |
| 32 | +|---|---|---|---| |
| 33 | +| 0.12.1 | 0.2.124 | 2.1.216 | 2.1.177 | |
| 34 | +| 0.12.12 | 0.2.159 | 2.1.281 | 2.1.281 | |
| 35 | + |
| 36 | +`environment_info.claude_code_cli` records `claude -v` from `PATH`, so a 0.12.1 run |
| 37 | +records 2.1.177 while 2.1.216 ran. That is a separate defect and is not fixed here. |
| 38 | + |
| 39 | +Three inputs set the auto-compact window. Both bundled CLIs (2.1.216 and 2.1.281) contain |
| 40 | +the same resolution function. The highest precedence comes first: |
| 41 | + |
| 42 | +1. `CLAUDE_CODE_AUTO_COMPACT_WINDOW` environment variable: a plain token count. It |
| 43 | + outranks the flag and every settings scope. |
| 44 | +2. `--autocompact <auto|tokens>`: added in 2.1.221 (CLI reference). It is absent from |
| 45 | + 2.1.216 as a parsed option. |
| 46 | +3. `autoCompactWindow` settings key. The settings schema is |
| 47 | + `int().min(100000).max(1000000).optional().catch(undefined)`, so an out-of-range |
| 48 | + value is **silently dropped**. |
| 49 | + |
| 50 | +The effective window is `min(model context window, configured value)`. Auto-compaction |
| 51 | +starts when the context approaches that window. |
| 52 | + |
| 53 | +### codex |
| 54 | + |
| 55 | +Verified in the `openai-codex-cli-bin` 0.156.1 binary (the pin) and its source at tag |
| 56 | +`rust-v0.156.1`: |
| 57 | + |
| 58 | +- `model_context_window` ("Size of the context window for the model, in tokens"): |
| 59 | + `with_config_overrides` sets the model's `context_window` to |
| 60 | + `min(value, max_context_window)`. Three things follow from it. The default |
| 61 | + auto-compact limit becomes 90% of it. The hard cap, which forces compaction, becomes |
| 62 | + `context_window * effective_context_window_percent / 100`. The remaining-window figure |
| 63 | + the model is shown also changes. |
| 64 | +- `model_auto_compact_token_limit` ("Token usage threshold triggering auto-compaction"): |
| 65 | + the trigger only, clamped to 90% of the resolved window. It does not change the window. |
| 66 | +- `model_auto_compact_token_limit_scope` (`total` by default, or `body_after_prefix`) and |
| 67 | + `model_post_turn_compact_threshold_percent` (turn-end compaction, off by default) tune |
| 68 | + when compaction starts. They are not caps. |
| 69 | + |
| 70 | +The thread `config` dict `thread_start` takes is the same override surface that |
| 71 | +coder_eval already uses for `enabled_tools` and `model_providers`. |
| 72 | + |
| 73 | +### antigravity, opencode, pi, delegate |
| 74 | + |
| 75 | +None of them has a context-window knob wired in coder_eval. They are out of scope, and |
| 76 | +they reject the field. |
| 77 | + |
| 78 | +## Options considered |
| 79 | + |
| 80 | +**Per-harness pass-through** (a Codex `config` pass-through next to `claude_settings`). |
| 81 | +It is flexible, but a variant would have to name a different key per harness, and the |
| 82 | +recorded value would mean something different on each. It also opens a free-form |
| 83 | +pass-through on Codex with no denylist. Rejected. |
| 84 | + |
| 85 | +**A field under `run_limits`.** Run limits are caps that the orchestrator or the agent |
| 86 | +enforces on a run (turns, time, tokens, USD), and the parity table lists them. A context |
| 87 | +window is not a spend cap. It is a harness setting that changes what the agent does, and |
| 88 | +it depends on the agent type, which `run_limits` does not know. The guide also says that |
| 89 | +`agent:` holds no run-time caps. Rejected. |
| 90 | + |
| 91 | +**A field on the agent config.** One name, accepted on a type-less config so that |
| 92 | +experiment defaults and variants can set it, and validated against the concrete type |
| 93 | +when the config resolves. **Chosen.** |
| 94 | + |
| 95 | +Naming: `context_window`, in tokens. It names the quantity the operator caps, matches |
| 96 | +Codex's `model_context_window`, and avoids "auto-compact", which describes a mechanism of |
| 97 | +one harness. `max_context_tokens` was considered, but it reads as a per-request input |
| 98 | +limit, which neither harness enforces. |
| 99 | + |
| 100 | +## Recommended design |
| 101 | + |
| 102 | +`BaseAgentConfig.context_window: int | None` (default `None`, `gt=0`). |
| 103 | + |
| 104 | +- Each config class declares `_context_window_range: ClassVar[tuple[int, int | None] | |
| 105 | + None]`. `None` (the base default) means that the harness cannot apply the field. |
| 106 | + `ClaudeCodeAgentConfig` declares `(100_000, 1_000_000)` and `CodexAgentConfig` declares |
| 107 | + `(32_768, None)`. |
| 108 | +- `check_context_window_supported` (model validator) rejects an unsupported type or an |
| 109 | + out-of-range value as soon as the type is known. A plugin agent inherits `None`, so it |
| 110 | + also rejects the field until it opts in. |
| 111 | +- The field uses the default `replace` merge strategy. It is set at any of the five |
| 112 | + layers, and with `-D agent.context_window=N`. |
| 113 | + |
| 114 | +Per-harness mapping: |
| 115 | + |
| 116 | +- **claude-code**: `CLAUDE_CODE_AUTO_COMPACT_WINDOW=<N>` in the SDK `env`. The |
| 117 | + environment variable was chosen over the settings key and the flag for four reasons. |
| 118 | + It has the highest precedence, so host, project and `claude_settings` values cannot |
| 119 | + override the cap. It works whether `claude_settings` is a dict or a file path. Both |
| 120 | + bundled CLIs read it, which the flag does not. `extra_args` stays framework-owned. |
| 121 | + `claude_settings.autoCompactWindow` together with `context_window` is a load error, |
| 122 | + because the environment variable would silently override it. |
| 123 | +- **codex**: `model_context_window: <N>` in the thread `config`. No trigger key is set, |
| 124 | + so Codex compacts at its default 90% of the window. |
| 125 | + |
| 126 | +## Validation |
| 127 | + |
| 128 | +- `gt=0` on every config, so a type-less config rejects nonsense as well. |
| 129 | +- claude-code: 100000-1000000, the CLI's documented range. Outside that range the CLI |
| 130 | + drops a settings value silently, so coder_eval enforces the range itself. |
| 131 | +- codex: 32768 and up. Codex clamps the value to the model's catalog maximum, and the |
| 132 | + floor rejects a window too small to work in, such as a YAML `yes` coerced to 1. |
| 133 | +- every other type: rejected at load, naming the supported types. A variant that |
| 134 | + switches `type` to an unsupported harness fails when the experiment resolves. |
| 135 | + |
| 136 | +## Recording |
| 137 | + |
| 138 | +The resolved agent config is persisted as `agent_config` on every task row (`run.json` |
| 139 | +and `task.json`). `agent.context_window` therefore appears per task, and config lineage |
| 140 | +records which layer set it (for example, the variant). The experiment report groups by |
| 141 | +variant, so a cap per variant groups without new report code. On claude-code the |
| 142 | +`sdk_options` dump also shows the environment variable in `env`. |
| 143 | + |
| 144 | +## Open questions |
| 145 | + |
| 146 | +- **A silent clamp to the model's window.** Neither harness reports that the model's |
| 147 | + window lowered the cap. A cap above the model's window runs at the model's window. |
| 148 | + Recording the effective window would need a per-model catalog in coder_eval. |
| 149 | +- **Trigger points differ.** Codex compacts at 90% of the window. Claude Code compacts |
| 150 | + when the context approaches the window, minus an internal buffer. Equal caps therefore |
| 151 | + do not compact at exactly the same token count. A separate harness-neutral trigger |
| 152 | + field is possible, but it is not added until a benchmark needs it. |
| 153 | +- **A host `CLAUDE_CODE_AUTO_COMPACT_WINDOW` and uncapped runs.** Decided: the SDK starts |
| 154 | + the CLI with `os.environ` underneath `options.env`, so when the field is unset the |
| 155 | + claude-code agent blanks a host value. A run recorded as uncapped is never capped by |
| 156 | + the host. This changes every claude-code run on a host that sets the variable, |
| 157 | + including runs that never set `context_window`. Codex has no equivalent: an unset |
| 158 | + field leaves a host `model_context_window` in `$CODEX_HOME/config.toml` in effect. |
| 159 | +- **The recorded CLI version is wrong.** See the version table above: |
| 160 | + `environment_info.claude_code_cli` should record the bundled CLI version. |
| 161 | +- **Other harnesses.** Pi can lower a model's window with |
| 162 | + `providers.<provider>.modelOverrides.<model>.contextWindow` in `models.json`. A mapping |
| 163 | + was written and then reverted (d1757a60), because no live run backed it and no task |
| 164 | + uses `type: pi`; it comes back with a live test. OpenCode (`limit.context` and |
| 165 | + `limit.input` in provider config) needs a `limit.output` coder_eval does not know, and |
| 166 | + an unpinned CLI. Antigravity's `CompactionConfig(token_threshold=N)` only moves the |
| 167 | + trigger. Delegate keeps its history in the backend. Each needs its own verification |
| 168 | + before it opts in. |
| 169 | +- **How often a run compacted is not recorded.** A cap A/B cannot yet tell "cap never |
| 170 | + reached" from "compacted N times" from the task row alone. |
0 commit comments