Skip to main content

Mistral logoMistral

Access 19 Mistral models through Mastra's model router. Authentication is handled automatically using the MISTRAL_API_KEY environment variable.

Learn more in the Mistral documentation.

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

const agent = new Agent({
name: "my-agent",
instructions: "You are a helpful assistant",
model: "mistral/codestral-latest",
});

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

Models

ModelImageAudioVideoToolsStreamingContext Window
mistral/devstral-medium-2507128,000
mistral/open-mixtral-8x22b64,000
mistral/ministral-8b-latest128,000
mistral/pixtral-large-latest128,000
mistral/ministral-3b-latest128,000
mistral/pixtral-12b128,000
mistral/mistral-medium-2505131,072
mistral/devstral-small-2505128,000
mistral/mistral-medium-2508262,144
mistral/mistral-small-latest128,000
mistral/magistral-small128,000
mistral/devstral-small-2507128,000
mistral/codestral-latest256,000
mistral/open-mixtral-8x7b32,000
mistral/mistral-nemo128,000
mistral/open-mistral-7b8,000
mistral/mistral-large-latest131,072
mistral/mistral-medium-latest128,000
mistral/magistral-medium-latest128,000

Advanced Configuration

Custom Headers

const agent = new Agent({
name: "custom-agent",
model: {
url: "https://api.mistral.ai/v1",
modelId: "codestral-latest",
apiKey: process.env.MISTRAL_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
? "mistral/pixtral-large-latest"
: "mistral/codestral-latest";
},
});

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/mistral