OpenAI API max_tokens 和 context window 有什么区别?

更新于 2026-11-03 · easyrouter.cyou

简答:一句话:max_tokens 是输出预算,context window 是单次请求的总 token 容量;实际可生成的内容还要同时受模型最大输出限制影响。Chat Completions 当前优先核对 max_completion_tokens,Responses API 核对 max_output_tokens,旧版 max_tokens 是否可用要以具体端点和兼容接口为准。

定义:context window 指模型一次请求能够处理的 token 总容量,而 max_tokens 及其新版本参数指本次请求允许模型生成的输出 token 上限,两者不是同一个限制。

适用范围:先确认你正在使用哪个端点

本文面向接入 OpenAI API 或 OpenAI 兼容接口的开发者,重点解释输出长度参数与上下文窗口的关系。参数名称、支持范围和错误返回可能因模型、端点或兼容层不同而变化;在 API Router、聚合网关或第三方客户端中,不能仅凭界面里出现了某个字段,就断定服务端一定会透传它。

截至 2026-11-03,OpenAI 文档对当前接口的表述是:Chat Completions 使用 max_completion_tokens,Responses 使用 max_output_tokens;Chat Completions 仍列出旧的 max_tokens,但标为已弃用,并说明它不兼容部分较新的推理模型。具体模型的 context window 和最大输出限制,应查看当前模型文档,而不是把一个模型的数字套用到另一个模型。

本文是第三方接入指南,不代表任何品牌或兼容服务的官方说明。

三行结论:总容量、输出预算和模型上限

概念它限制什么排查时看哪里
context window单次请求的总 token 容量,通常要容纳输入、输出,部分模型还包括 reasoning token模型详情页、模型文档、实际错误响应
max_tokens旧版或兼容接口常见的输出上限字段当前端点文档;不要默认它仍被支持
max_completion_tokensChat Completions 的生成上限,包括可见输出和部分不可见生成 tokenChat Completions 请求参数与响应 usage
max_output_tokensResponses API 的生成上限,包括可见输出和 reasoning tokenResponses API 请求参数与响应 usage
最大输出限制模型单次最多生成多少输出 token,可能小于 context window具体模型文档

OpenAI 文档将 context window 定义为单次请求可使用的最大 token 数,并说明其中包括输入、输出以及部分模型的 reasoning token;模型还可能有独立的输出上限。因此,“模型有很大的上下文窗口”并不等于“一次可以生成同样多的答案”。OpenAI 对话状态与上下文窗口说明

最短步骤:用一条公式判断输出空间

  1. 确认端点:请求地址是 Chat Completions 还是 Responses。前者通常使用 messages,后者使用 input。
  2. 确认模型:从当前控制台、模型列表或服务商文档复制准确的模型 ID。模型目录可见不等于当前项目一定有权调用。
  3. 统计输入:把系统提示、历史消息、工具定义、文件、图片等都视为可能占用上下文的输入,不要只统计用户最后一句话。
  4. 核对两个上限:同时查看 context window 和模型最大输出限制。
  5. 设置输出预算:可用输出空间通常可按以下关系估算:可用输出空间 ≈ context window - 实际输入 token - 必要的 reasoning 或结构开销;最终还要受模型最大输出限制和端点参数限制约束。
  6. 检查响应:Chat Completions 看 usage.prompt_tokens、completion_tokens、total_tokens;Responses 看 usage.input_tokens、output_tokens、total_tokens。

这个公式是排查思路,不是对所有模型和工具调用场景都精确适用的计费公式。完整请求还可能包含消息边界、工具 schema、图片和文件等额外结构。OpenAI 的 token 文档也明确提醒,纯文本统计不一定等于完整 API 请求的输入统计。OpenAI Token 统计说明

参数对照:max_tokens、max_completion_tokens 与 max_output_tokens

接口或场景建议优先核对的字段注意事项
Chat Completionsmax_completion_tokens官方定义为生成 completion 的上限,包含可见输出和 reasoning token。
Responsesmax_output_tokens官方定义为 response 的生成上限,也包含可见输出和 reasoning token。
旧版 Chat Completions 或旧 SDKmax_tokens当前参考文档标为 deprecated;是否仍能用要看端点、模型和兼容层。
OpenAI 兼容网关先看网关文档,再看实际返回可能透传、改名、忽略或拒绝字段;不能把原生接口行为直接当成网关行为。

Chat Completions 参考文档同时列出 max_completion_tokens 和已弃用的 max_tokens,并说明前者的上限包含可见输出与 reasoning token。Responses 参考文档使用 max_output_tokens,定义同样强调它是生成 token 的上限。Chat Completions 创建接口参考 Responses 创建接口参考

因此,不能简单写成“max_tokens 就是 context window”。更准确的说法是:它是一个请求级的输出预算字段;context window 是输入和输出共同消耗的总容量。

最小请求:分别测试两个端点

下面的命令是通用模板,不是已核验的某个兼容服务实例。运行前必须自行核对 Base URL、模型 ID、密钥权限和端点路径。密钥只从环境变量读取,不要直接写入代码、截图、日志或网页前端。

Chat Completions 模板

export API_KEY="请替换为当前密钥"

curl https://api.openai.com/v1/chat/completions -H "Authorization: Bearer $API_KEY" -H "Content-Type: application/json" -d '{"model":"请替换为已核实的模型ID","messages":[{"role":"user","content":"用一句话解释 context window。"}],"max_completion_tokens":100}'

成功时,重点检查 HTTP 状态码、choices[0].message.content 和 usage.prompt_tokens、usage.completion_tokens、usage.total_tokens。如果端点只接受旧字段,或返回参数不支持错误,不要盲目重复请求,应回到当前端点文档核对。

Responses 模板

curl https://api.openai.com/v1/responses -H "Authorization: Bearer $API_KEY" -H "Content-Type: application/json" -d '{"model":"请替换为已核实的模型ID","input":"用一句话解释 context window。","max_output_tokens":100}'

成功时,检查 HTTP 状态码、响应状态、文本输出字段以及 usage.input_tokens、usage.output_tokens、usage.total_tokens。Responses 的完整输入也可以使用官方 input-token counting API 估算;它能处理消息、图片、文件、工具和对话等输入形式。OpenAI Token 计算指南

如果你使用的是兼容接口,把示例中的主机名替换为服务商实际提供的 Base URL,并分别确认是否需要保留 /v1、是否支持 /chat/completions 或 /responses,以及参数是否原样透传。无法从当前文档核实的字段,应标记为“需核实”,不要把占位符命令当成已验证实例。

成功标准:不要只看“返回了文字”

  • HTTP 层:返回成功状态码,且没有被网关改写成错误。
  • 模型层:响应中的模型字段与请求或当前路由结果一致;如果不一致,先核对网关路由规则。
  • 内容层:能读取预期的文本字段,而不是只看到空的工具调用或不完整事件。
  • 长度层:查看结束原因。Chat Completions 可关注 finish_reason;如果是长度相关结束,检查输出预算是否过小或上下文是否接近上限。
  • usage 层:实际 token 统计与预估方向一致。reasoning 模型的可见文字较短,也可能消耗额外输出 token。

OpenAI 的 Token 文档指出,响应中的 output 或 completion token 不只等于最终显示的文字;某些模型会产生不可见的推理、格式或工具相关 token。因此,想保留固定长度的可见答案时,不宜把输出上限卡得过紧。OpenAI 输出 token 统计说明

错误分流:输入过大、输出太短和参数不支持不是一回事

现象优先检查处理方法
请求被拒绝并提示上下文或长度超限完整输入 token、历史消息、工具 schema、文件和图片删除重复上下文、拆分内容、先摘要,或更换明确支持更大上下文的模型;模型限制需重新核实。
响应很快结束,内容像被截断输出上限、结束原因、实际 completion/output token适当提高对应端点的输出参数,同时确认仍未超过 context window 和模型最大输出限制。
返回参数不支持或 invalid parameter端点、模型、参数名称和兼容层文档Chat Completions 优先试文档对应的 max_completion_tokens;Responses 使用 max_output_tokens;旧字段仅在确认支持时使用。
请求成功但输出没有变长模型自然结束、停止条件、网关是否忽略字段检查 finish_reason 或响应状态,再用一个明显不同的预算值做对照测试;不能仅凭一次结果判定字段有效。
列得出模型但调用失败项目权限、模型可用性、区域或路由规则把“模型列表可见”和“当前项目有权调用”分开验证,查看实际调用错误。

如果输入本身过大,提高 max_completion_tokens 或 max_output_tokens 通常不能解决问题,反而可能让请求更容易触及总容量限制。正确顺序是先统计输入,再确认模型上下文窗口,最后设置输出预算。

安全边界:token 参数不能代替用量和权限控制

输出上限主要控制单次生成规模,不等于速率限制、月度额度、项目预算或并发限制。OpenAI 文档明确将请求大小限制与 API rate limits、月度使用量和支出限制区分开来。OpenAI Token 与使用限制说明

  • 服务端保存 API Key,客户端只调用你自己的后端接口。
  • 日志中隐藏 Authorization 头、完整请求体和敏感文件内容。
  • 对用户输入、历史消息和工具结果设置长度边界,避免无上限拼接上下文。
  • 重试前区分 400 参数错误、401/403 认证权限错误、429 限流和超时;参数错误不应机械重试。
  • 使用兼容网关时,分别记录请求字段、实际响应字段和 HTTP 状态,便于判断参数是被接受、忽略还是改写。

最终判断以当前模型文档、端点参考、控制台权限和本次实际响应为准。没有直接证据时,不要把某个模型的上下文容量、价格或参数支持范围推断到其他模型或服务。

常见问题

max_tokens 越大,模型就一定会输出更长吗?

不一定。它或对应的新参数只是输出预算上限,模型可能自然结束;同时还受 context window、模型最大输出限制、停止条件和兼容层行为影响。可用一个较小和一个较大的预算分别请求,并比较结束原因与 usage 字段。

为什么输入不长,仍然提示上下文超限?

因为完整请求不只包含用户文字,还可能包括系统提示、历史消息、工具定义、结构化 schema、图片、文件和消息格式开销。先使用当前端点支持的 token 统计方式核对完整输入,再逐项删除历史或工具内容定位。

Chat Completions 应该写 max_tokens 还是 max_completion_tokens?

截至 2026-11-03,当前参考文档优先使用 max_completion_tokens,max_tokens 已标为 deprecated。实际使用前仍需核对目标模型和兼容接口是否支持该字段,不能只按客户端界面名称判断。

Responses API 能不能继续使用 max_tokens?

不能默认这样做。Responses API 的官方参数名是 max_output_tokens;如果某个兼容网关声称接受 max_tokens,也要通过该网关文档和实际返回核验,不能把 Chat Completions 的字段直接复制过去。

reasoning token 会不会占用 context window?

对相关推理模型,官方文档说明 reasoning token 会计入上下文或输出相关限制,且可能不会显示在最终答案中。排查时应查看 usage 的 reasoning 细分,并为不可见生成过程预留空间。

相关阅读

参考资料

  1. OpenAI API:Conversation state 与 context window
  2. OpenAI Help:Understanding and counting tokens
  3. OpenAI API Reference:Create chat completion
  4. OpenAI API Reference:Create a model response
  5. OpenAI API:Counting tokens
0
Home
Console
FAQ
Models
Docs

What Is Easy Router and Who Is It For?

Easy Router is presented as a unified gateway for accessing multiple AI model providers through one service. The page lists more than 40 providers or models and references services such as OpenAI, Claude, Gemini, Amazon Bedrock, Google Vertex AI, and Azure AI Studio. It may be relevant to individual learners, developers testing different models, small teams, and organizations handling broader AI workflows. The available model list, access scope, and service conditions should be checked against Easy Router’s own announcements before making a decision.

How to Start and Choose an Access Method

A general starting process is to review the available models, create or access an account, confirm the applicable credit or usage arrangement, and select a connection method that fits your workflow. The page shows an API endpoint, an OpenAI-compatible Python example, and references for Node.js, cURL, and the Anthropic SDK. Before integrating, replace the sample credentials with your own, confirm the supported model name, and test a small request. Exact parameters, availability, and supported clients should be verified in the current documentation.

What to Check Before Using Easy Router

Review service announcements, model availability, usage rules, credit deductions, and any limits before sending production traffic. Keep API keys private, avoid placing credentials in public repositories, and rotate them if exposure is suspected. Check whether your prompts and outputs may contain personal, confidential, copyrighted, or regulated information, and assess whether routing them through a third-party gateway is appropriate. Claims about latency, uptime, storage, or compliance should not be treated as universal guarantees; confirm the applicable terms and notices before relying on them.

FAQ

What is Easy Router used for?

It is presented as a unified gateway for connecting with multiple AI model providers through a common access method. The page mentions more than 40 providers or models, but the current list and availability should be checked in its documentation or announcements.

Can I connect Easy Router with an OpenAI-compatible SDK?

The page shows a Python example using the OpenAI package with a custom base URL and API key. Before implementation, confirm the endpoint, model identifier, authentication rules, and supported SDK behavior in the current documentation.

What should I verify before adding credits or using it in production?

Check the current usage terms, credit deductions, model availability, account security guidance, data handling notices, and any service limitations. If information is unclear or has changed, rely on Easy Router’s latest announcements before proceeding.

This page is a third-party introduction to Easy Router and is not affiliated with it; specific details should be verified through Easy Router’s announcements.