opencode zen
Access 10 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);
}
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 | Context | Tools | Reasoning | Image | Audio | Video | Input $/1M | Output $/1M |
---|---|---|---|---|---|---|---|---|
opencode/qwen3-coder | 262K | $0.45 | $2 | |||||
opencode/claude-opus-4-1 | 200K | $15 | $75 | |||||
opencode/kimi-k2 | 262K | $0.60 | $3 | |||||
opencode/claude-sonnet-4-5 | 1.0M | $3 | $15 | |||||
opencode/gpt-5-codex | 400K | $1 | $10 | |||||
opencode/claude-3-5-haiku | 200K | $0.80 | $4 | |||||
opencode/grok-code | 256K | — | — | |||||
opencode/code-supernova | 1.0M | — | — | |||||
opencode/qwen3-max | 262K | $2 | $6 | |||||
opencode/claude-sonnet-4 | 1.0M | $3 | $15 | |||||
opencode/gpt-5 | 400K | $1 | $10 |
Advanced Configuration
Custom Headers
const agent = new Agent({
name: "custom-agent",
model: {
url: "https://opencode.ai/zen/v1/chat/completions",
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/claude-3-5-haiku"
: "opencode/claude-opus-4-1";
}
});