Skip to content

[FEATURE]: Auto-route to RLM when context overflows #9427

Closed

Description

@rileyseaburg

Problem

When a conversation exceeds the model's context window, OpenCode currently fails with an error or enters an infinite compaction loop. This blocks users from continuing long conversations.

Proposed Solution

Automatically route to RLM (Recursive Language Model) when context overflow is detected. RLM can handle arbitrarily large context by using a Python REPL to analyze the conversation history and generate appropriate responses.

Implementation

When the LLM returns a "prompt is too long" error:

  1. Detect context overflow in processor.ts
  2. Return "rlm" result to the prompt loop
  3. SessionRlm.process() invokes the RLM tool with the full conversation context
  4. RLM uses GLM 4.7 (cheaper model) for subcalls by default
  5. Conversation continues with RLM's response

Working Example

We have a working implementation in our fork: https://github.com/anomalyco/opencode

Key files:

  • src/session/rlm.ts - SessionRlm for handling context overflow
  • src/session/processor.ts - Context overflow detection and routing
  • src/session/prompt.ts - Handle "rlm" result
  • src/tool/rlm.ts - GLM 4.7 as default subcall model
  • src/config/config.ts - RLM config options (mode, threshold, subcall_model)

Benefits

  • No more "context too long" errors blocking work
  • Uses cheaper model (GLM 4.7) for RLM subcalls
  • Configurable via rlm.mode (auto/off), rlm.threshold, rlm.subcall_model
  • Graceful degradation - falls back to session model if GLM unavailable

Would love feedback on whether this approach aligns with OpenCode's direction.

Activity

  1. github-actions commented on Jan 19, 2026

    @github-actions
    Contributor

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

    Feel free to ignore if your specific case involves additional requirements.

  2. github-actions commented on Mar 26, 2026

    @github-actions
    Contributor

    To stay organized issues are automatically closed after 90 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