概述
与 API 返回的结束原因一起,它们决定答案的结束位置,并告诉您它是自然结束还是被切断。正确处理它们可以防止 JSON 被截断、半成品答案和浪费的开支。
深入探讨
A language model generates one token at a time until something tells it to stop. There are three common reasons it stops. It produces its own end-of-turn token, meaning it decided the answer is complete. It reaches the maximum number of output tokens you allowed. Or it produces text that matches one of your stop sequences. Max tokens is often confused with the context window. The context window is the total number of tokens the model can handle at once, input plus output. Max tokens only limits the output of a single call. A model with a very large context window can still produce short responses if max tokens is set low, and input plus max tokens generally has to fit inside the context window. APIs report why generation ended. OpenAI's Chat Completions API returns a finish_reason such as 'stop' for a natural end or a matched stop sequence, 'length' when the output limit was hit, and 'tool_calls' when the model wants to call a tool. Anthropic's Messages API returns a stop_reason such as 'end_turn', 'max_tokens', 'stop_sequence' or 'tool_use'. Anthropic requires max_tokens on every request. For its reasoning models, OpenAI introduced max_completion_tokens, which counts hidden reasoning tokens as well as visible output. Stop sequences are useful for structured formats: ending after a closing tag, stopping before a speaker label, or cutting off after one list item. The matched text is normally not included in the returned output, so code should not expect to see it. The most common mistake is ignoring the finish reason. A truncated response can look plausible, especially prose, while structured output simply breaks. Another misconception is that a low max tokens makes the model write concisely. It does not; the model writes as it normally would and gets cut off. Ask for brevity in the prompt and use the limit as a safety cap.
现实世界的实施
摘要器将最大标记设置为 150,并且某些摘要在句子中间结束;检查完成原因会显示这些调用的“长度”,因此应用程序会提高限制并要求在提示中提供较短的摘要。
以 JSON 形式提取数据的脚本大约有 50 分之一的响应无法解析;每次失败都达到了输出限制,因此当完成原因表明输出被截断时,代码现在会以更高的限制重试。
一次生成一行对话的开发人员使用停止序列“用户:”,因此模型不会继续并写入用户的下一行。
转向推理模型的团队会发现答案为空,因为隐藏推理耗尽了输出限制,因此他们提高了限制,为推理和答案留出空间。
最大令牌和停止序列的未来
随着模型支持更长的生成,输出限制也随之增加,而限制对模式响应的结构化输出功能会减少(但不能消除)截断风险,因为足够长的响应仍然可能达到上限。推理模型使输出预算变得更加重要,因为隐藏的令牌现在共享相同的限制。参数名称和完成原因值在提供者之间有所不同,并且随着时间的推移而发生变化,因此检查当前文档并将这些详细信息包装在您自己的代码的一小层中是明智的。
付诸实践
为什么这很重要
- 成本与预算. 多年来,架构决策决定着性能和运营成本。
- 更清晰的判决. 技术教育帮助团队选择正确的堆栈,而不仅仅是最新的堆栈。
- 质量控制. 更好的工程选择可以减少生产中的可靠性事故。
留意
- 优化一项基准测试可以隐藏更广泛的系统弱点。
- 基础设施和维护成本常常被低估。
- 随着系统变得更加复杂,安全性和可观察性差距可能会扩大。
后续步骤
- 在实施之前定义延迟、质量和成本目标。
- 在实际负载和数据条件下进行基准测试。
- 仪器监控错误、漂移和用户影响。
- 在扩展之前准备回滚和事件响应路径。
不断探索
常见问题
什么是最大令牌和停止序列?
最大令牌是模型在一次响应中可以生成的令牌数量的上限,而停止序列是模型生成后立即结束生成的字符串。与 API 返回的结束原因一起,它们决定答案的结束位置,并告诉您它是自然结束还是被切断。正确处理它们可以防止 JSON 被截断、半成品答案和浪费的开支。
最大代币数量有什么限制?
最大令牌限制一次调用的输出。上下文窗口是涵盖输入和输出的单独总限制。
您的调用在 OpenAI 的 API 中返回“长度”的完成原因。发生了什么?
“length”表示由于达到输出限制而停止生成,因此响应可能不完整。
哪个 Anthropic stop_reason 表明模型决定其答案已完成?
'end_turn' 表示模型自然结束。 “max_tokens”表示它被切断,“stop_sequence”表示匹配的字符串之一。
为什么设置较低的最大标记值不能使答案简洁?
该限制是硬停止,而不是指令。要求提示简洁,并将限制作为安全上限。
匹配的停止序列通常包含在返回的文本中吗?
停止序列结束生成并且通常不包含在返回的输出中,因此您的代码不应依赖于查看它们。