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