Skip to content

Plugin API: pre-dispatch model override hook (or model field in chat.message output) #28901

Closed as not planned

Description

@Knyrps

Feature request: pre-dispatch model override hook (or model field in chat.message output)

Problem

Plugins currently have no way to influence which model is used for the user's in-flight turn. This blocks a class of plugins that want to route a single prompt to different models based on its content - for example:

  • An "AutoModel" plugin that classifies prompt complexity with a small/cheap model and routes the actual answer to mini/medium/high/huge model tiers.
  • A "thinking budget" plugin that picks a reasoning-heavy model only when the prompt looks like it needs one.
  • A "safety / policy" plugin that downgrades to a less powerful model for prompts matching a pattern.

Current state (verified against opencode v1.15.7, plugin SDK v? same release)

The hook lifecycle for a user turn is:

  1. chat.message
  2. experimental.chat.messages.transform
  3. experimental.chat.system.transform
  4. chat.params
  5. chat.headers
  6. LLM call
  7. tool.execute.*, experimental.text.complete
  8. Event stream (message.updated, session.idle, ...)

In every one of these hooks, model: Model (or equivalent) is input only. None of the output shapes accept a model field. The dispatcher resolves the model before the hook chain runs, and downstream hooks cannot change it.

I verified this by mutating input.model from inside chat.message and chat.params:

  • Mutating chat.message input.model.modelID and input.model.providerID: no effect on the assistant message that follows. The chat.params hook fired ~1s later still saw the original model.
  • Mutating chat.params input.model: the mutation persists in the params object (a subsequent params.entry log line shows the new value), but the dispatcher reads the model from elsewhere and the assistant still ran on the original model.

I also tested client.session.prompt({ body: { model: ... } }):

  • The HTTP request is accepted and the new chat.message hook for the queued user message shows input.model = the new model.
  • But the opencode run CLI tears down the process when the original turn idles, before the queued follow-up is dispatched, so I could not confirm whether the queued turn actually runs on the requested model. Even if it does, this is "fire a separate plugin-initiated turn", not "influence the user's current turn".

There is also no Session.model field on the SDK Session type, so a plugin cannot pre-set a session-level default model between turns either. The TUI's model picker lives in TUI state, not server state.

Proposed solutions (any one would unblock the use case)

Option A - allow model in chat.message output

"chat.message"?: (input, output: {
  message: UserMessage;
  parts: Part[];
  model?: { providerID: string; modelID: string }; // <-- new, optional override
}) => Promise<void>;

If multiple plugins set model, last-write-wins (or define a precedence).

Option B - a new pre-dispatch hook

"chat.model.select"?: (input: {
  sessionID: string;
  agent: string;
  message: UserMessage;
  defaultModel: { providerID: string; modelID: string };
}, output: {
  model: { providerID: string; modelID: string };
}) => Promise<void>;

This fires after chat.message (so plugins can inspect the full message) but before experimental.chat.messages.transform / chat.params. Whatever output.model is set to becomes the model used for the rest of the lifecycle.

Option C - allow chat.params to override the model

Add model to the chat.params output shape. Same effect, less invasive to the hook surface, but couples a model-routing concern to the params hook.

I would mildly prefer Option B for clarity (the new hook's single purpose is model selection), but Option A is the minimum-surface-area change.

Why not work around this in user-land

The only workaround today is "let the user's turn run on the default model, then fire a second plugin-initiated turn with client.session.prompt({ body: { model } })". That doubles token cost on every mis-routed turn, produces two assistant replies in the transcript, and confuses the UX. It's not a workaround - it's a different feature.

Repro / reference

Source for the spike showing each mutation attempt and its (lack of) effect is available on request. I'm happy to file a PR for Option A or B if a maintainer signals which shape is preferred.

Activity

  1. github-actions commented on May 22, 2026

    @github-actions
    Contributor

    This issue doesn't fully meet our contributing guidelines.

    What needs to be fixed:

    • Feature requests must use the feature request template, which requires the title to start with [FEATURE]: and include the verification checkbox confirming the feature hasn't been suggested before.

    Please edit this issue to address the above within 2 hours, or it will be automatically closed.


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

    If you believe this was flagged incorrectly, please let a maintainer know.

  2. added
    needs:compliance
    on May 22, 2026
  3. github-actions commented on May 22, 2026

    @github-actions
    Contributor

    This issue has been automatically closed because it was not updated to meet our contributing guidelines within the 2-hour window.

    Feel free to open a new issue that follows our issue templates.

  4. removed
    needs:compliance
    on May 22, 2026
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