Repository navigation
Feature Request: Configurable Context Compaction Threshold #11314
Description
OpenCode Feature Request: Configurable Context Compaction Threshold
Submitted by: @roberto_schmidt
Date: 2026-01-30
Priority: High
Category: Core Functionality / Configuration
Summary
Currently, OpenCode triggers context compaction at a hardcoded 75% threshold of a model's context window. This causes severe performance degradation for long-context models (Gemini, Claude, GPT-4) before compaction occurs, as these models begin losing coherence well before the 75% mark.
Request: Expose the compaction threshold as a user-configurable setting in opencode.json.
Problem Statement
Current Behavior
- Compaction triggers at exactly 75% of the model's advertised context window
- This is hardcoded in the OpenCode binary and cannot be changed
- Example: Gemini with 1M context triggers compaction at 786k tokens
Impact
- Gemini models: Performance degradation starts at ~30% (300k tokens), but compaction doesn't trigger until 75% (786k)
- Claude models: Quality drops significantly after ~50% of context
- Result: Users experience 2-3x slower responses, hallucinations, and poor code quality before automatic compaction kicks in
Proposed Solution
Add a compaction configuration section to opencode.json:
{
"$schema": "https://opencode.ai/config.json",
"compaction": {
"threshold": 0.40,
"strategy": "summarize",
"preserveRecentMessages": 10,
"preserveSystemPrompt": true
},
"provider": {
...
}
}Configuration Options
| Option | Type | Default | Description |
|---|---|---|---|
threshold |
number (0.0-1.0) | 0.75 | Percentage of context window at which to trigger compaction |
strategy |
string | "summarize" | Compaction strategy: "summarize", "truncate", "archive" |
preserveRecentMessages |
number | 10 | Number of recent messages to always preserve |
preserveSystemPrompt |
boolean | true | Always preserve the system prompt |
Per-Model Overrides
Allow model-specific thresholds:
{
"provider": {
"google": {
"models": {
"gemini-1.5-pro": {
"limit": { "context": 1048576 },
"compaction": {
"threshold": 0.30
}
}
}
}
}
}Use Cases
- VRP Optimization Work (CuraOps): Need compaction at 30% for Gemini to maintain constraint tracking accuracy
- Long Document Analysis: Users working with 500k+ token documents need early compaction to preserve coherence
- Multi-Session Coding: 8+ hour coding sessions accumulate context that degrades model performance
Alternative Solutions Considered
- Manual compaction: Users can trigger
session.compactmanually, but this interrupts workflow and requires constant monitoring - Smaller context windows: Setting
limit.contextto lower values doesn't work - OpenCode uses the model's ACTUAL window, not the config value - Different models: Switching to shorter-context models sacrifices capability
Implementation Notes
The compaction logic already exists in the OpenCode binary. This feature request is about exposing the hardcoded 0.75 value as a configuration parameter.
Relevant SDK types found:
compaction?: {
auto?: boolean; // Already exists
prune?: boolean; // Already exists
threshold?: number; // REQUESTED: 0.0-1.0
}Benefits
- Improved productivity: Models stay in high-performance zone longer
- Better code quality: Less context degradation = fewer bugs
- User control: Power users can tune for their specific workflows
- Backward compatible: Default remains 0.75, no breaking changes
Related Issues
- Context window management for long-running sessions
- Performance degradation in multi-step workflows
- Memory/Neo4j integration for persistent context
Contact: roberto.schmidt@curaops.de
Willing to test: Yes, happy to validate beta implementations
Activity
github-actions commentedon Jan 30, 2026
This issue might be a duplicate of existing issues. Please check:
The core request aligns with #11086 regarding early compaction triggering at hardcoded thresholds. Additionally, #11287 addresses the broader feature of model-level configuration overrides that could support per-model compaction thresholds.
Feel free to ignore if your specific compaction threshold configuration needs differ from these issues.
jeremyakers commentedon Jan 30, 2026
The "auto-archive" mode is exactly what I want/need. Being able to archive old (timestamped) messages into a location on disk where the agent can still access them is best of both worlds: If the agent ever needs to "refer back" to something that exceeds it's context: It can just do a surgical ripgrep / grepai / aigrep search using a combination of timestamp filter plus contextual search of that archive to reference that archived information.
iyunbo commentedon Jan 31, 2026
➕
The best practices proposed by Anthropic is mostly about "context window management":
The same principles apply to most of LLM coding tools including opencode, therefore, this feature is a high priority IMO. 🙏
iyunbo commentedon Jan 31, 2026
#10123 seems the PR to address it.
jeremyakers commentedon Jan 31, 2026
That PR doesn't seem to add auto archive. But I created a plugin that addresses that use case:
https://github.com/jeremyakers/opencode-plugins/tree/main/packages/opencode-archive-before-compaction
I also published to npm so you can add it with:
opencode.json:WietRob commentedon Feb 2, 2026
Thanks for the reference to related issues!
I'd like to clarify how this request differs:
This issue is specifically about:
compaction.threshold: 0.30per-modelThe model-level config (#11287) could support this, but we need to expose the specific
thresholdparameter first.WietRob commentedon Feb 2, 2026
@jeremyakers Thanks for the validation! The 'auto-archive' mode with timestamp-based archiving and searchable context is exactly the direction we need.
Could the compaction threshold be exposed as a model-level configuration option alongside the auto-archive feature? That would give users both:
This seems complementary to your 'best of both worlds' approach.
WietRob commentedon Feb 3, 2026
@saksham1341 Wow, thank you for the quick implementation! 🙌
I'd be happy to test this out. Could you provide guidance on:
Looking forward to testing this! This will be huge for CuraOps VRP optimization with long-context models.
cc: @jeremyakers (FYI - this feature is now implemented!)
WietRob commentedon Feb 3, 2026
@saksham1341 Thanks for the detailed example! I've updated my configuration:
{ "compaction": { "token_threshold": 300000, "context_threshold": 0.30, "models": { "google/antigravity-gemini-3-flash": { "token_threshold": 300000, "context_threshold": 0.30 }, "google/antigravity-gemini-3-pro-high": { "token_threshold": 300000, "context_threshold": 0.30 }, "google/antigravity-gemini-3-pro-low": { "token_threshold": 300000, "context_threshold": 0.30 } } } }I'm ready to test. How should I verify it's working?
Looking forward to testing this!
WietRob commentedon Feb 3, 2026
@saksham1341 Also added GLM-4.7 to my config:
This compacts at 138k tokens (~50% of the model's likely context window of 128k+). Perfect for coding sessions with GLM.
feat(compaction): add configurable threshold and model (global + per-…
didvc commentedon Mar 4, 2026
+1
GianniDPC commentedon Mar 21, 2026
+1
filmo commentedon Mar 21, 2026
+1 for per-model configurations.
lsls01 commentedon Apr 18, 2026
+1 for per-model configurations.
github-actions commentedon Jun 26, 2026
To stay organized issues are automatically closed after 60 days of no activity. If the issue is still relevant please open a new one.