> Discover all available pages from the documentation index: https://mastra.zisheng.pro/llms.txt # ![Mastra 标志](https://mastra.ai/brand/logo.svg)Mastra Gateway 是兼容 OpenAI API 的代理,内置 [Observational Memory](https://gateway.mastra.ai/docs/features#observational-memory)。将任意 HTTP client、SDK 或框架指向该 Gateway,即可自动记住每次对话,无需编写 Memory 管理代码。 更多信息请参阅[Gateway 文档](https://gateway.mastra.ai/docs)。 ## 获取 API key 前往 [gateway.mastra.ai](https://gateway.mastra.ai) 注册 Mastra 账号。完成引导流程后,你会获得用于验证请求的个人 API key。 ## 用法 将 API key 定义为环境变量: ```bash MASTRA_GATEWAY_API_KEY=your-gateway-key ``` 设置 Gateway 模型 ID: ```typescript 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', }) ``` 需要通过 Gateway 运行的 embedding 模型也使用相同前缀: ```typescript import { ModelRouterEmbeddingModel } from '@mastra/core/llm' const embedder = new ModelRouterEmbeddingModel('mastra/openai/text-embedding-3-small') ``` 生成或流式输出响应时传入 `memory.thread` 和 `memory.resource`,即可启用 Observational Memory: ```typescript 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) } ``` ## 配置 ```bash # Use gateway API key MASTRA_GATEWAY_API_KEY=your-gateway-key ``` ## 了解更多 - [功能](https://gateway.mastra.ai/docs/features) - [模型](https://gateway.mastra.ai/docs/models) - [限制](https://gateway.mastra.ai/docs/limits) - [API 参考](https://gateway.mastra.ai/docs/api/overview) - [示例](https://gateway.mastra.ai/docs/examples/)