mastra-models
Skill2 starsChanged 29 days agoApache-2.0View on GitHub
Pick, verify, and configure language and embedding models in Mastra through the model router: provider/model strings, gateways such as OpenRouter, Vercel, Netlify, Azure OpenAI, and the Mastra Gateway, custom gateways, provider API key env vars, fallback chains, provider options, custom headers, local OpenAI-compatible endpoints, and embedding models. Use this skill whenever the user chooses or changes a model, gets a missing API key or unknown provider error, wants a cheaper or faster model, needs failover between providers, or asks which env var a provider needs. Triggers on model router, provider/model, openai/, anthropic/, google/, openrouter/, ModelRouterEmbeddingModel, @mastra/core/llm, MastraModelGateway, addGateway, AzureOpenAIGateway, providerOptions, reasoningEffort, cacheControl, model fallback, OPENAI_API_KEY, ANTHROPIC_API_KEY, LMStudio, Ollama, embedder. Also triggers whenever a model ID must be written into code, because model IDs change often and must be verified.
- Reads credentials
In this repository46 other files it gives its agents.
- mastra-a2a-acpSkill
- mastra-agent-approval-hitlSkill
- mastra-agentic-uiSkill
- mastra-agent-skillsSkill
- mastra-agentsSkill
- mastra-authSkill
- mastra-browserSkill
- mastra-build-playbookSkill
- mastra-channelsSkill
- mastra-classifierSkill
- mastra-cli-apiSkill
- mastra-client-sdkSkill
- mastra-datasets-experimentsSkill
- mastra-deploymentSkill
- mastra-evalsSkill
- mastra-file-based-agentsSkill
- mastra-framework-integrationSkill
- mastra-guideSkill
- mastra-harnessSkill
- mastra-knowledgeSkill
- mastra-mcpSkill
- mastra-memorySkill
- mastra-migrationsSkill
- mastra-multi-agentSkill
- mastra-observabilitySkill
- mastra-observational-memorySkill
- mastra-platformSkill
- mastra-processors-guardrailsSkill
- mastra-project-setupSkill
- mastra-ragSkill
- mastra-schedulesSkill
- mastra-semantic-recallSkill
- mastra-serverSkill
- mastra-signalsSkill
- mastra-storageSkill
- mastra-streamingSkill
- mastra-structured-outputSkill
- mastra-studio-editorSkill
- mastra-tool-integrationsSkill
- mastra-toolsSkill
- mastra-troubleshootingSkill
- mastra-vector-storesSkill
- mastra-voiceSkill
- mastra-workflowsSkill
- mastra-workflow-suspend-resumeSkill
- mastra-workspace-sandboxSkill
The file
We split this skill into the rules it gives an agent
9 rules. About 0 are common, 1 is distinctive, and 8 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈1
Distinctive · estimate
This author's own choices. Worth a look.
8
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 50
If the user names a model from memory, verify it before writing it into code.
- Very specific: line 93
Verify the API: If node_modules/@mastra/ exists, read node_modules/@mastra/core/dist/docs/ and run the registry script (step 2).
- Very specific: line 95
Pick the env var: Look up the prefix in references/providers.md.
- Very specific: line 96
Write the model value: Use the router string. Switch to the config object only for headers, an explicit apiKey, or a custom url.
- Very specific: line 97
Add resilience if asked: Use a fallback array with a maxRetries per entry.
- Very specific: line 146
Use provider/model in id when the server expects a bare model name.
- Very specific: line 147
Add api: 'responses' when the endpoint exposes the OpenAI Responses API.
- Very specific: line 160
urlset to/v1/chat/completions: Use the base URL (http://localhost:1234/v1). - Very specific: line 163
Custom gateway models fail type checks: Run with MASTRA_DEV=true to generate types, or widen with a ModelRouterModelId | CustomModelId …
≈ “Common” is an estimate: at least 1 in 5 of the 408,460 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Mastra Models
Choose a model, write its ID in the router format, make sure its API key is set, and add fallbacks or provider options when needed. Mastra's model router reaches thousands of models from hundreds of providers with one string format and no extra provider packages.
When to use
- Writing a
model:value on an agent, scorer, classifier, or structured output call - Verifying that a provider prefix or model ID exists in the installed Mastra version
- Finding the environment variable a provider needs
- Routing through a gateway (OpenRouter, Vercel, Netlify, Neon, Merge, Azure OpenAI, Mastra Gateway)
- Building a custom gateway for a private or enterprise LLM deployment
- Adding a fallback chain across providers
- Setting provider options (
reasoningEffort,cacheControl,thinkingConfig) or custom headers - Pointing Mastra at a local or self-hosted OpenAI-compatible server (LMStudio, vLLM, Ollama)
- Choosing an embedding model for memory or RAG
When NOT to use
- Writing the agent itself, its instructions, tools, or
generate()calls (usemastra-agents) - Semantic recall configuration and vector indexes for memory (use
mastra-semantic-recall) - Chunking, embedding pipelines, and retrieval (use
mastra-rag) - Choosing a vector database (use
mastra-vector-stores) - Voice models, speech-to-text, and text-to-speech (use
mastra-voice) - Model cost and token metrics in traces (use
mastra-observability) - Debugging a broken project setup or
.envloading (usemastra-project-setupormastra-troubleshooting)
Required inputs
- The task the model serves (reasoning, fast chat, extraction, embeddings) and any budget or latency limit
- The provider or gateway the user already has credentials for
- Whether the deployment must stay inside a cloud or region (Azure, Bedrock-style gateway, self-hosted)
- Whether failover across providers is required
- For embeddings: the target vector dimension, which must match the vector index
If the user names a model from memory, verify it before writing it into code.
The framework: one string, many providers
The router takes provider/model for direct providers and gateway/provider/model for gateways. It reads the matching env var (for example ANTHROPIC_API_KEY) and throws a runtime error naming the missing variable when it is absent.
import { Agent } from '@mastra/core/agent'
export const agent = new Agent({
id: 'my-agent',
name: 'My Agent',
instructions: 'You are a helpful assistant',
model: 'anthropic/claude-sonnet-5-5', // provider/model
})
// Gateway form: 'openrouter/anthropic/claude-haiku-4.5', 'mastra/openai/gpt-5-mini'
// Azure form: 'azure-openai/<your-deployment-name>'The model field accepts five shapes:
| Shape | Example | Use for |
| Router string | 'openai/gpt-6.1-sol' | Default choice |
| Config object | { id, apiKey?, headers?, url?, api? } | Custom headers, explicit key, OpenAI-compatible URL |
| Fallback array | [{ model, maxRetries, modelSettings?, providerOptions?, headers? }] | Failover across providers |
| Function | ({ requestContext }) => string | Per-tenant or per-tier choice |
| AI SDK model | groq('gemma2-9b-it') | When you must use an @ai-sdk/* provider module directly |
Embeddings use the same strings through ModelRouterEmbeddingModel from @mastra/core/llm:
import { ModelRouterEmbeddingModel } from '@mastra/core/llm'
const embedder = new ModelRouterEmbeddingModel('openai/text-embedding-3-small') // 1536 dimsWorkflow
- Verify the API. If
node_modules/@mastra/exists, readnode_modules/@mastra/core/dist/docs/and run the registry script (step 2). Otherwise fetchhttps://mastra.ai/models.mdand the provider page, for examplehttps://mastra.ai/models/providers/openai.md. Trust these over memory. Model IDs in training data are often stale. - Verify the model ID. From the project root, run
node <skill-dir>/scripts/provider-registry.mjs --listto list provider prefixes, then--provider <prefix>to list that provider's models, newest first. The script readsnode_modules/@mastra/core/dist/provider-registry.json, so it reflects the installed version. Withoutnode_modules, use the provider page's model table. - Pick the env var. Look up the prefix in
references/providers.md. Add the variable to.env.exampleand the real value to.env. - Write the model value. Use the router string. Switch to the config object only for headers, an explicit
apiKey, or a customurl. - Add resilience if asked. Use a fallback array with a
maxRetriesper entry. Put the cheapest reliable model last. - Tune with provider options. Set
providerOptionson the system message, a user message, the call options, or a fallback entry. - Register custom gateways on
new Mastra({ gateways })or withmastra.addGateway(), then reference models as<gateway-id>/<provider>/<model>. - Test one call through Studio (
npx mastra dev) or a short script, and confirm the response comes from the expected model.
Deep dive: fallbacks and provider options
import { Agent } from '@mastra/core/agent'
export const resilient = new Agent({
id: 'tuned-resilient',
name: 'Tuned Resilient Agent',
instructions: 'You are a helpful assistant.',
model: [
{
model: 'google/gemini-3.8-flash',
maxRetries: 2,
modelSettings: { temperature: 0.3 },
providerOptions: { google: { thinkingConfig: { thinkingBudget: 0 } } },
},
{
model: 'openai/gpt-6-luna',
maxRetries: 2,
modelSettings: { temperature: 0.7 },
providerOptions: { openai: { reasoningEffort: 'low' } },
},
],
})- Mastra moves to the next entry on a 500 error, rate limit, or timeout, after that entry's retries are spent.
- Precedence for
modelSettingsandproviderOptions: fallback entry, then call-time options, then agentdefaultOptions.modelSettingsshallow-merges.providerOptionsdeep-merges. - Headers: call-time
modelSettings.headersbeat per-entryheaders, which beat headers from the router model. - Options are namespaced by provider key:
openai,anthropic,google,azure. The provider page lists every option (for example AnthropiccacheControlandthinking).
Deep dive: local and custom endpoints
Point at any OpenAI-compatible server with url (the base URL ending in /v1, never the chat endpoint):
model: {
id: 'lmstudio/qwen/qwen3-30b-a3b-2507',
url: 'http://localhost:1234/v1',
}- Use
provider/modelinidwhen the server expects a bare model name. Usegateway/provider/modelwhen the upstream namespace includes the provider. - Add
api: 'responses'when the endpoint exposes the OpenAI Responses API. The default is'chat'. Responses readsproviderOptions.openai; chat readsproviderOptions['openai-compatible']. - To proxy a built-in provider without changing code, set
OPENAI_BASE_URLorANTHROPIC_BASE_URLnext to the API key.
Failure patterns
- Colon form
openai:gpt-.... The router uses a slash. Write'openai/gpt-6.1-sol'. - Model ID from memory. It may not exist in the installed version. Run the registry script or read the provider page first.
- Installing
@ai-sdk/openaiand importingopenai(...)by default. Not needed. The router string needs no provider package. Use AI SDK modules only when the router lacks the provider. - Missing or misnamed env var. The error names the expected variable. Check the table: Google uses
GOOGLE_API_KEY(falls back toGOOGLE_GENERATIVE_AI_API_KEY), Together usesTOGETHER_API_KEY, Vercel usesAI_GATEWAY_API_KEY. - Gateway string without the provider segment. OpenRouter, Vercel, Netlify, and Mastra Gateway need
gateway/provider/model. - Azure model string set to a model name. Azure uses your deployment name:
'azure-openai/my-gpt-5-4-deployment', plus anAzureOpenAIGatewayregistered onMastra. urlset to/v1/chat/completions. Use the base URL (http://localhost:1234/v1).- Embedding dimension mismatch.
text-embedding-3-smallis 1536 dimensions,text-embedding-3-largeis 3072. Create the vector index with the same dimension. - Fallback chain with identical providers. An outage takes out every entry. Mix providers.
- Custom gateway models fail type checks. Run with
MASTRA_DEV=trueto generate types, or widen with aModelRouterModelId | CustomModelIdunion.
Output format
A finished model change contains:
- The
model:value as a verified router string, fallback array, or config object - The provider env var in
.env.example(never commit.env) - Any gateway registered on
new Mastra({ gateways }) - For embeddings: the embedder and the matching vector dimension
- Evidence: the registry script output or provider page used to verify the ID, and one successful call
Reference files
references/api.md- Model config shapes, fallback fields, embeddings, gateway classes,MastraModelGatewaymethods,ProviderConfig, Mastra gateway accessors, related env vars.references/providers.md- Every provider and gateway prefix with its environment variables.references/examples.md- Dynamic model per tier, Azure OpenAI gateway, custom private gateway, local model with embeddings.references/checklist.md- Model selection and production checklist.scripts/provider-registry.mjs- Lists installed providers (--list) and a provider's models (--provider <prefix>). Adapted from github.com/mastra-ai/skills (Apache-2.0).
Live docs
- https://mastra.ai/models
- https://mastra.ai/models/providers
- https://mastra.ai/models/gateways
- https://mastra.ai/models/gateways/custom-gateways
- https://mastra.ai/models/embeddings
- https://mastra.ai/models/environment-variables
- https://mastra.ai/reference/core/addGateway
- https://mastra.ai/reference/core/mastra-model-gateway
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

