openclaw - 💡(How to fix) Fix Plugin hook: support per-message model switching in persistent sessions [1 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
openclaw/openclaw#57941Fetched 2026-04-08 01:55:55
View on GitHub
Comments
0
Participants
1
Timeline
0
Reactions
0
Participants

Error Message

LiveSessionModelSwitchError: Live session model switch requested: anthropic/claude-sonnet-4-6

Fix Action

Fix / Workaround

Current Workaround

None viable:

  • Spawning subagents loses session continuity (main agent never sees subagent responses)
  • Separate sessions per model break shared history between channels (e.g. WhatsApp + LMC)
  • Intercepting via before_dispatch with handled: true bypasses the main agent entirely, breaking tool access and memory

Code Example

LiveSessionModelSwitchError: Live session model switch requested: anthropic/claude-sonnet-4-6
RAW_BUFFERClick to expand / collapse

Feature Request

Problem

The before_model_resolve plugin hook can only influence the model at session start. When a session is already running with model A and the hook returns a different model for a subsequent message, OpenClaw throws a LiveSessionModelSwitchError instead of honoring the override.

Error observed:

LiveSessionModelSwitchError: Live session model switch requested: anthropic/claude-sonnet-4-6

Use Case

Per-message model routing based on message complexity — e.g.:

  • Short/simple messages → cheaper model (openrouter/auto)
  • Normal messages → anthropic/claude-sonnet-4-6
  • Complex/code messages → anthropic/claude-opus-4-6

This would allow significant cost savings without sacrificing quality for complex tasks.

Expected Behavior

before_model_resolve should be honored on every message turn, not just session start. If the model changes mid-session, OpenClaw should either:

  1. Spawn a temporary sub-context for that turn using the requested model, then merge the result back into the session history, or
  2. Allow model switching mid-session with the understanding that history is shared across model changes

Current Workaround

None viable:

  • Spawning subagents loses session continuity (main agent never sees subagent responses)
  • Separate sessions per model break shared history between channels (e.g. WhatsApp + LMC)
  • Intercepting via before_dispatch with handled: true bypasses the main agent entirely, breaking tool access and memory

Additional Context

Tested with a custom model-router plugin using before_model_resolve. Classification works correctly, but the session model switch causes errors mid-conversation. The hook fires but OpenClaw rejects the model change after the first turn.

Would love to see this as a native feature — it would unlock powerful cost-optimization use cases for plugin authors.

extent analysis

Fix Plan

To address the LiveSessionModelSwitchError, we need to modify the before_model_resolve plugin hook to handle model switching mid-session. Here are the steps:

  • Update the before_model_resolve hook to check if the session is already running and if the requested model is different from the current one.
  • If the models are different, create a temporary sub-context for that turn using the requested model.
  • Merge the result back into the session history after the turn is completed.

Example code snippet:

def before_model_resolve(session, message):
    # Check if session is already running and models are different
    if session.model and session.model != message.model:
        # Create a temporary sub-context for this turn
        temp_session = session.clone()
        temp_session.model = message.model
        # Process the message with the temporary sub-context
        response = temp_session.process_message(message)
        # Merge the result back into the session history
        session.history.append(response)
        return response
    # If models are the same, proceed as usual
    return session.model

Verification

To verify that the fix worked, test the before_model_resolve hook with different models and message types. Check that the session history is updated correctly and that the model switching works as expected.

Extra Tips

  • Make sure to handle any potential errors that may occur during the model switching process.
  • Consider adding additional logging to track model switches and any issues that may arise.
  • This fix assumes that the before_model_resolve hook has access to the session and message objects. If this is not the case, additional modifications may be needed to pass the required information to the hook.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING