Google
Access 18 Google models through Mastra's model router. Authentication is handled automatically using the GOOGLE_GENERATIVE_AI_API_KEY environment variable.
Learn more in the Google documentation.
GOOGLE_GENERATIVE_AI_API_KEY=your-api-key
import { Agent } from "@mastra/core";
const agent = new Agent({
name: "my-agent",
instructions: "You are a helpful assistant",
model: "google/gemini-1.5-flash",
});
// 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);
}
Models
| Model | Image | Audio | Video | Tools | Streaming | Context Window |
|---|---|---|---|---|---|---|
google/gemini-2.5-flash-preview-05-20 | ✓ | ✓ | ✓ | ✓ | ✗ | 1,048,576 |
google/gemini-flash-lite-latest | ✓ | ✓ | ✓ | ✓ | ✗ | 1,048,576 |
google/gemini-2.5-flash | ✓ | ✓ | ✓ | ✓ | ✗ | 1,048,576 |
google/gemini-flash-latest | ✓ | ✓ | ✓ | ✓ | ✗ | 1,048,576 |
google/gemini-2.5-pro-preview-05-06 | ✓ | ✓ | ✓ | ✓ | ✗ | 1,048,576 |
google/gemini-2.0-flash-lite | ✓ | ✓ | ✓ | ✓ | ✗ | 1,048,576 |
google/gemini-live-2.5-flash-preview-native-audio | ✗ | ✓ | ✓ | ✓ | ✗ | 131,072 |
google/gemini-2.0-flash | ✓ | ✓ | ✓ | ✓ | ✗ | 1,048,576 |
google/gemini-2.5-flash-lite | ✓ | ✓ | ✓ | ✓ | ✗ | 1,048,576 |
google/gemini-2.5-pro-preview-06-05 | ✓ | ✓ | ✓ | ✓ | ✗ | 1,048,576 |
google/gemini-2.5-flash-lite-preview-06-17 | ✓ | ✓ | ✓ | ✓ | ✗ | 1,048,576 |
google/gemini-2.5-flash-preview-09-2025 | ✓ | ✓ | ✓ | ✓ | ✗ | 1,048,576 |
google/gemini-2.5-flash-preview-04-17 | ✓ | ✓ | ✓ | ✓ | ✗ | 1,048,576 |
google/gemini-2.5-pro | ✓ | ✓ | ✓ | ✓ | ✗ | 1,048,576 |
google/gemini-1.5-flash | ✓ | ✓ | ✓ | ✓ | ✗ | 1,000,000 |
google/gemini-1.5-flash-8b | ✓ | ✓ | ✓ | ✓ | ✗ | 1,000,000 |
google/gemini-2.5-flash-lite-preview-09-2025 | ✓ | ✓ | ✓ | ✓ | ✗ | 1,048,576 |
google/gemini-1.5-pro | ✓ | ✓ | ✓ | ✓ | ✗ | 1,000,000 |
Advanced Configuration
Custom Headers
const agent = new Agent({
name: "custom-agent",
model: {
modelId: "gemini-1.5-flash",
apiKey: process.env.GOOGLE_GENERATIVE_AI_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
? "google/gemini-live-2.5-flash-preview-native-audio"
: "google/gemini-1.5-flash";
},
});
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/google
yarn add @ai-sdk/google
pnpm add @ai-sdk/google
bun add @ai-sdk/google