English
主导航

旧版 API

Token 计数

在发送请求前获取准确的输入 Token 计数。

Token 计数可让你在将请求发送给模型之前,确定该请求将要消耗的输入 Token 数量。它可用于:

  • 优化提示词 以适应上下文限制
  • 估算成本 在进行 API 调用之前
  • 路由请求 根据大小(例如,将较小的提示词路由到更快的模型)
  • 避免意外情况 涉及图像和文件时——无需再使用基于字符的估算

The 输入 Token 计数端点 接受与以下对象相同的输入格式: Responses API。传入文本、消息、图像、文件、工具或对话——API 将返回模型接收到的准确 token 数量。

为什么要使用 Token 计数 API?

等本地分词器 tiktoken 适用于纯文本,但存在局限性:

  • 图像和文件 不受支持——类似于 characters / 4 的估算并不准确
  • 工具和 schemas 会增加难以在本地计数的 Token
  • 特定模型的行为 可能会改变 Token 化方式(例如,推理、缓存)

Token 计数 API 可以处理所有这些问题。使用与发送给 responses.create 相同的负载,即可获得准确的计数。然后将结果应用到你的消息验证或成本估算流程中。

计算基本消息中的 Token 数

纯文本输入
1
2
3
4
5
6
7
8
9
from openai import OpenAI

client = OpenAI()

response = client.responses.input_tokens.count(
    model="gpt-5",
    input="Tell me a joke."
)
print(response.input_tokens)

计算对话中的 Token 数

多轮对话
1
2
3
4
5
6
7
8
9
10
11
12
13
from openai import OpenAI

client = OpenAI()

response = client.responses.input_tokens.count(
    model="gpt-5",
    input=[
        {"role": "user", "content": "What is 2 + 2?"},
        {"role": "assistant", "content": "2 + 2 equals 4."},
        {"role": "user", "content": "What about 3 + 3?"},
    ],
)
print(response.input_tokens)

计算包含指令的 Token 数

包含系统指令的输入
1
2
3
4
5
6
7
8
9
10
from openai import OpenAI

client = OpenAI()

response = client.responses.input_tokens.count(
    model="gpt-5",
    instructions="You are a helpful assistant that explains concepts simply.",
    input="Explain quantum computing in one sentence.",
)
print(response.input_tokens)

计算包含图像的 Token 数

图像根据其大小和细节层级消耗 Token。Token 计数 API 会返回确切的数值——无需猜测。

包含图像的输入
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
from openai import OpenAI

client = OpenAI()

# Use file_id from uploaded file, or image_url for a URL
response = client.responses.input_tokens.count(
    model="gpt-5",
    input=[
        {
            "role": "user",
            "content": [
                {"type": "input_image", "image_url": "https://example.com/chart.png"},
                {"type": "input_text", "text": "Summarize this chart."},
            ],
        }
    ],
)
print(response.input_tokens)

你可以使用 file_id (来自 Files API) or image_url (URL 或 base64 数据 URL)。参见 图像与视觉 for details.

计算包含工具的 Token 数

工具定义(函数 schemas、MCP 服务器等)会向上下文中添加 Token。将它们与你的输入一起计算:

包含函数工具的输入
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
from openai import OpenAI

client = OpenAI()

response = client.responses.input_tokens.count(
    model="gpt-5",
    tools=[
        {
            "type": "function",
            "name": "get_weather",
            "description": "Get the current weather in a location",
            "parameters": {
                "type": "object",
                "properties": {"location": {"type": "string"}},
                "required": ["location"],
            },
        }
    ],
    input="What is the weather in San Francisco?",
)
print(response.input_tokens)

计算包含文件的 Token 数

文件输入——目前支持 PDF。像传递给 file_id, file_url, or file_data 那样传入 responses.create。Token 数量反映了模型的完整处理输入。

API 参考

有关完整的参数和响应结构,请参见 计算输入 Token API 参考。端点为:

POST /v1/responses/input_tokens

The response includes input_tokens (整数)和 object: "response.input_tokens".