主动上下文压缩 — 由模型驱动的 OpenCode 上下文管理,20w token 就足够了。
Active Context Pruning for OpenCode
The model decides when and what to compress — not a hard limit.
200K tokens is enough.
The paper itself is open-sourced under the MIT License as part of the codebase (
paper/). It is a living document — anyone may edit it; improvements are welcome via pull request.
A production-scale longitudinal study: 4.5 months, three hosts, 174,327 model calls, 18.76B cumulative input tokens (~24.7B across all hosts), zero window violations on 204,800-token models, marathon sessions of 8,584–12,049 calls.
opencode plugin opencode-acp@stable --global
Discussion, help, and updates on QQ — one group covers all three projects (billion-context, billion-context-pi, opencode-acp):
QQ Group: 1056132097
ACP hands all context-management authority to the model itself — not relying on external models or any complex external mechanism to do context management. It is, to date, the best context-management implementation on the market.
This brings two concrete effects:
Pick by your client:
| Client | Use |
|---|---|
| pi | billion-context-pi (in-process extension) |
| opencode | opencode-acp (in-process extension) |
| omp | billion-context via bili omp (built-in plugin) |
| everything else (no context hook) | billion-context — bili (launcher, preferred) or /bili/ prefix |
Real engineering context, in practice.
Across 6 active engineering sessions (11,000+ API calls), context p90 stays at 150K–190K (15–19%), p95 at 160K–210K (16–21%) of the 1M window — with an aggregate prompt-cache hit ratio of 91%. (Why aggregate — not per-session — matters is explained in Impact on Prompt Caching, where it turns out to save far more tokens than traditional compression.)
| Session | Duration | Messages | API calls | Cumulative | Cache hit | Context p50 | Context p90 | Context p95 |
|---|---|---|---|---|---|---|---|---|
| 0b89319b | 230h (9.5d) | 3,344 | 2,796 | 339M | 93% | 108K (11%) | 167K (17%) | 210K (21%) |
| 0a3be0cd | 130h (5.4d) | 3,183 | 2,499 | 276M | 91% | 104K (10%) | 145K (15%) | 153K (15%) |
| 0b2cd5a7 | 131h (5.4d) | 2,560 | 2,181 | 314M | 91% | 142K (14%) | 191K (19%) | 197K (20%) |
| 08f2d501 | 37h (1.5d) | 1,985 | 1,888 | 196M | 95% | 100K (10%) | 156K (16%) | 168K (17%) |
| 1410c791† | 865h (36d) | 1,279 | 1,100 | 218M | 87% | 132K (13%) | 407K (41%) | 427K (43%) |
| 096cf8c4 | 72h (3d) | 1,041 | 918 | 91M | 89% | 92K (9%) | 148K (15%) | 161K (16%) |
† Bug-testing session; p95 is abnormally high. Excluding it, p95 stays ≤ 210K across all other sessions.
(Context percentages are of the 1M window.)
opencode plugin opencode-acp@stable --globalOr add to your opencode config:
{
"plugin": {
"opencode-acp": "stable"
}
}ACP hands the context-compression tool directly to the model. The model is 100% responsible for context compression. The model's primary tools are compress and decompress, supported by acp_status (context monitoring) and search_context (search compressed content). Compression uses a three-tier LSM-tree architecture (T1 capture → T2 distill → T3 condense) that keeps context bounded for years. A hardcoded 100% GC fallback acts as a safety net when the context window is completely full.
ACP uses a three-tier LSM-tree compression architecture, inspired by database storage engines. Each tier compresses the previous tier's output, creating progressively denser summaries with natural frequency decrease:
stateDiagram-v2
Raw --> Tier1 : compress (every ~7 turns)
Tier1 --> Tier2 : distill (every ~250 turns)
Tier2 --> Tier3 : condense (every ~2500 turns)
Tier1 --> Raw : decompress
Tier2 --> Raw : decompress (recursive)
Tier3 --> Raw : decompress (recursive)
Tier1 --> GC_Truncated : GC at 100% context| Tier | Name | Input | Output | Compression ratio | When it fires |
|---|---|---|---|---|---|
| T1 | Capture | Raw conversation | Detailed summary | ~45× | Context exceeds maxContextLimit |
| T2 | Distill | T1 summaries (≥ nudgeGrowthTokens) |
Condensed decisions/outcomes | ~10× | T1 summaries accumulate past threshold |
| T3 | Condense | T2 summaries (≥ nudgeGrowthTokens) |
Bare facts (1-3 per block) | ~5× | T2 summaries accumulate past threshold |
How triggers work:
nudgeGrowthTokens (fixed default
50000). The model distills old T1 blocks — keeping decisions and
outcomes, dropping verbose process details.Each tier has an independent cadence counter — T2 firing doesn't block T3.
T1 has priority via a !shouldInject guard: if T1 fires, T2/T3 wait until next
turn. This ensures raw context compression happens first (it has the biggest
impact).
Session capacity — total tokens a single session can process from empty → T1 → T2 → T3 → context limit (real-calibrated: 500 API calls/day, ~9.6K new tokens/call, T1=45x/T2=10x/T3=3x):
| Context limit | 1 month | 3 months | At limit | Limit reached |
|---|---|---|---|---|
| 1M | 1.9B tok | 10.5B tok | 68.9B tok | day 259 (~8.6 mo) |
| 400K | 1.9B tok | 10.3B tok | 10.3B tok | day 89 (~3 mo) |
| 400K (200 calls/day) | 559M tok | 2.5B tok | 9.5B tok | day 212 (~7 mo) |
Token savings — without ACP, context grows unbounded and the session crashes after ~100 API calls (~0.2 days). With ACP, context is bounded by compression:
| Metric | Without ACP | With ACP (1M model) |
|---|---|---|
| Session lifetime | ~0.2 days | 259 days (1295x longer) |
| Total tokens processed | ~52M | 68.9B (1325x more work) |
The core value: ACP doesn't just reduce per-call token cost — it enables a single session to process 1000x more total work by keeping context bounded across the full session lifetime.
The model uses the same compress tool for all tiers. T2/T3 compressions
use block IDs as boundaries (compress({ content: [{ startId: "b5", endId: "b20", summary: "..." }] })). Tier is auto-detected from consumed blocks.
The system injects a prompt telling the model the current context ratio, the compression ratio, whether context is idle, and compression suggestions. When the trigger ratio is hit, content is compressed in priority order:
After compression, the original content is replaced by a short block that
references the original (recoverable via decompress).
The model decides when to decompress. When the context is large enough to interfere with the model's self-attention, short blocks lead the model to compress some content first, handle the urgent matter, then decompress what it needs in later work.
When context reaches 100%, the system automatically truncates old-gen block summaries to prevent overflow. This is a last-resort safety net — with three-tier compression, the GC rarely activates because T2/T3 distillation keeps summary overhead bounded.
After each compress call, ACP can run a pluggable quality gate to detect summaries that catastrophically lost content (e.g., a 5K-token range compressed to a 147-char summary with none of the technical keywords). Failures only emit logger.warn — they never reject the compression (the result is already committed to state and visible to the model).
The default algorithm (rouge-recall-v1) is a two-layer gate calibrated against 6,913 real-world blocks:
[!IMPORTANT] Disable OpenCode's built-in auto-compaction. ACP handles context management itself — OpenCode's compaction conflicts with ACP and can cause issues (re-expanded messages, lost compression state). Add to your
opencode.json:{ "compaction": { "auto": false, }, }Or set the environment variable:
OPENCODE_DISABLE_AUTOCOMPACT=1
[!NOTE] If you use models with smaller context windows, such as GitHub Copilot models or local models, lower
compress.minContextLimitandcompress.maxContextLimitin your configuration to match the available context.
Default Configuration (click to expand)
…jsonc { "compress": { "maxContextLimit": "80%", "providers": { "anthropic": { "nudgeGrowthTokens": 20000, "models": { "claude-sonnet-4.6": { "maxContextLimit": "70%", "nudgeForce": "strong" } } }, "openai": { "nudgeGrowthTokens": 40000 } } } }
Resolution is **per field**: model > provider > global. Unknown provider/model IDs fall back to the global value. A nested `maxContextLimit` also wins over the legacy flat `modelMaxLimits` map. Overrides deep-merge across the three config layers (global → config dir → project) per provider/model key.
Not overridable here: `permission`, the deprecated `minContextLimit` / `modelMinLimits` family, and the flat `modelMaxLimits` / `modelMinLimits` maps themselves. Note: `modelMaxLimits` is **not** deprecated — it stays fully supported; a nested `maxContextLimit` simply outranks it.
See the [`compres暂无开放 Issues,或尚未同步最近议题。