Feature Request: Configurable Context Compaction Threshold #8050

Open
opened 2026-02-16 18:09:01 -05:00 by yindo · 10 comments
Owner

Originally created by @WietRob on GitHub (Jan 30, 2026).

Originally assigned to: @thdxr on GitHub.

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

  1. VRP Optimization Work (CuraOps): Need compaction at 30% for Gemini to maintain constraint tracking accuracy
  2. Long Document Analysis: Users working with 500k+ token documents need early compaction to preserve coherence
  3. Multi-Session Coding: 8+ hour coding sessions accumulate context that degrades model performance

Alternative Solutions Considered

  1. Manual compaction: Users can trigger session.compact manually, but this interrupts workflow and requires constant monitoring
  2. Smaller context windows: Setting limit.context to lower values doesn't work - OpenCode uses the model's ACTUAL window, not the config value
  3. 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

  1. Improved productivity: Models stay in high-performance zone longer
  2. Better code quality: Less context degradation = fewer bugs
  3. User control: Power users can tune for their specific workflows
  4. 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

Originally created by @WietRob on GitHub (Jan 30, 2026). Originally assigned to: @thdxr on GitHub. # 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`: ```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: ```json { "provider": { "google": { "models": { "gemini-1.5-pro": { "limit": { "context": 1048576 }, "compaction": { "threshold": 0.30 } } } } } } ``` --- ## Use Cases 1. **VRP Optimization Work** (CuraOps): Need compaction at 30% for Gemini to maintain constraint tracking accuracy 2. **Long Document Analysis**: Users working with 500k+ token documents need early compaction to preserve coherence 3. **Multi-Session Coding**: 8+ hour coding sessions accumulate context that degrades model performance --- ## Alternative Solutions Considered 1. **Manual compaction**: Users can trigger `session.compact` manually, but this interrupts workflow and requires constant monitoring 2. **Smaller context windows**: Setting `limit.context` to lower values doesn't work - OpenCode uses the model's ACTUAL window, not the config value 3. **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:** ```typescript compaction?: { auto?: boolean; // Already exists prune?: boolean; // Already exists threshold?: number; // REQUESTED: 0.0-1.0 } ``` --- ## Benefits 1. **Improved productivity**: Models stay in high-performance zone longer 2. **Better code quality**: Less context degradation = fewer bugs 3. **User control**: Power users can tune for their specific workflows 4. **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
Author
Owner

@github-actions[bot] commented on GitHub (Jan 30, 2026):

This issue might be a duplicate of existing issues. Please check:

  • #11086: GPT 5.2 Codex - Compaction happens way too early and context limit is wrong
  • #11287: [FEATURE]: Support Model-Level Configuration override Provider-Level, such as baseURL and apiKey
  • #11138: [FEATURE]: Configurable per-session token budget limit (closed)

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.

@github-actions[bot] commented on GitHub (Jan 30, 2026): This issue might be a duplicate of existing issues. Please check: - #11086: GPT 5.2 Codex - Compaction happens way too early and context limit is wrong - #11287: [FEATURE]: Support Model-Level Configuration override Provider-Level, such as baseURL and apiKey - #11138: [FEATURE]: Configurable per-session token budget limit (closed) 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.
Author
Owner

@jeremyakers commented on GitHub (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.

@jeremyakers commented on GitHub (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.
Author
Owner

@iyunbo commented on GitHub (Jan 31, 2026):


The best practices proposed by Anthropic is mostly about "context window management":

Most best practices are based on one constraint: Claude’s context window fills up fast, and performance degrades as it fills.

The same principles apply to most of LLM coding tools including opencode, therefore, this feature is a high priority IMO. 🙏

@iyunbo commented on GitHub (Jan 31, 2026): ➕ The best [practices proposed](https://code.claude.com/docs/en/best-practices) by Anthropic is mostly about "context window management": > Most best practices are based on one constraint: Claude’s context window fills up fast, and performance degrades as it fills. The same principles apply to most of LLM coding tools including opencode, therefore, this feature is a **high priority** IMO. 🙏
Author
Owner

@iyunbo commented on GitHub (Jan 31, 2026):

https://github.com/anomalyco/opencode/pull/10123 seems the PR to address it.

@iyunbo commented on GitHub (Jan 31, 2026): https://github.com/anomalyco/opencode/pull/10123 seems the PR to address it.
Author
Owner

@jeremyakers commented on GitHub (Jan 31, 2026):

#10123 seems the PR to address it.

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:

{
  "plugin": ["opencode-archive-before-compaction"]
}

@jeremyakers commented on GitHub (Jan 31, 2026): > [#10123](https://github.com/anomalyco/opencode/pull/10123) seems the PR to address it. 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:` ``` { "plugin": ["opencode-archive-before-compaction"] } ```
Author
Owner

@WietRob commented on GitHub (Feb 2, 2026):

Thanks for the reference to related issues!

I'd like to clarify how this request differs:

  • #11086 is about compaction happening too early
  • #11287 is about model-level config overrides (general)
  • #11138 is about session token budgets (global limits)

This issue is specifically about:

  • Per-model compaction threshold configuration
  • Problem: Models like Gemini lose quality at ~30% context (300k tokens)
  • Current hardcoded 75% threshold means models work in degraded state for ~600k tokens
  • Need ability to set compaction.threshold: 0.30 per-model

The model-level config (#11287) could support this, but we need to expose the specific threshold parameter first.

@WietRob commented on GitHub (Feb 2, 2026): Thanks for the reference to related issues! I'd like to clarify how this request differs: - **#11086** is about compaction happening *too early* - **#11287** is about model-level config overrides (general) - **#11138** is about session token budgets (global limits) **This issue is specifically about:** - **Per-model compaction threshold configuration** - Problem: Models like Gemini lose quality at ~30% context (300k tokens) - Current hardcoded 75% threshold means models work in degraded state for ~600k tokens - Need ability to set `compaction.threshold: 0.30` per-model The model-level config (#11287) could support this, but we need to expose the specific `threshold` parameter first.
Author
Owner

@WietRob commented on GitHub (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:

  1. Fine-grained control over when compaction triggers (per-model thresholds)
  2. Persistent access to archived context for surgical retrieval

This seems complementary to your 'best of both worlds' approach.

@WietRob commented on GitHub (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: 1. Fine-grained control over when compaction triggers (per-model thresholds) 2. Persistent access to archived context for surgical retrieval This seems complementary to your 'best of both worlds' approach.
Author
Owner

@WietRob commented on GitHub (Feb 3, 2026):

@saksham1341 Wow, thank you for the quick implementation! 🙌

I'd be happy to test this out. Could you provide guidance on:

  1. How to update my configuration - what's the new JSON schema for per-model thresholds?
  2. How to test - should I restart OpenCode? Any commands to verify it's working?
  3. What to test specifically - I'll verify it compacts at the new threshold vs the old 75%

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 commented on GitHub (Feb 3, 2026): @saksham1341 Wow, thank you for the quick implementation! 🙌 I'd be happy to test this out. Could you provide guidance on: 1. **How to update my configuration** - what's the new JSON schema for per-model thresholds? 2. **How to test** - should I restart OpenCode? Any commands to verify it's working? 3. **What to test specifically** - I'll verify it compacts at the new threshold vs the old 75% 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!)
Author
Owner

@WietRob commented on GitHub (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?

  • Just monitor when compaction happens?
  • Use a session.log or debug flag?
  • Should I restart OpenCode after changing config?

Looking forward to testing this!

@WietRob commented on GitHub (Feb 3, 2026): @saksham1341 Thanks for the detailed example! I've updated my configuration: ```json { "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? - Just monitor when compaction happens? - Use a session.log or debug flag? - Should I restart OpenCode after changing config? Looking forward to testing this!
Author
Owner

@WietRob commented on GitHub (Feb 3, 2026):

@saksham1341 Also added GLM-4.7 to my config:

"opencode/glm-4.7-free": {
  "token_threshold": 138000,
  "context_threshold": 0.50
}

This compacts at 138k tokens (~50% of the model's likely context window of 128k+). Perfect for coding sessions with GLM.

@WietRob commented on GitHub (Feb 3, 2026): @saksham1341 Also added GLM-4.7 to my config: ```json "opencode/glm-4.7-free": { "token_threshold": 138000, "context_threshold": 0.50 } ``` This compacts at 138k tokens (~50% of the model's likely context window of 128k+). Perfect for coding sessions with GLM.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#8050