Skip to main content

OpenAI logoOpenAI

通过 Mastra 的模型路由访问 40 个 OpenAI 模型。身份验证会自动使用 OPENAI_API_KEY 环境变量处理。

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

OpenAI文档中了解更多。

🌐 Learn more in the OpenAI documentation.

.env
OPENAI_API_KEY=your-api-key
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: "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);
}

模型
Direct link to 模型

🌐 Models

ModelContextToolsReasoningImageAudioVideoInput $/1MOutput $/1M
openai/codex-mini-latest200K$2$6
openai/gpt-3.5-turbo16K$0.50$2
openai/gpt-48K$30$60
openai/gpt-4-turbo128K$10$30
openai/gpt-4.11.0M$2$8
openai/gpt-4.1-mini1.0M$0.40$2
openai/gpt-4.1-nano1.0M$0.10$0.40
openai/gpt-4o128K$3$10
openai/gpt-4o-2024-05-13128K$5$15
openai/gpt-4o-2024-08-06128K$3$10
openai/gpt-4o-2024-11-20128K$3$10
openai/gpt-4o-mini128K$0.15$0.60
openai/gpt-5400K$1$10
openai/gpt-5-chat-latest400K$1$10
openai/gpt-5-codex400K$1$10
openai/gpt-5-mini400K$0.25$2
openai/gpt-5-nano400K$0.05$0.40
openai/gpt-5-pro400K$15$120
openai/gpt-5.1400K$1$10
openai/gpt-5.1-chat-latest128K$1$10
openai/gpt-5.1-codex400K$1$10
openai/gpt-5.1-codex-max400K$1$10
openai/gpt-5.1-codex-mini400K$0.25$2
openai/gpt-5.2400K$2$14
openai/gpt-5.2-chat-latest128K$2$14
openai/gpt-5.2-codex400K$2$14
openai/gpt-5.2-pro400K$21$168
openai/o1200K$15$60
openai/o1-mini128K$1$4
openai/o1-preview128K$15$60
openai/o1-pro200K$150$600
openai/o3200K$2$8
openai/o3-deep-research200K$10$40
openai/o3-mini200K$1$4
openai/o3-pro200K$20$80
openai/o4-mini200K$1$4
openai/o4-mini-deep-research200K$2$8
openai/text-embedding-3-large8K$0.13
openai/text-embedding-3-small8K$0.02
openai/text-embedding-ada-0028K$0.10
40 available models

高级配置
Direct link to 高级配置

🌐 Advanced Configuration

自定义头
Direct link to 自定义头

🌐 Custom Headers

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

动态模型选择
Direct link to 动态模型选择

🌐 Dynamic Model Selection

src/mastra/agents/my-agent.ts
const agent = new Agent({
id: "dynamic-agent",
name: "Dynamic Agent",
model: ({ requestContext }) => {
const useAdvanced = requestContext.task === "complex";
return useAdvanced
? "openai/text-embedding-ada-002"
: "openai/codex-mini-latest";
}
});

提供商选项
Direct link to 提供商选项

🌐 Provider Options

OpenAI 通过 providerOptions 参数支持以下特定于提供商的选项:

🌐 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
}
}
});

可用选项
Direct link to 可用选项

🌐 Available Options

conversation?:

string | null | undefined

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

promptCacheRetention?:

"in_memory" | "24h" | 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
Controls whether OpenAI stores your API requests for model training. Required to be "false" if your organization has zero data retention enabled. See: https://platform.openai.com/docs/guides/your-data#zero-data-retention

strictJsonSchema?:

boolean | null | undefined

textVerbosity?:

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

truncation?:

"auto" | "disabled" | null | undefined

user?:

string | null | undefined

直接提供商安装
Direct link to 直接提供商安装

🌐 Direct Provider Installation

这个提供程序也可以作为独立包直接安装,可以替代 Mastra 模型路由字符串使用。有关更多详细信息,请查看 包文档

🌐 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

有关详细的提供商专用文档,请参见 [AI SDK OpenAI provider docs](https://ai-sdk.dev/providers/ai-sdk-providers/openai)。

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