zed/docs/src
Enoch 9a5344b9eb
agent: Add agent.compaction_model setting for context compaction (#60012)
# Objective

Add a new `agent.compaction_model` setting that lets users specify a
separate language model for context compaction (`/compact` and
auto-compaction), independent of the thread's active conversation model.

Compaction is just summarization — there's no reason to pay Opus prices
for it when a cheaper model does the job faster. We've also seen
reasoning models misbehave on this task (empty responses, repetition
loops at high effort), so picking a dedicated non-reasoning model for
compaction is useful.


## Solution

- New `compaction_model: Option<LanguageModelSelection>` field in
`AgentSettingsContent` and `AgentSettings`, mirroring
`thread_summary_model`.
- New `compaction_model: Option<ConfiguredModel>` slot on
`LanguageModelRegistry` with `select_/set_/compaction_model()` trio,
mirroring the existing pattern. The setter deliberately does **not**
emit a registry event in v1; callers read the slot lazily at compaction
time.
- New `Thread::compaction_model(&self, cx: &App)` helper that returns
the configured model or falls back to `self.model()`. Two call sites —
`Thread::compact` and `perform_compaction_if_needed` — now go through
this helper instead of reading `self.model()` directly.
- `build_compaction_telemetry` accepts the compaction model explicitly
so the `model` field reflects the model that actually streamed the
request. `max_tokens` still derives from `thread.model()` (threshold
semantics are unchanged).
- Documentation updated at `docs/src/ai/agent-settings.md` (new
user-facing setting).

**Example:**

```json
{
  "agent": {
    "default_model": {
      "provider": "anthropic",
      "model": "claude-opus-4-6"
    },
    "compaction_model": {
      "provider": "anthropic",
      "model": "claude-sonnet-4-5"
    }
  }
}
```

**Resolution chain:**

```
agent.compaction_model (if set & available)
  → thread.model()  (always available, current behavior)
```

**Behavior change:**

| Trigger | Before | After |
| --- | --- | --- |
| Manual `/compact` | Uses `thread.model()` | Uses
`agent.compaction_model` if set & available; else `thread.model()` |
| Auto-compaction | Uses `thread.model()` | Same as above |
| `/compact` when thread has no model | `NoModelConfiguredError` |
Succeeds if `compaction_model` resolves |
| `compaction_model` configured but provider missing / model id unknown
| n/a | Falls back to `thread.model()` and logs a one-time warning |

**Explicit non-goals:**

- No `Event::CompactionModelChanged`(no consumer; the `_cx` parameter on
`set_compaction_model` is intentionally accepted for future use).
- No GUI selector (consistent with all other feature-specific models).
- No per-profile override.
- No runtime API-error fallback — only config-time failure (provider not
registered, model id not in `provided_models`) triggers fallback. This
matches every other feature-specific model.
- No change to threshold calculation, auto-compact trigger, or
`COMPACTION_PROMPT`.
- No propagation to subagent threads.

## Testing

3 new unit tests in `crates/agent/src/thread.rs::tests`:

- `test_compaction_uses_configured_compaction_model` — manual `/compact`
routes to the configured model; thread's primary model receives no
request; telemetry reflects the configured model.
- `test_compaction_falls_back_when_compaction_model_unavailable` —
configured-but-unresolvable falls back to `thread.model()`; telemetry
reflects the fallback model.
- `test_auto_compaction_uses_compaction_model` — auto-compaction
triggered by threshold honors the same setting.

All 14 existing compaction tests still pass. Test suites in
`crates/agent_settings`, `crates/language_model`,
`crates/settings_content`, `crates/agent_ui` unchanged. `cargo clippy`
clean on the changed crates.

**How reviewers can test:**

1. Add `agent.compaction_model` to `settings.json` with a cheaper model
than the thread's primary model, run `/compact`, observe the cheaper
model receives the request.
2. Set `agent.compaction_model` to a non-existent provider/model id, run
`/compact`, observe fallback to thread model and a `log::warn!` line.
3. Trigger auto-compaction by reaching the threshold, observe it uses
`compaction_model`.

**Platforms tested:** local Linux (cargo check + cargo test on agent /
agent_settings / language_model / settings_content / agent_ui crates).

## Self-Review Checklist:

- [x] I've reviewed my own diff for quality, security, and reliability
- [x] Unsafe blocks (if any) have justifying comments — no `unsafe`
blocks introduced
- [x] The content adheres to Zed's UI standards — N/A: settings-only
change, no UI touched
- [x] Tests cover the new/changed behavior
- [x] Performance impact has been considered and is acceptable — model
resolution is an O(1) registry lookup, not on any hot path; no new work
in the streaming loop


Release Notes:

- agent: Add support for specifying which model is used for compaction
(`agent.compaction_model`)

---------

Co-authored-by: Bennet Bo Fenner <bennetbo@gmx.de>
Co-authored-by: Bennet Bo Fenner <bennet@zed.dev>
2026-07-24 12:48:58 +00:00
..
account Update Zed-hosted model documentation (#60771) 2026-07-10 23:24:52 +00:00
ai agent: Add agent.compaction_model setting for context compaction (#60012) 2026-07-24 12:48:58 +00:00
business Update Zed-hosted model documentation (#60771) 2026-07-10 23:24:52 +00:00
collaboration docs: Update actions format (#54869) 2026-05-07 06:55:11 +00:00
development Remove stale musl linker error workaround from Linux dev docs (#61444) 2026-07-22 10:04:12 +00:00
extensions extension: Check for rustup before calling it (#56090) 2026-07-21 10:42:39 +00:00
languages docs: Add PHPantom language server configuration (#61387) 2026-07-22 13:46:00 +00:00
migrate docs: Use official brew cask (#61495) 2026-07-23 05:26:09 +00:00
reference Split VSCode and Zed keymap files (#61532) 2026-07-24 07:54:03 +00:00
all-actions.md docs: Apply documentation standards across all docs (#49177) 2026-02-17 20:58:17 -06:00
appearance.md docs: Update actions format (#54869) 2026-05-07 06:55:11 +00:00
authentication.md Fix typos and grammatical mistakes in docs (#59495) 2026-07-08 12:32:00 +00:00
command-palette.md Fix typos and grammatical mistakes in docs (#59495) 2026-07-08 12:32:00 +00:00
completions.md Remove Supermaven-related code from Zed (#50537) 2026-03-02 22:18:49 +00:00
configuring-languages.md docs: Add PHPantom language server configuration (#61387) 2026-07-22 13:46:00 +00:00
configuring-zed.md Restructure AI docs (#57614) 2026-06-04 15:55:21 +00:00
debugger.md Fix typos and grammatical mistakes in docs (#59495) 2026-07-08 12:32:00 +00:00
dev-containers.md dev_container: Support the classic Docker builder via a setting (#59288) 2026-06-15 07:04:03 +00:00
development.md Improve documentation for ETW profiling (#50426) 2026-02-28 23:03:49 -05:00
diagnostics.md Fix typos and grammatical mistakes in docs (#59495) 2026-07-08 12:32:00 +00:00
editing-code.md docs: SEO content improvements for AI section (#49128) 2026-02-13 13:05:57 -08:00
environment.md Fix typos and grammatical mistakes in docs (#59495) 2026-07-08 12:32:00 +00:00
extensions.md docs: Update agent server extension documentation (#58306) 2026-06-02 08:50:46 +00:00
finding-navigating.md docs: Add new text finder (#60189) 2026-07-03 18:46:05 +00:00
getting-started.md Fixes for AI docs (#58969) 2026-06-11 15:15:55 +00:00
git.md Fix typos and grammatical mistakes in docs (#59495) 2026-07-08 12:32:00 +00:00
globs.md Fix grammatical errors throughout the documentation (#58183) 2026-05-31 22:05:41 +00:00
helix.md docs: Apply documentation standards across all docs (#49177) 2026-02-17 20:58:17 -06:00
icon-themes.md docs: Update actions format (#54869) 2026-05-07 06:55:11 +00:00
installation.md Fix grammatical errors throughout the documentation (#58183) 2026-05-31 22:05:41 +00:00
key-bindings.md Split VSCode and Zed keymap files (#61532) 2026-07-24 07:54:03 +00:00
languages.md Fix grammatical errors throughout the documentation (#58183) 2026-05-31 22:05:41 +00:00
linux.md Remove the 3rd party section (#61087) 2026-07-16 07:51:58 +00:00
macos.md install_cli: Show notification instead of failing CLI install (#59575) 2026-06-22 18:20:57 +00:00
modelines.md Add vim/emacs modeline support (#49267) 2026-03-25 03:15:51 +00:00
multibuffers.md Fix grammatical errors throughout the documentation (#58183) 2026-05-31 22:05:41 +00:00
outline-panel.md docs: Update actions format (#54869) 2026-05-07 06:55:11 +00:00
performance.md Fix typos and grammatical mistakes in docs (#59495) 2026-07-08 12:32:00 +00:00
project-panel.md project_panel: Open files in a permanent tab on middle click (#60563) 2026-07-08 13:06:10 +00:00
quick-start.md
remote-development.md Fix typos and grammatical mistakes in docs (#59495) 2026-07-08 12:32:00 +00:00
repl.md Fix typos and grammatical mistakes in docs (#59495) 2026-07-08 12:32:00 +00:00
roles.md Document Billing Manager role (#58447) 2026-06-03 18:17:18 +00:00
running-testing.md docs: Apply documentation standards across all docs (#49177) 2026-02-17 20:58:17 -06:00
semantic-tokens.md docs: Update actions format (#54869) 2026-05-07 06:55:11 +00:00
snippets.md snippets: Strip path separators from language snippet file names (#61421) 2026-07-23 04:49:39 +00:00
soc2.md docs: Restructure nav and add Zed Business section (#51915) 2026-05-06 16:07:12 +00:00
SUMMARY.md Add agent sandboxing docs (#59774) 2026-06-23 23:54:28 +00:00
tab-switcher.md docs: Apply documentation standards across all docs (#49177) 2026-02-17 20:58:17 -06:00
tasks.md git_ui: Pass the clicked ref to git graph custom commands (#58781) 2026-06-09 06:30:07 +00:00
telemetry.md docs: Fix stale panic struct reference in telemetry docs (#61145) 2026-07-21 06:01:42 +00:00
terminal.md terminal: Open links with Cmd/Ctrl-click when mouse mode is enabled (#60067) 2026-07-08 12:41:48 +00:00
themes.md docs: Update actions format (#54869) 2026-05-07 06:55:11 +00:00
toolchains.md docs: Apply documentation standards across all docs (#49177) 2026-02-17 20:58:17 -06:00
troubleshooting.md feedback: Add action for listing installed extensions (#48664) 2026-04-16 07:45:38 +00:00
uninstall.md docs: Update guide to remove user data on macOS when uninstalling Zed (#52631) 2026-03-31 05:35:04 +00:00
update.md docs: Update actions format (#54869) 2026-05-07 06:55:11 +00:00
vim.md Fix typos and grammatical mistakes in docs (#59495) 2026-07-08 12:32:00 +00:00
visual-customization.md title_bar: Add show_worktree_name setting (#61137) 2026-07-21 06:04:06 +00:00
windows-and-projects.md Fixes for AI docs (#58969) 2026-06-11 15:15:55 +00:00
windows.md docs: Apply documentation standards across all docs (#49177) 2026-02-17 20:58:17 -06:00
worktree-trust.md Fix typos and grammatical mistakes in docs (#59495) 2026-07-08 12:32:00 +00:00