Skip to content

[Bug] Model parameter ignored when launching subagent with agent name #18615

@iceblove

Description

@iceblove

Summary

When launching a subagent session via the SDK with both agent and model parameters, opencode ignores the model parameter and uses its built-in fallback chain for that agent instead.

Environment

  • opencode version: 1.2.27
  • Platform: macOS (darwin)

Steps to Reproduce

  1. Configure a custom provider with models:
// ~/.config/opencode/opencode.json
{
  "provider": {
    "opencode-go": {
      "npm": "@ai-sdk/anthropic",
      "options": {
        "baseURL": "https://cloud.infini-ai.com/maas/coding/v1",
        "apiKey": "{env:GENSTUDIO_API_KEY}"
      },
      "models": {
        "minimax-m2.7": { "name": "MiniMax M2.7" },
        "glm-5": { "name": "GLM-5" }
      }
    }
  }
}
  1. Verify the provider and models work correctly:
$ opencode models opencode-go
opencode-go/glm-5
opencode-go/minimax-m2.7

$ opencode run -m opencode-go/minimax-m2.7 "Say: test"
# Works correctly! Uses opencode-go/minimax-m2.7
  1. Launch a subagent via SDK with explicit model parameter:
await client.session.promptAsync({
  path: { id: sessionID },
  body: {
    agent: "explore",
    model: { providerID: "opencode-go", modelID: "minimax-m2.7" },
    parts: [{ type: "text", text: "Say what model you are using" }]
  }
});
  1. Check the database record:
SELECT data FROM message WHERE agent = 'explore' AND role = 'user' ORDER BY time_created DESC LIMIT 1;
-- Result: {"agent":"explore","model":{"providerID":"opencode","modelID":"minimax-m2.5-free"},...}
-- The model is WRONG! It's using opencode/minimax-m2.5-free instead of opencode-go/minimax-m2.7

Expected Behavior

When model parameter is explicitly provided, it should be used as-is. The agent's built-in fallback chain should only be used when NO model parameter is provided.

Actual Behavior

The model parameter is ignored when an agent is specified. OpenCode uses the agent's built-in fallback chain, which resolves to opencode/minimax-m2.5-free (a free built-in model).

Evidence

Database records showing the issue:

  1. Direct model usage (works correctly):
SELECT modelID, providerID FROM message WHERE role = 'assistant' ORDER BY time_created DESC LIMIT 1;
-- Result: modelID = "minimax-m2.7", providerID = "opencode-go" ✓
  1. Subagent with agent name (broken):
SELECT json_extract(data, '$.model') FROM message WHERE agent = 'explore' AND role = 'user' ORDER BY time_created DESC LIMIT 1;
-- Result: {"providerID":"opencode","modelID":"minimax-m2.5-free"} ✗

Root Cause

OpenCode has built-in fallback chains for agents like explore, librarian, etc. When processing a session prompt:

  • If agent is specified, OpenCode appears to use the agent's fallback chain
  • The explicitly provided model parameter is overridden

Why This Matters

  1. Plugin compatibility: Plugins like oh-my-opencode configure custom models for agents, but these are ignored
  2. User choice: Users cannot override agent models even when explicitly specifying them
  3. Cost control: Users may want to use their own API keys with specific providers, but fall back to built-in free models instead

Suggested Fix

The priority should be:

  1. User-provided model (highest priority) - use as-is
  2. Agent's built-in fallback chain (fallback) - only when no model is provided
  3. System default (last resort)

Related Issue

Also reported to oh-my-opencode: code-yeongyu/oh-my-openagent#2741

Initially thought it was an oh-my-opencode issue, but database evidence shows the model is already wrong in the user message record, indicating the issue is in opencode's session handling.

Metadata

Metadata

Assignees

Labels

coreAnything pertaining to core functionality of the application (opencode server stuff)

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions