Skip to content

[FEATURE] Add auto-compact threshold for long sessions #40113

Closed

Description

@secondsoft

Problem

Currently opencode TUI has no automatic context compression. After long sessions, context grows uncontrollably (observed 160K+ tokens), forcing manual compact command.

Proposed Solution

Add configuration for automatic compression:

{
  "session": {
    "maxContextTokens": 120000,
    "autoCompactThreshold": 0.75
  }
}

When context reaches 75% of max, auto-trigger compact to ~40-50% threshold.

Why

  • Openclaw has similar auto-compact feature (works well)
  • Qwen3.5-122B has 128K context limit, 75% (96K) is safe operational threshold
  • Current manual workflow is error-prone for long debugging sessions

Impact

  • Preserve session continuity without manual intervention
  • Prevent GPU memory overload during long tasks

Activity

  1. github-actions commented on Aug 2, 2026

    @github-actions
    Contributor

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

    If your request is meaningfully different from these, please clarify what's distinct.

  2. coyc commented on Aug 2, 2026

    @coyc

    Hi! Building a plugin that addresses this differently: instead of waiting for an auto-compact threshold, the agent compacts its own context proactively, mid-task.

    It exposes compact_context() as a tool — non-blocking, between steps, then the agent continues from a fresh summary. No waiting for the context to grow to 160K+, no manual compact at the wrong moment. Zero custom logic — just the built-in compaction wrapped as a tool.

    Details: https://github.com/coyc/opencode-agent-compaction

  3. ndeybach commented on Sep 30, 2026

    @ndeybach

    +1

    This is needed when using APIs and having long sessions / runs. If a runs grows large and you don't notice ... then you notice the bill at the end of the month !

  4. neriousy commented on Oct 19, 2026

    @neriousy
    Member

    V2 automatically compacts long sessions by default, and compaction.buffer lets you set how many tokens before the model's usable input limit compaction starts. You can also configure how much recent context is retained with compaction.keep.tokens. To get v2: npm i -g @opencode/cli@latest or curl -fsSL https://opencode.ai/v2/install | bash (desktop: update the app). If you still see this on v2, please open a new issue with the details.

  5. ndeybach commented on Oct 20, 2026

    @ndeybach

    Hi @neriousy ,

    Thanks for the headsup and informations.

    Though, "compaction.buffer lets you set how many tokens before the model's usable input limit compaction starts" does not seem to really fit the need.

    If I understood correctly, it depends on the model selected and its maximum input token budget ... which usually you would rather define "I want compaction starting at X amount of token"...

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