Skip to Content
DocsGetting StartedModel Providers

Model Providers

Model providers are used to interact with different language models. Mastra uses Vercel’s AI SDK  as a model routing layer to provide a similar syntax for many models:

src/mastra/agents/weather-agent.ts
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?");

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 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.

src/mastra/agents/weather-agent.ts
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:

src/mastra/agents/weather-agent.ts
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.