#Switching between models to control costs

1 messages Β· Page 1 of 1 (latest)

tall dust
#

Can before_model_resolve fire after message:transcribed for the same message β€” enabling same-turn model switching based on message content?

I want to auto-switch to a more capable model (gpt-5.4) when a WhatsApp message contains travel-related content, without a one-message lag. My current thinking:

message:transcribed hook detects travel keywords, writes a flag file
before_model_resolve hook reads the flag, overrides model to gpt-5.4
Does the message pipeline sequence allow this β€” i.e. does before_model_resolve run after transcription for the same message? Or is there a better pattern for content-aware model switching?

Running OC 4.15, WhatsApp channel, OpenAI provider.

final ploverBOT
#

To help others find answers, you can mark your question as solved via Right click solution message -> Apps -> βœ… Mark Solution

formal ridgeBOT
# tall dust Can before_model_resolve fire after message:transcribed for the same message β€” e...

Short answer: don’t do the flag-file handoff from message:transcribed to before_model_resolve.

I checked the current source. The order is roughly:

  1. media understanding/transcription updates the finalized inbound context
  2. OpenClaw emits message:transcribed and message:preprocessed
  3. the agent prompt is built
  4. before_model_resolve runs before resolveModel()

But the important bit: message:transcribed is fired via a fire-and-forget internal hook. So although it is scheduled before the agent run, OpenClaw does not await your handler before model resolution. A flag file can race and give you flaky same-turn behavior.

Better pattern: put the routing logic directly in before_model_resolve and inspect event.prompt. By the time that hook runs, the prompt should already include the preprocessed/transcribed user content for that turn, so you can keyword/classifier-route there and return modelOverride / providerOverride directly.

So for same-turn content-aware switching:

formal ridgeBOT
# tall dust Can before_model_resolve fire after message:transcribed for the same message β€” e...
api.on("before_model_resolve", async (event) => {
  const text = event.prompt.toLowerCase();

  if (/\b(flight|hotel|itinerary|travel|airport)\b/.test(text)) {
    return {
      providerOverride: "openai",
      modelOverride: "<your higher-cost travel model>",
    };
  }
});

Use message:transcribed / message:preprocessed for observation, logging, side effects, or metrics, not as a dependency for model selection.

Docs:
https://docs.openclaw.ai/automation/hooks

tall dust
#

Could you share a minimal working plugin skeleton that uses api.on("before_model_resolve", ...)?

We have the hook logic but need the surrounding plugin structure β€” package.json, how to import api, and the install command. Just the bare minimum to get it running locally.

formal ridgeBOT
formal ridgeBOT
formal ridgeBOT
formal ridgeBOT