Skip to main content

OpenCode Zen logoOpenCode Zen

Access 14 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/an-gbt"
});

// 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);
}
info

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/an-gbt200,000
opencode/big-pickle200,000
opencode/claude-3-5-haiku200,000
opencode/claude-haiku-4-5200,000
opencode/claude-opus-4-1200,000
opencode/claude-sonnet-41,000,000
opencode/claude-sonnet-4-51,000,000
opencode/glm-4.6204,800
opencode/gpt-5400,000
opencode/gpt-5-codex400,000
opencode/grok-code256,000
opencode/kimi-k2262,144
opencode/minimax-m2200,000
opencode/qwen3-coder262,144

Advanced Configuration

Custom Headers

const agent = new Agent({
name: "custom-agent",
model: {
url: "https://opencode.ai/zen/v1",
id: "opencode/an-gbt",
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-coder"
: "opencode/an-gbt";
}
});

On this page