openclaw - 💡(How to fix) Fix Feature Request: before_agent_turn plugin hook for model routing [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#57393Fetched 2026-04-08 01:50:10
View on GitHub
Comments
0
Participants
1
Timeline
1
Reactions
0
Timeline (top)
cross-referenced ×1

Fix Action

Fix / Workaround

Current State

  • message:preprocessed (managed hook): fires before the agent sees the message, but body mutations do not propagate to model selection
  • message_sending (plugin hook): fires on outbound replies — too late to change the model
  • before_tool_call (plugin hook): fires before tool execution, not before model dispatch
  • No before_agent_turn or model_select hook exists in the current SDK

Proposed Solution

Add a new plugin hook event — before_agent_turn or model_select — that fires after message preprocessing but before model dispatch. The hook should allow plugins to return:

Workarounds Attempted

  1. message:preprocessed hook with body mutation → model selection not affected
  2. message_sending plugin hook → fires too late (outbound)
  3. ❌ Inline /model directive prepend → not parsed from hook-mutated body
  4. sessions.patch with modelOnce → no runtime API available in hook context

Code Example

{
  model?: string;        // Override model for this turn only (modelOnce)
  thinkingLevel?: string; // Override thinking level for this turn
}
RAW_BUFFERClick to expand / collapse

Feature Request: before_agent_turn Plugin Hook for Model Routing

Problem

There is no plugin hook that fires before the model is selected for an agent turn. This means plugins cannot dynamically route messages to different models based on message complexity, cost optimization, or other criteria.

Current State

  • message:preprocessed (managed hook): fires before the agent sees the message, but body mutations do not propagate to model selection
  • message_sending (plugin hook): fires on outbound replies — too late to change the model
  • before_tool_call (plugin hook): fires before tool execution, not before model dispatch
  • No before_agent_turn or model_select hook exists in the current SDK

Proposed Solution

Add a new plugin hook event — before_agent_turn or model_select — that fires after message preprocessing but before model dispatch. The hook should allow plugins to return:

{
  model?: string;        // Override model for this turn only (modelOnce)
  thinkingLevel?: string; // Override thinking level for this turn
}

Use Case: Smart Model Router

We built a plugin that classifies inbound message complexity via regex (zero API cost, <1ms):

LevelExampleTarget Model
1 (trivial)"hello", "yes", "thanks"Haiku (low cost, fastest)
2 (simple)"what time is it", "check status"Sonnet (medium cost)
3 (complex)architecture, multi-step, codingOpus (full power)

The classifier works (91% test accuracy), the plugin loads and registers correctly, but there is no hook point to actually switch the model before the turn starts.

Impact

  • Cost savings: ~40% of messages in a typical session are trivial/simple and could use cheaper models
  • Latency: Haiku responds 3-5x faster than Opus for simple queries
  • Token efficiency: Smaller models use fewer output tokens for simple responses

Implementation Notes

  • Plugin is ready at projects/smart-model-router/ — just needs the hook point
  • Regex-only classification (no API call needed) — adds <1ms latency
  • Conservative default: unknown complexity → stays on default model (no downgrade risk)

Workarounds Attempted

  1. message:preprocessed hook with body mutation → model selection not affected
  2. message_sending plugin hook → fires too late (outbound)
  3. ❌ Inline /model directive prepend → not parsed from hook-mutated body
  4. sessions.patch with modelOnce → no runtime API available in hook context

Environment

  • OpenClaw 2026.3.28 (f9b1079)
  • Plugin SDK: native plugin with definePluginEntry
  • Hooks: managed + plugin both attempted

extent analysis

Fix Plan

To implement the before_agent_turn plugin hook, follow these steps:

  • Add a new hook point in the SDK:

// Add this to the plugin SDK export interface BeforeAgentTurnHook { (message: any): { model?: string; thinkingLevel?: string; }; }

*   Modify the agent turn logic to call the new hook:
    ```typescript
// In the agent turn logic
const beforeAgentTurnHooks = getRegisteredHooks('before_agent_turn');
beforeAgentTurnHooks.forEach((hook) => {
  const override = hook(message);
  if (override.model) {
    // Override the model for this turn
    model = override.model;
  }
  if (override.thinkingLevel) {
    // Override the thinking level for this turn
    thinkingLevel = override.thinkingLevel;
  }
});
  • Register the before_agent_turn hook in the plugin:

// In the smart model router plugin definePluginEntry({ // ... hooks: { 'before_agent_turn': (message) => { // Classify the message complexity using regex const complexity = classifyMessageComplexity(message); switch (complexity) { case 1: return { model: 'Haiku' }; case 2: return { model: 'Sonnet' }; case 3: return { model: 'Opus' }; default: return {}; // Stay on default model } }, }, });


### Verification
To verify that the fix worked, test the smart model router plugin with different message complexities and check that the correct model is used for each turn.

### Extra Tips
*   Make sure to handle unknown message complexities by staying on the default model.
*   Consider adding logging or metrics to track the effectiveness of the smart model router.
*   Keep the regex classification logic efficient to avoid adding significant latency to the agent turn.

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