Skip to main content

OpenAI logoOpenAI

Access 31 OpenAI models through Mastra's model router. Authentication is handled automatically using the OPENAI_API_KEY environment variable.

Learn more in the OpenAI documentation.

OPENAI_API_KEY=your-api-key
import { Agent } from "@mastra/core";

const agent = new Agent({
name: "my-agent",
instructions: "You are a helpful assistant",
model: "openai/codex-mini-latest"
});

// Generate a response
const response = await agent.generate("Hello!");

// Stream a response
const stream = await agent.stream("Tell me a story");
for await (const chunk of stream) {
console.log(chunk);
}

Models

ModelImageAudioVideoToolsStreamingContext Window
openai/codex-mini-latest200,000
openai/gpt-3.5-turbo16,385
openai/gpt-48,192
openai/gpt-4-turbo128,000
openai/gpt-4.11,047,576
openai/gpt-4.1-mini1,047,576
openai/gpt-4.1-nano1,047,576
openai/gpt-4o128,000
openai/gpt-4o-2024-05-13128,000
openai/gpt-4o-2024-08-06128,000
openai/gpt-4o-2024-11-20128,000
openai/gpt-4o-mini128,000
openai/gpt-5400,000
openai/gpt-5-chat-latest400,000
openai/gpt-5-codex400,000
openai/gpt-5-mini400,000
openai/gpt-5-nano400,000
openai/gpt-5-pro400,000
openai/o1200,000
openai/o1-mini128,000
openai/o1-preview128,000
openai/o1-pro200,000
openai/o3200,000
openai/o3-deep-research200,000
openai/o3-mini200,000
openai/o3-pro200,000
openai/o4-mini200,000
openai/o4-mini-deep-research200,000
openai/text-embedding-3-large8,191
openai/text-embedding-3-small8,191
openai/text-embedding-ada-0028,192

Advanced Configuration

Custom Headers

const agent = new Agent({
name: "custom-agent",
model: {
id: "openai/codex-mini-latest",
apiKey: process.env.OPENAI_API_KEY,
headers: {
"X-Custom-Header": "value"
}
}
});

Dynamic Model Selection

const agent = new Agent({
name: "dynamic-agent",
model: ({ runtimeContext }) => {
const useAdvanced = runtimeContext.task === "complex";
return useAdvanced
? "openai/text-embedding-ada-002"
: "openai/codex-mini-latest";
}
});

Provider Options

OpenAI supports the following provider-specific options via the providerOptions parameter:

const response = await agent.generate("Hello!", {
providerOptions: {
openai: {
// See available options in the table below
}
}
});

Available Options

include?:

("file_search_call.results" | "message.output_text.logprobs" | "reasoning.encrypted_content")[] | null | undefined

instructions?:

string | null | undefined

logprobs?:

number | boolean | undefined

maxToolCalls?:

number | null | undefined

metadata?:

any

parallelToolCalls?:

boolean | null | undefined

previousResponseId?:

string | null | undefined

promptCacheKey?:

string | null | undefined

reasoningEffort?:

string | null | undefined

reasoningSummary?:

string | null | undefined

safetyIdentifier?:

string | null | undefined

serviceTier?:

"default" | "auto" | "flex" | "priority" | null | undefined

store?:

boolean | null | undefined

strictJsonSchema?:

boolean | null | undefined

textVerbosity?:

"low" | "medium" | "high" | null | undefined

truncation?:

"auto" | "disabled" | null | undefined

user?:

string | null | undefined

Direct Provider Installation

This provider can also be installed directly as a standalone package, which can be used instead of the Mastra model router string. View the package documentation for more details.

npm install @ai-sdk/openai

For detailed provider-specific documentation, see the AI SDK OpenAI provider docs.