Skip to content

Feature Request: Configurable Context Compaction Threshold #11314

Closed as not planned

Description

@WietRob

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

Activity

  1. github-actions commented on Jan 30, 2026

    @github-actions
    Contributor

    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.

  2. jeremyakers commented on Jan 30, 2026

    @jeremyakers

    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.

  3. iyunbo commented on Jan 31, 2026

    @iyunbo

    ➕
    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. 🙏

  4. iyunbo commented on Jan 31, 2026

    @iyunbo

    #10123 seems the PR to address it.

  5. jeremyakers commented on Jan 31, 2026

    @jeremyakers

    #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"]
    }
    
    
  6. WietRob commented on Feb 2, 2026

    @WietRob
    Author

    Thanks for the reference to related issues!

    I'd like to clarify how this request differs:

    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.

  7. WietRob commented on Feb 2, 2026

    @WietRob
    Author

    @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.

  8. WietRob commented on Feb 3, 2026

    @WietRob
    Author

    @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!)

  9. WietRob commented on Feb 3, 2026

    @WietRob
    Author

    @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!

  10. WietRob commented on Feb 3, 2026

    @WietRob
    Author

    @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.

  11. didvc commented on Mar 4, 2026

    @didvc

    +1

  12. GianniDPC commented on Mar 21, 2026

    @GianniDPC

    +1

  13. filmo commented on Mar 21, 2026

    @filmo

    +1 for per-model configurations.

  14. lsls01 commented on Apr 18, 2026

    @lsls01

    +1 for per-model configurations.

  15. assigned and unassigned on Apr 26, 2026
  16. github-actions commented on Jun 26, 2026

    @github-actions
    Contributor

    To stay organized issues are automatically closed after 60 days of no activity. If the issue is still relevant please open a new one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions