跳到主要内容
非官方简体中文翻译,内容版权归 Mastra 所有,遵循 Apache-2.0 许可。

Mastra logoMastra

The Gateway is an OpenAI-compatible API proxy with built-in Observational Memory. Point any HTTP client, SDK, or framework at the gateway and every conversation is automatically remembered without any memory management code.

Learn more in the Gateway documentation.

Get an API key
Get an API key的直接链接

Go to gateway.mastra.ai and sign up for a Mastra account. During the onboarding you'll get your personal API key to authenticate requests.

Usage
Usage的直接链接

Define your API key as an environment variable:

.env
MASTRA_GATEWAY_API_KEY=your-gateway-key

Set your gateway model ID:

src/mastra/agents/my-agent.ts
import { Agent } from "@mastra/core/agent";

const agent = new Agent({
id: "my-agent",
name: "My Agent",
instructions: "You are a helpful assistant",
model: "mastra/openai/gpt-5-mini"
});

Use the same prefix for embedding models that should run through the gateway:

src/mastra/rag/embed.ts
import { ModelRouterEmbeddingModel } from "@mastra/core/llm";

const embedder = new ModelRouterEmbeddingModel("mastra/openai/text-embedding-3-small");

Pass memory.thread and memory.resource when you generate/stream responses to enable Observational Memory:

src/mastra/run.ts
import { weatherAgent } from "./agents/weather-agent";

const memory = {
thread: "assistant-thread-1",
resource: "user-42",
};

const result = await weatherAgent.stream("My name is Alex and I prefer concise answers.", {
memory,
});

for await (const chunk of result.textStream) {
process.stdout.write(chunk);
}

Configuration
Configuration的直接链接

# Use gateway API key
MASTRA_GATEWAY_API_KEY=your-gateway-key

Learn more
Learn more的直接链接

On this page