Feature Request: Custom compaction threshold to trigger earlier #7195

Open
opened 2026-02-16 18:06:26 -05:00 by yindo · 3 comments
Owner

Originally created by @openAgi2 on GitHub (Jan 22, 2026).

Originally assigned to: @thdxr on GitHub.

Problem

Currently, auto-compaction only triggers when token count exceeds the model's context limit (count > usable in compaction.ts):

export async function isOverflow(input: { tokens; model }) {
  // ...
  const count = input.tokens.input + input.tokens.cache.read + input.tokens.output
  const usable = input.model.limit.input || context - output
  return count > usable  // Only triggers at the limit
}

For models with large context windows (e.g., Claude Opus/Sonnet with 200K), this means:

  • Users must accumulate ~200K tokens before auto-compaction kicks in
  • By that point, API responses are already extremely slow (10+ minutes of waiting)
  • The "auto" compaction effectively becomes useless for preventing slowdowns

Proposed Solution

Add a configurable threshold option to trigger compaction earlier:

{
  "compaction": {
    "auto": true,
    "prune": true,
    "threshold": 50000  // Trigger compaction when input tokens exceed this value
  }
}

Implementation would be straightforward - modify isOverflow() to check against the threshold:

export async function isOverflow(input: { tokens; model }) {
  const config = await Config.get()
  if (config.compaction?.auto === false) return false
  
  const count = input.tokens.input + input.tokens.cache.read + input.tokens.output
  
  // Check custom threshold first
  if (config.compaction?.threshold && count > config.compaction.threshold) {
    return true
  }
  
  // Fall back to model limit check
  const context = input.model.limit.context
  if (context === 0) return false
  const output = Math.min(input.model.limit.output, SessionPrompt.OUTPUT_TOKEN_MAX) || SessionPrompt.OUTPUT_TOKEN_MAX
  const usable = input.model.limit.input || context - output
  return count > usable
}

Use Case

After 20-30 conversation turns with many tool calls, sessions become painfully slow. Users currently must:

  1. Notice the slowdown (often too late)
  2. Manually run /compact

With a configurable threshold, users could set a reasonable limit (e.g., 50K-80K tokens) to maintain responsive sessions automatically.

Alternatives Considered

  • Manual /compact: Works but requires user vigilance; easy to forget until it's too late
  • Lower the percentage of context used: Less flexible than an absolute threshold

Additional Context

Related code: packages/opencode/src/session/compaction.ts

Originally created by @openAgi2 on GitHub (Jan 22, 2026). Originally assigned to: @thdxr on GitHub. ## Problem Currently, auto-compaction only triggers when token count exceeds the model's context limit (`count > usable` in `compaction.ts`): ```typescript export async function isOverflow(input: { tokens; model }) { // ... const count = input.tokens.input + input.tokens.cache.read + input.tokens.output const usable = input.model.limit.input || context - output return count > usable // Only triggers at the limit } ``` For models with large context windows (e.g., Claude Opus/Sonnet with 200K), this means: - Users must accumulate ~200K tokens before auto-compaction kicks in - By that point, API responses are already extremely slow (10+ minutes of waiting) - The "auto" compaction effectively becomes useless for preventing slowdowns ## Proposed Solution Add a configurable `threshold` option to trigger compaction earlier: ```jsonc { "compaction": { "auto": true, "prune": true, "threshold": 50000 // Trigger compaction when input tokens exceed this value } } ``` Implementation would be straightforward - modify `isOverflow()` to check against the threshold: ```typescript export async function isOverflow(input: { tokens; model }) { const config = await Config.get() if (config.compaction?.auto === false) return false const count = input.tokens.input + input.tokens.cache.read + input.tokens.output // Check custom threshold first if (config.compaction?.threshold && count > config.compaction.threshold) { return true } // Fall back to model limit check const context = input.model.limit.context if (context === 0) return false const output = Math.min(input.model.limit.output, SessionPrompt.OUTPUT_TOKEN_MAX) || SessionPrompt.OUTPUT_TOKEN_MAX const usable = input.model.limit.input || context - output return count > usable } ``` ## Use Case After 20-30 conversation turns with many tool calls, sessions become painfully slow. Users currently must: 1. Notice the slowdown (often too late) 2. Manually run `/compact` With a configurable threshold, users could set a reasonable limit (e.g., 50K-80K tokens) to maintain responsive sessions automatically. ## Alternatives Considered - **Manual `/compact`**: Works but requires user vigilance; easy to forget until it's too late - **Lower the percentage of context used**: Less flexible than an absolute threshold ## Additional Context Related code: `packages/opencode/src/session/compaction.ts`
Author
Owner

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

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

  • #9896: "Compact above" - Partial Compaction - Allows manual selection of where to compact in the conversation
  • #9889: expose current context window usage in agent state - Enables monitoring and automation of compaction based on context threshold
  • #9427: Auto-route to RLM when context overflows - Alternative approach using RLM for context overflow management

Feel free to ignore if none of these address your specific case.

@github-actions[bot] commented on GitHub (Jan 22, 2026): This issue might be a duplicate of existing issues. Please check: - #9896: "Compact above" - Partial Compaction - Allows manual selection of where to compact in the conversation - #9889: expose current context window usage in agent state - Enables monitoring and automation of compaction based on context threshold - #9427: Auto-route to RLM when context overflows - Alternative approach using RLM for context overflow management Feel free to ignore if none of these address your specific case.
Author
Owner

@saksham1341 commented on GitHub (Jan 22, 2026):

Would it be better to compact based on context percentage threshold instead of token count threshold? Or could provide an option?

For example:

{
  "compaction": {
    "auto": true,
    "prune": true,
    "token_threshold": 50000  // Trigger compaction when input tokens exceed this value
    "context_threshold": 0.75  // Trigger compaction when context exceeds 75%
  }
}

Could auto-compact whenever either of the conditions is met.

@saksham1341 commented on GitHub (Jan 22, 2026): Would it be better to compact based on context percentage threshold instead of token count threshold? Or could provide an option? For example: ``` { "compaction": { "auto": true, "prune": true, "token_threshold": 50000 // Trigger compaction when input tokens exceed this value "context_threshold": 0.75 // Trigger compaction when context exceeds 75% } } ``` Could auto-compact whenever either of the conditions is met.
Author
Owner

@FN-FAL113 commented on GitHub (Feb 16, 2026):

This is gonna be a great addition, Not everyone has the best rig out there for local models nor even setting a context window of 200k just to use the auto compaction feature.

@FN-FAL113 commented on GitHub (Feb 16, 2026): This is gonna be a great addition, Not everyone has the best rig out there for local models nor even setting a context window of 200k just to use the auto compaction feature.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#7195