Mastra
Mastra is a framework for building and deploying AI-powered features using a modern JavaScript stack powered by the Vercel AI SDK. Integrating with AI Gateway provides unified model management and routing capabilities.
First, create a new Mastra project using the CLI:
terminalpnpm dlx create-mastra@latest
During the setup, the system prompts you to name your project, choose a default provider, and more. and more. Feel free to use the default settings.
To use the AI Gateway provider, install the
@ai-sdk/gateway
package:pnpm i @ai-sdk/gateway
Using AI SDK
v5
with Mastra requires pre-release versions of the Mastra packages:pnpm i mastra@ai-v5 @mastra/core@ai-v5 @mastra/memory@ai-v5
Create or update your
.env
file with your Vercel AI Gateway API key:.envAI_GATEWAY_API_KEY=your-api-key-here
Now, swap out the
@ai-sdk/openai
package (or your existing model provider) for the@ai-sdk/gateway
package.Update your agent configuration file, typically
src/mastra/agents/weather-agent.ts
to the following code:src/mastra/agents/weather-agent.tsimport 'dotenv/config'; import { gateway } from '@ai-sdk/gateway'; import { Agent } from '@mastra/core/agent'; import { Memory } from '@mastra/memory'; import { LibSQLStore } from '@mastra/libsql'; import { weatherTool } from '../tools/weather-tool'; export const weatherAgent = new Agent({ name: 'Weather Agent', instructions: ` You are a helpful weather assistant that provides accurate weather information and can help planning activities based on the weather. Your primary function is to help users get weather details for specific locations. When responding: - Always ask for a location if none is provided - If the location name isn't in English, please translate it - If giving a location with multiple parts (e.g. "New York, NY"), use the most relevant part (e.g. "New York") - Include relevant details like humidity, wind conditions, and precipitation - Keep responses concise but informative - If the user asks for activities and provides the weather forecast, suggest activities based on the weather forecast. - If the user asks for activities, respond in the format they request. Use the weatherTool to fetch current weather data. `, model: gateway('openai/gpt-4o-mini'), tools: { weatherTool }, memory: new Memory({ storage: new LibSQLStore({ url: 'file:../mastra.db', // path is relative to the .mastra/output directory }), }), }); (async () => { try { const response = await weatherAgent.generate( 'What's the weather in San Francisco today?', ); console.log('Weather Agent Response:', response.text); } catch (error) { console.error('Error invoking weather agent:', error); } })();
Since your agent is now configured to use AI Gateway, run the Mastra development server:
pnpm dev
Open the Mastra Playground and Mastra API to test your agents, workflows, and tools.
Was this helpful?