How does opencode's auto-compact actually work — trigger threshold and config surface? #27

Closed
opened 2026-09-03 04:19:33 +00:00 by haylan · 1 comment
Owner

Part of #26

Question

opencode has an "auto-compact" behavior (summarizing/truncating older
messages when context fills). What, precisely, does it use to decide when
to trigger — a hardcoded percentage of `limit.context`? A separate config
key? Does it exist at all for the `@ai-sdk/openai-compatible` provider
path (the one this repo's `opencode-cli-setup.md` documents for
llama.cpp/litellm-style backends), or only for opencode's built-in/hosted
providers?

Specifically resolve:

  • The config key(s), if any, that control compaction threshold/behavior
    (global `opencode.json`, per-provider, or per-model under `models.`).
  • Whether compaction accounts only for visible message tokens, or also for
    a reasoning/thinking budget (relevant here — Qwen3.8 spends
    `reasoning_content` tokens before `content`, see `litellm-config.yaml`'s
    `max_tokens` comment on this repo's `qwen3.8-27b-local`).
  • Whether compaction can be disabled/tuned per-model, or is a single global
    behavior.
  • Freshness check: opencode's docs move fast (this repo's own
    `docs/research/opencode-cli-setup.md` is dated 2026-08-24) — confirm
    current docs/changelog, don't just trust that file.

Primary sources: https://opencode.ai/docs/ (compaction/context-management
section, wherever it currently lives — may be under a "context" or
"sessions" page, not obviously named), the opencode GitHub repo
(`anomalyco/opencode`) issues/source if docs are silent.

Part of #26 ## Question opencode has an "auto-compact" behavior (summarizing/truncating older messages when context fills). What, precisely, does it use to decide when to trigger — a hardcoded percentage of \`limit.context\`? A separate config key? Does it exist at all for the \`@ai-sdk/openai-compatible\` provider path (the one this repo's \`opencode-cli-setup.md\` documents for llama.cpp/litellm-style backends), or only for opencode's built-in/hosted providers? Specifically resolve: - The config key(s), if any, that control compaction threshold/behavior (global \`opencode.json\`, per-provider, or per-model under \`models.<id>\`). - Whether compaction accounts only for visible message tokens, or also for a reasoning/thinking budget (relevant here — Qwen3.8 spends \`reasoning_content\` tokens before \`content\`, see \`litellm-config.yaml\`'s \`max_tokens\` comment on this repo's \`qwen3.8-27b-local\`). - Whether compaction can be disabled/tuned per-model, or is a single global behavior. - Freshness check: opencode's docs move fast (this repo's own \`docs/research/opencode-cli-setup.md\` is dated 2026-08-24) — confirm current docs/changelog, don't just trust that file. Primary sources: https://opencode.ai/docs/ (compaction/context-management section, wherever it currently lives — may be under a "context" or "sessions" page, not obviously named), the opencode GitHub repo (\`anomalyco/opencode\`) issues/source if docs are silent.
haylan added the wayfinder:research label 2026-09-03 04:19:33 +00:00
Author
Owner

Researched OpenCode's auto-compact mechanism directly from source (anomalyco/opencode @ b578b72, v1.18.27, cloned 2026-09-03) plus live docs.opencode.ai fetches.

Key findings — full writeup at docs/research/opencode-auto-compact.md on branch research/opencode-auto-compact:

  • Config surface: single global top-level compaction key in opencode.json (auto, prune, reserved, tail_turns, preserve_recent_tokens). No threshold/percentage key exists, and it is NOT nested per-provider or per-model.
  • Trigger is NOT a hardcoded percentage: usedTokens >= context - reservedBuffer (reservedBuffer defaults to min(20000, maxOutputTokens)). A closed GitHub feature request claims 'hardcoded 75%' — that does not match current source and conflicts with another issue claiming '100%'; treat both as unverified user claims, not fact.
  • Reasoning tokens (relevant to qwen3.8-27b-local's reasoning_content behavior) ARE counted toward the trigger in the normal path via the provider's usage.total_tokens, but would be undercounted if a provider ever omitted total_tokens (the fallback sum excludes the reasoning field explicitly).
  • No per-model/per-agent override exists; multiple feature requests for this are closed as not-planned. The only per-model lever is each model's own limit.context/limit.output.
  • The mechanism is fully provider-agnostic — applies identically to a hand-declared @ai-sdk/openai-compatible provider (this repo's llamacpp setup) as to hosted providers, provided limit.context is set to a nonzero value (if left unset it silently disables compaction for that model, per opencode-cli-setup.md's provider block).

Confidence levels and full citations are in the doc.

Researched OpenCode's auto-compact mechanism directly from source (anomalyco/opencode @ b578b72, v1.18.27, cloned 2026-09-03) plus live docs.opencode.ai fetches. Key findings — full writeup at docs/research/opencode-auto-compact.md on branch research/opencode-auto-compact: - Config surface: single global top-level compaction key in opencode.json (auto, prune, reserved, tail_turns, preserve_recent_tokens). No threshold/percentage key exists, and it is NOT nested per-provider or per-model. - Trigger is NOT a hardcoded percentage: usedTokens >= context - reservedBuffer (reservedBuffer defaults to min(20000, maxOutputTokens)). A closed GitHub feature request claims 'hardcoded 75%' — that does not match current source and conflicts with another issue claiming '100%'; treat both as unverified user claims, not fact. - Reasoning tokens (relevant to qwen3.8-27b-local's reasoning_content behavior) ARE counted toward the trigger in the normal path via the provider's usage.total_tokens, but would be undercounted if a provider ever omitted total_tokens (the fallback sum excludes the reasoning field explicitly). - No per-model/per-agent override exists; multiple feature requests for this are closed as not-planned. The only per-model lever is each model's own limit.context/limit.output. - The mechanism is fully provider-agnostic — applies identically to a hand-declared @ai-sdk/openai-compatible provider (this repo's llamacpp setup) as to hosted providers, provided limit.context is set to a nonzero value (if left unset it silently disables compaction for that model, per opencode-cli-setup.md's provider block). Confidence levels and full citations are in the doc.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Reference: haylan/LLM-Server#27