Mastra with an OpenAI-compatible API: provider setup
Point a Mastra agent at the catalog through one model object: the provider block with a base URL and key, an agent with tools, and how to fail over between ids.
Mastra's model config takes any OpenAI-compatible provider, so a Mastra agent runs on the catalog by setting the provider base URL to https://api.uplinkswitch.com/v1, the key and a catalog model id. This guide gives the provider block, an agent with a tool, and a short note on switching ids or falling over, laid out the way the live LangChain and CrewAI guides are.
The model object
This guide passes the model as Mastra's configuration object on the agent, which needs no provider package; an AI SDK provider instance would also work. For a custom endpoint the object wants two things: a url that ends at the gateway root, and an id whose segment after the slash is the catalog model. The label before the slash is only a provider key you choose; the part the gateway reads is gpt-6.1-sol.
import { Agent } from "@mastra/core/agent";
export const billingAgent = new Agent({
id: "billing-agent",
name: "billing-agent",
instructions: "Answer account questions in two short sentences.",
model: {
id: "uplink/gpt-6.1-sol",
url: "https://api.uplinkswitch.com/v1",
apiKey: process.env.UPLINKSWITCH_API_KEY,
},
});The url stops at /v1. Mastra appends the route, so adding /chat/completions yourself is the usual cause of a path that resolves to nowhere. If a Mastra build does not forward apiKey for a custom url, the object also accepts a headers map, and an Authorization Bearer value there reaches the gateway the same way.
An agent with a tool
Both named ids route tool calls natively, so a Mastra createTool definition passes straight through. Define the tool, hand it to the agent, and generate:
import { createTool } from "@mastra/core/tools";
import { z } from "zod";
const lookupOrder = createTool({
id: "lookup-order",
description: "Return the status of an order by its id",
inputSchema: z.object({ orderId: z.string() }),
execute: async ({ orderId }) => ({ status: await orders.status(orderId) }),
});
const orderAgent = new Agent({
id: "order-agent",
name: "order-agent",
instructions: "Look the order up before you answer.",
model: {
id: "uplink/gpt-6.1-sol",
url: "https://api.uplinkswitch.com/v1",
apiKey: process.env.UPLINKSWITCH_API_KEY,
},
tools: { lookupOrder },
});
const reply = await orderAgent.generate("Where is order A-914?");
console.log(reply.text);A catalog entry that lists tools in supported_parameters has been verified for tool calls; an entry with no such list is unverified, not refused, which missing supported_parameters covers.
Switch ids, or fail over
Because the only per-model difference is the id, a second agent is a copy with one string changed, keeping the same url and key. Put the cheaper, faster id on the agents that fetch and condense, and the stronger id on the one that drafts the final answer:
| Role | Model id | Price per 1M, in / out | Context |
|---|---|---|---|
| retrieval, summaries | gpt-6.1-sol | $0.582 / $2.91 | 1.1M |
| long-context reasoning | claude-sonnet-5-5 | $1.164 / $2.91 | 1M |
For failover, wrap generate so a thrown error retries on a backup agent built on the second id:
async function ask(prompt: string) {
try {
return await billingAgent.generate(prompt);
} catch {
return await backupAgent.generate(prompt); // same url + key, id: "uplink/claude-sonnet-5-5"
}
}The gateway can also do this server side: an auto router key sends every request as auto and sorts it to a tier without the model name changing in your code, which suits a Mastra app whose model id you would rather not edit per deploy. The patterns for retry order and routed keys are on /blog/llm-failover.
Check the connection
Reach the gateway with no key first, so a failure here cannot be the key:
curl -sS -o /dev/null -w '%{http_code}\n' https://api.uplinkswitch.com/v1/catalogA 200 means the host and the catalog are up; a connection error printed instead is network or TLS. Then run the agent once; a 401 at that point means the gateway did not accept a key, either because none was sent or because the one sent is wrong or revoked. The signed receipt at GET /v1/requests/{request_id}/receipt records what each run cost once you want an audit trail.
Common errors
- 401 from the first
generate: no valid key reached the gateway. ConfirmapiKeyis set to a current key, or move the key into theheadersmap as anAuthorizationBearer value. - 404 model not found: the segment after the provider label is not a live id. List what the key may call at
GET /v1/modelsand copy the id exactly, with no vendor prefix. - Tool calls ignored: the request went out on a non-chat mode, or the chosen model does not call tools. Try an id whose catalog entry lists
tools. - A path error on send: the
urlcarries a route after/v1. Trim it back to the gateway root.