Skip to content

Feature: Automatic context compaction for long conversations #15298

Description

@BrunoCerberus

Problem

When using OpenCode with local models (MLX, Ollama, LM Studio), each new turn sends the entire conversation history as the prompt. This causes progressively slower responses as context grows:

  • Turn 1: Small prompt → fast prefill (~3s)
  • Turn 5: Accumulated context → slow prefill (~30s+)
  • Turn 10+: Massive prompt → unusable latency (minutes)

This is especially painful with local models where prompt processing is the bottleneck. Cloud APIs handle this better due to server-side caching, but local inference has to reprocess the full context every time.

Proposed Solution

Implement automatic context compaction — similar to how Claude Code handles it. When the conversation context approaches a configurable threshold:

  1. Summarize the conversation history (tool call results, code snippets, prior reasoning) into a compact representation
  2. Replace the full history with the summary + recent turns
  3. Continue the conversation with a much smaller prompt

Configuration

{
  "context": {
    "compaction": {
      "enabled": true,
      "strategy": "summarize",
      "threshold": 32000
    }
  }
}

Why This Matters

  • Local models (27B-70B) have limited context windows and slow prefill
  • Agentic coding tasks naturally accumulate large contexts (file reads, grep results, edits)
  • Without compaction, users are forced to manually start new conversations mid-task
  • Claude Code, Cursor, and other tools already implement this — OpenCode should too

Metadata

Metadata

Assignees

Labels

coreAnything pertaining to core functionality of the application (opencode server stuff)perfIndicates a performance issue or need for optimization

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions