Mastra Adds Private Model Gateways for Self-Hosted AI Agents

Mastra has introduced private model gateways that let agents route requests to self-hosted open-weight models through a custom gateway. The feature can connect Mastra agents to local services such as Ollama, while allowing teams to control which models appear in Studio.
- Mastra agents can route requests to self-hosted open-weight models through custom gateways.
- The example connects Ollama and gpt-oss:20b through a local OpenAI-compatible API.
- Model references use the gateway, provider and model format.
- AUTO_BLOCK_EXTERNAL_PROVIDERS=true hides external providers in Studio.
- The feature requires @mastra/[email protected] or later.
Routing agent requests to private models
Private model gateways add a configurable layer between Mastra agents and the models they use. Developers assign a gateway an ID, then reference the model with a gateway, provider and model path such as private/ollama/gpt-oss:20b.
Mastra's example uses Ollama and gpt-oss:20b. Running ollama serve starts an OpenAI-compatible API at http://localhost:11434/v1. A research agent in Studio can then run on the local model through the gateway, with Ollama logs showing requests arriving locally. In the example described by Mastra, the agent answers with a guide to deploying the model on AWS.
How developers configure a gateway
To use the feature, developers install the latest @mastra/core and an AI SDK provider for the gateway API. Mastra specifies @ai-sdk/[email protected] for the example and says the feature requires @mastra/[email protected] or later. The capability was added in PR #18153.
A gateway definition includes an ID and display name, plus functions for returning available providers and models, building the model-call URL, supplying an API key and resolving the language model for each request. The example registers a gateway called private, exposes Ollama and gpt-oss:20b, and points requests to the local Ollama endpoint.
After registration in the Mastra instance, an agent can select the model with the path private/ollama/gpt-oss:20b. The gateway example leaves the API key empty because the local model does not require one.
Compliance, cost and Studio controls
Mastra says hosted provider services can complicate HIPAA or GDPR compliance unless data retention agreements are in place. Self-hosting removes the third party from the setup, preventing sensitive data from reaching models outside the network. The company also says costs may be reduced because users pay for infrastructure rather than tokens, while retaining control over the models available to their agents.
Teams can prevent external providers from appearing in Studio's model picker by setting AUTO_BLOCK_EXTERNAL_PROVIDERS=true in their .env file. Mastra lists custom gateways as useful for showing only custom gateways in Studio, gateway-owned authentication and model pricing. For users, the feature provides a way to run supported agents against locally managed models instead of relying on external model providers, where the deployment and infrastructure are controlled by their own team.


