当前位置: 首页 > news >正文

openai接口参数max_tokens改名max-completion-tokens?

文章目录

    • 关于max_tokens参数
    • max_tokens改max-completion-tokens?
    • 控制 OpenAI 模型响应的长度

关于max_tokens参数

大模型 API(比如 OpenAI、DeepSeek、Moonshot 等)都是按照“Token 数量”来计费。
控制内容:控制一次请求返回的“全部 token 数量”上限,包括提示(prompt)和生成的内容(completion)。
举例:你发送一个 100 tokens 的 prompt,max_tokens 设为 200,则最多会生成 100 tokens 的回答(100 prompt + 100 completion = 200)。

如果需要输出的内容超出Max_Tokens的最大值,那就需要做好可能被聊天大模型主动截断的准备。

一般,在多轮对话中,历史对话的输入输出都会作为新一轮的模型输入 token 进行计费。

Context Length(上下文长度):
定义:“context length”指的是模型在进行一次特定的推理时可以考虑的最大令牌数。换句话说,它是模型在生成响应之前可以“回顾”和“理解”的输入内容的长度。

作用:这个参数决定了模型能够记住和参考多少先前的信息。较长的上下文长度允许模型在生成响应时利用更多的历史信息。

OpenAI 官方解释更名是为了让参数含义更加清晰,max_completion_tokens 明确表示只限制“生成内容(completion)”部分,而不是输入(prompt)+输出(completion)的总token数,避免长期因 max_tokens 命名带来的困惑。过去,有用户常误解 max_tokens 的精确作用,导致不必要的Bug和支持请求。

max_tokens改max-completion-tokens?

openapi开发者官方论坛:https://community.openai.com/t/why-was-max-tokens-changed-to-max-completion-tokens/938077/1

We are doing this because max_tokens previously meant both the number of tokens we generated (and billed you for) and the number of tokens you got back in your response. With the o1 models, this is no longer true — we generate more tokens than we return, as reasoning tokens are not visible. Some clients may have depended on the previous behavior and written code that assumes that max_tokens equals usage.completion_tokens or the number of tokens they received. To avoid breaking these clients, we are requiring you opt-in to the new behavior by using a new parameter.
我们这样做是因为 max_tokens 之前既表示我们生成的 tokens 数量(以及您为此支付的费用),也表示您在响应中收到的 tokens 数量。对于 o1 模型,情况已不再如此——我们生成的 tokens 比返回的 tokens 多,因为推理 tokens 是不可见的。某些客户端可能依赖于之前的行为,并编写了假设 max_tokens 等于 usage.completion_tokens 或他们收到的 tokens 数量的代码。为了避免破坏这些客户端,我们要求您通过使用一个新参数来选择启用新行为。

More documentation here: https://platform.openai.com/docs/guides/reasoning/controlling-costs
更多信息请参考: https://platform.openai.com/docs/guides/reasoning/controlling-costs

兼容性影响
讨论区用户认为,仅仅为了术语准确就引入breaking change,确实带来定制代码或第三方工具的兼容问题,特别是自动化构建和API适配方面。
但OpenAI表示如此改动是为了长远的API直观性、预期一致性和文档优化。

token限制机制
技术讨论还涉及token配额的计数方式,OpenAI进一步clarify:模型的实际生成token不会超过指定的max_completion_tokens数量。输入太长时会提示报错而不是截断生成。

控制 OpenAI 模型响应的长度

官网文档:https://help.openai.com/en/articles/5072518-controlling-the-length-of-openai-model-responses

You can control the length of a model’s output using several techniques depending on your goals and the model you’re working with.
您可以使用多种技术来控制模型输出的长度,具体取决于您的目标和您正在使用的模型。

Set a Maximum Token Limit
设置最大令牌限制
Use the max_completion_tokens parameter to limit how many tokens the model will generate.
使用 max_completion_tokens 参数来限制模型将生成的令牌数量。

Playground: This is labeled as “Maximum Length”.
Playground:标记为 “Maximum Length”。

API: 应用程序接口 :

For reasoning models like o3, o4-mini, and gpt-4.1, use max_completion_tokens.
对于 o3、o4-mini 和 gpt-4.1 等推理模型 ,请使用 max_completion_tokens

For earlier models, max_tokens still works and behaves the same as before.
对于早期的模型 ,max_tokens 的工作方式和行为与以前相同。

Important: 重要 :
max_tokens is deprecated for newer reasoning models.
max_tokens 对于较新的推理模型已弃用 。

You can review the model reference page for the token behavior of specific models.
您可以查看模型参考页面 ,了解特定模型的令牌行为。

相关文章:

  • Google LLM prompt engineering(谷歌提示词工程指南)
  • 第一章 例行性工作(任务计划)
  • 线性动态规划3
  • 软考 系统架构设计师系列知识点之杂项集萃(56)
  • Unity中AssetBundle使用整理(一)
  • Java NIO 文件处理接口
  • GO语言-导入自定义包
  • Linux基础io知识
  • [超详细,推荐!!!]前端性能优化策略详解
  • 如何实现并运用责任链模式
  • 当虚拟照进现实——《GTA6》如何重新定义开放世界的可能性‌
  • Python3安装HTMLTestRunner
  • 【Tools】Visual Studio使用经验介绍(包括基本功能、远程调试、引入第三方库等等)
  • 使用lldb看看Rust的HashMap
  • 信息系统项目管理师-软考高级(软考高项)​​​​​​​​​​​2025最新(十七)
  • 害怕和别人发生冲突怎么办? --deepseek
  • lampiao靶场渗透
  • springboot3学习
  • Problem E: 实现冒泡排序(内存优化)
  • Linux常见指令解析(三)
  • 陕西河南山西等地将现“干热风”灾害,小麦产区如何防范?
  • 梅花奖在上海丨陈丽俐“婺剧折戏专场”:文戏武做,武戏文唱
  • 最高降九成!特朗普签署降药价行政令落地存疑,多家跨国药企股价收涨
  • 人民网评:守护健康证的“健康”,才有舌尖上的安全
  • 国内大模型人才大战打响!大厂各出奇招
  • 未来之城湖州,正在书写怎样的城市未来