Model Providers
Model providers are used to interact with different language models.
Vercel’s AI SDK  is one of the most popular Typescript AI libraries. The AI SDK offers model routing (a unified interface on top of OpenAI, Anthropic, etc), structured output, and tool calling.
Mastra is compatible with the AI SDK to help teams build their proof-of-concepts into production-ready apps. When creating agents in Mastra, you can use any AI SDK-compatible model provider.
Here’s a minimal example of an agent using the OpenAI provider from the AI SDK:
import { openai } from "@ai-sdk/openai";
import { Agent } from "@mastra/core/agent";
const agent = new Agent({
name: "WeatherAgent",
instructions: "Instructions for the agent...",
model: openai("gpt-4-turbo"),
});
const result = await agent.generate("What is the weather like?");
Mastra is built on top of the AI SDK and uses AI SDK-compatible model providers for model routing.
Types of AI SDK model providers
Model providers from the AI SDK can be grouped into three main categories:
- Official providers maintained by the AI SDK team
- OpenAI-compatible providers
- Community providers
You can find a list of all available model providers in the AI SDK documentation .
AI SDK model providers are packages that need to be installed in your Mastra project. The default model provider selected during the installation process is installed in the project.
If you want to use a different model provider, you need to install it in your project as well.
Here are some examples of how Mastra agents can be configured to use the different types of model providers:
Official providers
Official providers
Official model providers are maintained by the AI SDK team.
Their packages are usually prefixed with @ai-sdk/
, e.g. @ai-sdk/anthropic
, @ai-sdk/openai
, etc.
import { openai } from "@ai-sdk/openai";
import { Agent } from "@mastra/core/agent";
const agent = new Agent({
name: "WeatherAgent",
instructions: "Instructions for the agent...",
model: openai("gpt-4-turbo"),
});
Additional configuration may be done by importing a helper function from the AI SDK provider. Here’s an example using the OpenAI provider:
import { createOpenAI } from "@ai-sdk/openai";
import { Agent } from "@mastra/core/agent"
const openai = createOpenAI({
baseUrl: "<your-custom-base-url>",
apiKey: "<your-custom-api-key>",
...otherOptions
});
const agent = new Agent({
name: "WeatherAgent",
instructions: "Instructions for the agent...",
model: openai("<model-name>"),
});
Different AI providers may have different options for configuration. Please refer to the AI SDK documentation  for more information.