OpenCode 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
| Model | Image | Audio | Video | Tools | Streaming | Context Window |
|---|---|---|---|---|---|---|
opencode/an-gbt | ✗ | ✗ | ✗ | ✓ | ✗ | 200,000 |
opencode/big-pickle | ✗ | ✗ | ✗ | ✓ | ✗ | 200,000 |
opencode/claude-3-5-haiku | ✓ | ✗ | ✗ | ✓ | ✗ | 200,000 |
opencode/claude-haiku-4-5 | ✓ | ✗ | ✗ | ✓ | ✗ | 200,000 |
opencode/claude-opus-4-1 | ✓ | ✗ | ✗ | ✓ | ✗ | 200,000 |
opencode/claude-sonnet-4 | ✓ | ✗ | ✗ | ✓ | ✗ | 1,000,000 |
opencode/claude-sonnet-4-5 | ✓ | ✗ | ✗ | ✓ | ✗ | 1,000,000 |
opencode/glm-4.6 | ✗ | ✗ | ✗ | ✓ | ✗ | 204,800 |
opencode/gpt-5 | ✓ | ✗ | ✗ | ✓ | ✗ | 400,000 |
opencode/gpt-5-codex | ✓ | ✗ | ✗ | ✓ | ✗ | 400,000 |
opencode/grok-code | ✗ | ✗ | ✗ | ✓ | ✗ | 256,000 |
opencode/kimi-k2 | ✗ | ✗ | ✗ | ✓ | ✗ | 262,144 |
opencode/minimax-m2 | ✗ | ✗ | ✗ | ✓ | ✗ | 200,000 |
opencode/qwen3-coder | ✗ | ✗ | ✗ | ✓ | ✗ | 262,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";
}
});