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

Open
opened 2026-02-16 18:05:20 -05:00 by yindo · 1 comment
Owner

Originally created by @rileyseaburg on GitHub (Jan 19, 2026).

Originally assigned to: @thdxr on GitHub.

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.

Originally created by @rileyseaburg on GitHub (Jan 19, 2026). Originally assigned to: @thdxr on GitHub. ## 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.
Author
Owner

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

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

  • #8726: Stuck at 'prompt is too long' - reports the exact error condition this feature aims to solve
  • #8455: Recursive Language Model as alternative to Compaction - proposes RLM as an alternative approach with detailed rationale
  • #8554: Enable programmatic sub-LLM calls for RLM - technical implementation proposal for RLM capabilities
  • #8394: Compaction Fails Agent forgets everything - shows current compaction approach is unreliable, motivating the RLM alternative
  • #9349: config not work(feature compact) - reports broken auto-compaction state

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

@github-actions[bot] commented on GitHub (Jan 19, 2026): This issue might be a duplicate of existing issues. Please check: - #8726: Stuck at 'prompt is too long' - reports the exact error condition this feature aims to solve - #8455: Recursive Language Model as alternative to Compaction - proposes RLM as an alternative approach with detailed rationale - #8554: Enable programmatic sub-LLM calls for RLM - technical implementation proposal for RLM capabilities - #8394: Compaction Fails Agent forgets everything - shows current compaction approach is unreliable, motivating the RLM alternative - #9349: config not work(feature compact) - reports broken auto-compaction state Feel free to ignore if your specific case involves additional requirements.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#6810