Skip to main content

Cerebras logoCerebras

Access 3 Cerebras models through Mastra's model router. Authentication is handled automatically using the CEREBRAS_API_KEY environment variable.

Learn more in the Cerebras documentation.

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

const agent = new Agent({
name: "my-agent",
instructions: "You are a helpful assistant",
model: "cerebras/gpt-oss-120b",
});

// 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 Cerebras documentation for details.

Models

ModelImageAudioVideoToolsStreamingContext Window
cerebras/qwen-3-235b-a22b-instruct-2507131,000
cerebras/qwen-3-coder-480b131,000
cerebras/gpt-oss-120b131,072

Advanced Configuration

Custom Headers

const agent = new Agent({
name: "custom-agent",
model: {
url: "https://api.cerebras.ai/v1",
modelId: "gpt-oss-120b",
apiKey: process.env.CEREBRAS_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 ? "cerebras/qwen-3-coder-480b" : "cerebras/gpt-oss-120b";
},
});

Direct Provider Installation

This provider can also be installed directly as a standalone package, which can be used instead of the Mastra model router string. View the package documentation for more details.

npm install @ai-sdk/cerebras