Skip to main content

OpenCode Zen logoOpenCode Zen

Access 12 OpenCode Zen models through Mastra's model router. Authentication is handled automatically using the OPENCODE_API_KEY environment variable.

Learn more in the OpenCode Zen documentation.

OPENCODE_API_KEY=your-api-key
import { Agent } from "@mastra/core";

const agent = new Agent({
name: "my-agent",
instructions: "You are a helpful assistant",
model: "opencode/claude-3-5-haiku",
});

// Generate a response
const response = await agent.generate("Hello!");

// Stream a response
const stream = await agent.stream("Tell me a story");
for await (const chunk of stream) {
console.log(chunk);
}
OpenAI Compatibility

Mastra uses the OpenAI-compatible /chat/completions endpoint. Some provider-specific features may not be available. Check the OpenCode Zen documentation for details.

Models

ModelImageAudioVideoToolsStreamingContext Window
opencode/qwen3-coder262,144
opencode/claude-opus-4-1200,000
opencode/kimi-k2262,144
opencode/claude-sonnet-4-51,000,000
opencode/gpt-5-codex400,000
opencode/claude-3-5-haiku200,000
opencode/glm-4.6204,800
opencode/grok-code256,000
opencode/code-supernova1,000,000
opencode/qwen3-max262,144
opencode/claude-sonnet-41,000,000
opencode/gpt-5400,000

Advanced Configuration

Custom Headers

const agent = new Agent({
name: "custom-agent",
model: {
url: "https://opencode.ai/zen/v1",
modelId: "claude-3-5-haiku",
apiKey: process.env.OPENCODE_API_KEY,
headers: {
"X-Custom-Header": "value",
},
},
});

Dynamic Model Selection

const agent = new Agent({
name: "dynamic-agent",
model: ({ runtimeContext }) => {
const useAdvanced = runtimeContext.task === "complex";
return useAdvanced ? "opencode/qwen3-max" : "opencode/claude-3-5-haiku";
},
});