Skip to main content

FastRouter logoFastRouter

Access 14 FastRouter models through Mastra's model router. Authentication is handled automatically using the FASTROUTER_API_KEY environment variable.

Learn more in the FastRouter documentation.

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

const agent = new Agent({
name: "my-agent",
instructions: "You are a helpful assistant",
model: "fastrouter/anthropic/claude-opus-4.1",
});

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

Models

ModelImageAudioVideoToolsStreamingContext Window
fastrouter/moonshotai/kimi-k2131,072
fastrouter/x-ai/grok-4256,000
fastrouter/google/gemini-2.5-flash1,048,576
fastrouter/google/gemini-2.5-pro1,048,576
fastrouter/openai/gpt-5-nano400,000
fastrouter/openai/gpt-4.11,047,576
fastrouter/openai/gpt-5-mini400,000
fastrouter/openai/gpt-oss-20b131,072
fastrouter/openai/gpt-oss-120b131,072
fastrouter/openai/gpt-5400,000
fastrouter/qwen/qwen3-coder262,144
fastrouter/anthropic/claude-opus-4.1200,000
fastrouter/anthropic/claude-sonnet-4200,000
fastrouter/deepseek-ai/deepseek-r1-distill-llama-70b131,072

Advanced Configuration

Custom Headers

const agent = new Agent({
name: "custom-agent",
model: {
url: "https://go.fastrouter.ai/api/v1",
modelId: "anthropic/claude-opus-4.1",
apiKey: process.env.FASTROUTER_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
? "fastrouter/x-ai/grok-4"
: "fastrouter/anthropic/claude-opus-4.1";
},
});