Skip to main content

Inference logoInference

Access 9 Inference models through Mastra's model router. Authentication is handled automatically using the INFERENCE_API_KEY environment variable.

Learn more in the Inference documentation.

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

const agent = new Agent({
name: "my-agent",
instructions: "You are a helpful assistant",
model: "inference/google/gemma-3",
});

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

Models

ModelImageAudioVideoToolsStreamingContext Window
inference/mistral/mistral-nemo-12b-instruct16,000
inference/google/gemma-3125,000
inference/osmosis/osmosis-structure-0.6b4,000
inference/qwen/qwen3-embedding-4b32,000
inference/qwen/qwen-2.5-7b-vision-instruct125,000
inference/meta/llama-3.2-11b-vision-instruct16,000
inference/meta/llama-3.1-8b-instruct16,000
inference/meta/llama-3.2-3b-instruct16,000
inference/meta/llama-3.2-1b-instruct16,000

Advanced Configuration

Custom Headers

const agent = new Agent({
name: "custom-agent",
model: {
url: "https://inference.net/v1",
modelId: "google/gemma-3",
apiKey: process.env.INFERENCE_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
? "inference/qwen/qwen3-embedding-4b"
: "inference/google/gemma-3";
},
});