OpenAI API input tokens 和 output tokens 是什么?

更新于 2026-10-18 · easyrouter.cyou

简答:OpenAI API 中,input tokens 是发送给模型的内容,output tokens 是模型生成的内容;两者通常分别按模型价格计费。Responses API 可从 usage.input_tokens、usage.output_tokens 和 usage.total_tokens 读取实际用量,推理模型产生的 reasoning tokens 通常计入 output tokens。

定义:Token 是模型处理文本时使用的基本计量单位,input tokens 表示请求输入,output tokens 表示模型输出,二者不是简单的字数统计。

先理解 input tokens 和 output tokens 的区别

Input tokens 是请求发送给模型的输入内容,包括系统指令、开发者指令、用户问题、对话历史、工具定义、文件或图片等经过 API 处理的输入。它不只等于用户最后输入的那一句话。

Output tokens 是模型生成的内容,包括返回给你的文本、结构化输出、工具调用参数,以及推理模型在生成答案前使用的内部 reasoning tokens。推理 tokens 通常不会直接出现在可见回答里,但会计入输出用量。

因此,下面两种情况都可能让实际用量高于直觉:一是请求携带了很长的历史对话或工具定义;二是模型为了完成任务产生了较多内部推理。OpenAI 的说明还指出,同一段文字的 token 数量会受到模型、编码方式、语言、空格和大小写影响,不能把 token 机械地等同于汉字数或单词数。可参考 OpenAI 对 token 计算和 usage 字段的说明。

对常见英文文本,可以把 1 token 约等于 4 个字符、约等于四分之三英文单词作为粗略估算;这只是估算,中文、代码、JSON 和混合语言文本可能有明显差异。精确计算时,应使用官方 Tokenizer 或在请求完成后读取 API 返回的 usage。

usage 字段怎么看:Responses API 与 Chat Completions API

不同接口的字段名称不完全相同。Responses API 通常使用 input_tokens、output_tokens 和 total_tokens;Chat Completions 常见字段则是 prompt_tokens、completion_tokens 和 total_tokens。排查费用时,应先确认自己调用的是哪个接口,再按该接口的 usage 结构读取。

字段含义排查重点
input_tokens本次请求输入的 token 数检查历史消息、系统提示词、工具定义和文件是否过长
output_tokens模型生成的输出 token 数检查回答长度、输出上限和推理模型的内部推理消耗
total_tokens输入与输出等用量的汇总字段用于快速记录,但计费仍应按模型价格表拆分查看
cached_tokens被提示词缓存复用的输入 token是否命中缓存以及缓存输入价格,以当前模型价格为准
reasoning_tokens推理模型使用的内部推理 token通常属于 output tokens,不等于可见回答字数

缓存输入不是“额外重复计费”的同义词。命中的缓存输入通常使用单独的 cached input 费率;某些模型还可能存在 cache write 费率。OpenAI 的提示词缓存文档说明,缓存价格和写入规则取决于模型,应结合实际价格页和返回的 usage 字段判断。查看 Prompt Caching 官方指南 与 OpenAI API 官方价格页。

最小可运行示例:用 Python 读取 input 和 output tokens

下面示例使用官方模型页面中可核实的 gpt-5.4,通过 Responses API 发起一次文本请求,并打印输入、输出和总 token 数。模型名称、可用接口和参数可能变化,正式使用前应以当前官方模型文档为准。

pip install openai

macOS 或 Linux 可以这样设置 API Key:

export OPENAI_API_KEY="你的_API_Key"

Windows PowerShell 可以这样设置:

$env:OPENAI_API_KEY="你的_API_Key"
from openai import OpenAI

client = OpenAI()

response = client.responses.create(
    model="gpt-5.4",
    input="用一句话解释 input tokens 和 output tokens 的区别。"
)

print(response.output_text)
print("input_tokens:", response.usage.input_tokens)
print("output_tokens:", response.usage.output_tokens)
print("total_tokens:", response.usage.total_tokens)

如果代码报模型不可用、权限不足或参数不支持,不要直接把错误归因于 token。先检查 API Key 是否有效、项目是否有该模型权限、模型名称是否仍在官方列表中,以及 SDK 是否为兼容版本。GPT-5.4 官方模型说明可用于核对模型能力、上下文窗口和价格入口。

如何根据 token 用量估算 API 成本

最基本的估算方式是:

预计成本 = 输入 token 数 ÷ 1,000,000 × 输入单价 + 输出 token 数 ÷ 1,000,000 × 输出单价

如果请求包含缓存命中、缓存写入、批处理、不同服务层级、区域处理或内置工具,还要按照对应模型和服务层级的官方费率单独计算。不能只看可见回答的字数,也不能把所有模型套用同一组输入和输出价格。

实际项目建议每次请求至少记录以下信息:请求日期、模型名、接口类型、input_tokens、output_tokens、cached_tokens、请求是否重试,以及是否调用了工具。这样遇到费用异常时,可以区分是输入变长、输出变长、缓存未命中、推理消耗增加,还是失败重试造成的额外请求。

  1. 先读取响应中的 usage,而不是用字符数反推。
  2. 再到当前官方价格页确认该模型的 input、cached input、cache write 和 output 费率。
  3. 最后检查 Batch、Fast、Flex、Priority、Scale Tier 或区域处理等服务层级是否改变了费率。

如果只是想预估一段纯文本的大小,可以使用 OpenAI Tokenizer;如果要计算完整 Responses 请求,还应考虑消息结构、工具 schema、图片、文件和对话边界等额外输入。

控制 input 和 output tokens 的实用方法

  • 减少无效输入:不要把完整历史对话、重复的系统提示词或无关工具定义永久附加到每次请求中。
  • 限制输出长度:根据接口和模型使用支持的输出上限参数,例如 Responses API 中的 max_output_tokens,但要先核对目标模型是否支持。
  • 区分可见回答和推理消耗:推理模型的可见文本很短,并不代表 output tokens 一定很少。
  • 观察缓存:将稳定、重复的提示词前缀放在前面,查看实际 cached tokens 和 cache write 相关字段。
  • 按模型记录:同一段文本在不同模型或编码方式下,token 数可能不同,成本比较必须使用真实请求数据。

出现 429、超时或流式中断时,也要检查客户端是否自动重试。每次重试都可能产生新的输入和输出用量,因而应把请求 ID、重试次数和 usage 一起记录。

常见问题

input tokens 是不是用户输入的字数?

不是。input tokens 包括请求中的系统指令、开发者指令、用户内容、历史消息、工具定义以及部分文件或图片处理内容,字数只能做非常粗略的估计。

output tokens 是不是等于回答显示出来的字数?

不是。output tokens 可能包含结构化输出、工具调用参数和推理模型的内部 reasoning tokens,因此可见回答很短时,实际 output tokens 仍可能较高。

cached tokens 会不会再次按完整 input 价格计算?

不能一概而论。缓存命中通常按模型的 cached input 费率处理,部分模型还可能有 cache write 费率,应以当前官方价格页和实际 usage 为准。

如何准确知道一次请求用了多少 token?

请求完成后直接读取响应中的 usage 字段;Responses API 重点看 input_tokens、output_tokens 和 total_tokens,Chat Completions 则通常看 prompt_tokens、completion_tokens 和 total_tokens。

为什么同一句中文和英文的 token 数不同?

token 切分受语言、编码、空格、大小写和模型影响。英文可以用官方给出的粗略比例估算,但中文和代码不宜直接套用 1 token 等于固定字数的说法。

相关阅读

参考资料

  1. OpenAI Help Center:Understanding and counting tokens
  2. OpenAI API Pricing
  3. OpenAI Prompt Caching Guide
  4. OpenAI Tokenizer
  5. OpenAI GPT-5.4 Model Documentation
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.