Mistral
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
| Model | Image | Audio | Video | Tools | Streaming | Context Window |
|---|---|---|---|---|---|---|
mistral/devstral-medium-2507 | ✗ | ✗ | ✗ | ✓ | ✗ | 128,000 |
mistral/open-mixtral-8x22b | ✗ | ✗ | ✗ | ✓ | ✗ | 64,000 |
mistral/ministral-8b-latest | ✗ | ✗ | ✗ | ✓ | ✗ | 128,000 |
mistral/pixtral-large-latest | ✓ | ✗ | ✗ | ✓ | ✗ | 128,000 |
mistral/ministral-3b-latest | ✗ | ✗ | ✗ | ✓ | ✗ | 128,000 |
mistral/pixtral-12b | ✓ | ✗ | ✗ | ✓ | ✗ | 128,000 |
mistral/mistral-medium-2505 | ✓ | ✗ | ✗ | ✓ | ✗ | 131,072 |
mistral/devstral-small-2505 | ✗ | ✗ | ✗ | ✓ | ✗ | 128,000 |
mistral/mistral-medium-2508 | ✓ | ✗ | ✗ | ✓ | ✗ | 262,144 |
mistral/mistral-small-latest | ✓ | ✗ | ✗ | ✓ | ✗ | 128,000 |
mistral/magistral-small | ✗ | ✗ | ✗ | ✓ | ✗ | 128,000 |
mistral/devstral-small-2507 | ✗ | ✗ | ✗ | ✓ | ✗ | 128,000 |
mistral/codestral-latest | ✗ | ✗ | ✗ | ✓ | ✗ | 256,000 |
mistral/open-mixtral-8x7b | ✗ | ✗ | ✗ | ✓ | ✗ | 32,000 |
mistral/mistral-nemo | ✗ | ✗ | ✗ | ✓ | ✗ | 128,000 |
mistral/open-mistral-7b | ✗ | ✗ | ✗ | ✓ | ✗ | 8,000 |
mistral/mistral-large-latest | ✗ | ✗ | ✗ | ✓ | ✗ | 131,072 |
mistral/mistral-medium-latest | ✓ | ✗ | ✗ | ✓ | ✗ | 128,000 |
mistral/magistral-medium-latest | ✗ | ✗ | ✗ | ✓ | ✗ | 128,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
- yarn
- pnpm
- bun
npm install @ai-sdk/mistral
yarn add @ai-sdk/mistral
pnpm add @ai-sdk/mistral
bun add @ai-sdk/mistral