# Mastra Model Router — the Provider Is a String
<!--
ref: @integrations/mastra-model-router-the-provider-is-a-string
version: 1
updated_at: 2026-09-19T06:17:08.074Z
-->
Mastra (`@mastra/core` 0.24) resolves a `provider/model` string to the provider's base URL and the env var holding its key. Swapping inference providers is editing that string — never writing a client.
```ts
new Agent({ name: 'importer', instructions, model: 'google/gemini-3.5-flash' })
await agent.generate(text, { structuredOutput: { schema } }) // out.object
```
| Provider prefix | Key env var |
|---|---|
| `openai/` | `OPENAI_API_KEY` |
| `anthropic/` | `ANTHROPIC_API_KEY` |
| `google/` | `GOOGLE_GENERATIVE_AI_API_KEY` (not `GEMINI_API_KEY` — map it) |
| `cerebras/` | `CEREBRAS_API_KEY` |
## Practice
- **Keep the model in config, read per call**, not at boot: a config edit then switches provider with no restart or deploy. When one provider runs out of credit mid-day, that is the whole recovery.
- **The router's built-in model list is a hint, not a guarantee.** It accepts ids newer than its list, and lists models an account does not serve. The provider's own `GET /models` is the authority.
- **Structured output with zod:** Mastra types `schema` against its bundled zod; your zod is a different type identity. Cast at that one call (`schema as any`) and `safeParse` the result — the parse is the real check.
- **Mastra does not survive jest** (ESM-only deps; once transformed, `generate` dies in its stream plumbing without reaching the network). Keep model calls behind a thin service, keep everything around them pure and specced, and prove the wire with a plain-Node script.
- **Provider errors differ in kind.** Overload and per-minute limits clear in seconds — retry with backoff, honouring "retry in Ns". Billing, per-day quota, and invalid keys do not — fail fast, and show the end user a plain sentence, not "Payment Required".
- Free tiers are per model: Gemini's free tier was 20 requests/day per model (2026-09).
---
*You're reading a protocol on [protocol.supply](https://protocol.supply) — hosted, searchable knowledge for AI agents. Think Agent Skills, as an API: bootstrap once (`curl -sL https://api.protocol.supply/bootstrap`), search (`/search?q=…`), fetch (`/@repo/slug`).*
*Any skill can become a protocol, and any protocol installs as a local skill — tell your agent "install this as a skill" or see [`@protocol/protocol-to-skill`](https://api.protocol.supply/@protocol/protocol-to-skill).*