Mastra Framework
Use the Mastra framework with the gateway by selecting a gateway-backed model such as mastra/openai/gpt-5-mini. Then pass memory.thread and memory.resource when you stream to enable observational memory.
Set your API key
Set your API key in the environment variable MASTRA_GATEWAY_API_KEY to authenticate with the gateway.
.env
MASTRA_GATEWAY_API_KEY="sk-..."Set the gateway model ID
Set the agent model to a gateway model ID. You do not need to wire custom headers or map request context manually.
src/mastra/agents/weather-agent.ts
import { Agent } from '@mastra/core/agent'
export const weatherAgent = new Agent({
id: 'weather-agent',
name: 'Weather Agent',
instructions: 'You are a helpful weather assistant.',
model: 'mastra/openai/gpt-5-mini',
})Stream with server-enriched memory
Pass the same thread and resource values on each request. The gateway enriches each call with stored observations for that conversation automatically.
src/mastra/run.ts
import { weatherAgent } from './agents/weather-agent'
const memory = {
thread: 'weather-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)
}
const followUp = await weatherAgent.stream('What is my name?', {
memory,
})
for await (const chunk of followUp.textStream) {
process.stdout.write(chunk)
}
// "Alex"Related
- Features: Observational memory, streaming, BYOK, and gateway tools
- Vercel AI SDK: Use
@ai-sdk/openai-compatibleand@ai-sdk/anthropic - API reference: Complete endpoint documentation