Token 计算详解
什么是 Token
Token 是大语言模型处理文本的最小基本单位。模型不直接处理原始文本,而是将文本转换为 token 序列进行处理。
Tokenizer(分词器)
Tokenizer 负责将文本转换为 token 序列:
Input text → Tokenizer → Token sequence
"今天天气很好" → [192, 3847, 2093, 3847, 452, 2398] # Chinese tokenization demo (6 tokens)Detokenizer(反分词器)
Detokenizer 负责将 token 序列转换回文本:
Token sequence → Detokenizer → Output text
[192, 3847, 2093, 3847, 452, 2398] → "今天天气很好" # Chinese tokenization demo (6 tokens)Token 计算规则
中英文差异
| 文本类型 | 平均 Token 数 | 示例 |
|---|---|---|
| 英文单词 | 1 Token ≈ 0.75 单词 | "hello" → 1 token |
| 中文汉字 | 1 Token ≈ 1-2 字符 | "今天" → 2 tokens |
| 标点符号 | 1 个 ≈ 1 Token | "。" → 1 token |
| 空格 | 英文空格计入 | "hello world" → 2 tokens |
实际计算示例
English: "hello world"
Tokens: [15339, 1917]
Count: 2 tokens
Chinese: "今天天气很好" # Chinese tokenization demo (6 tokens)
Tokens: [192, 3847, 2093, 3847, 452, 2398]
Count: 6 tokens常见 Token 计算工具
平台内置计算
在 天枢AI-TokenHub 平台的 API 响应中,会返回实际消耗的 token 数量:
json
{
"id": "chatcmpl-xxx",
"model": "gpt-4o",
"usage": {
"prompt_tokens": 28, // input consumption
"completion_tokens": 45, // output consumption
"total_tokens": 73 // total consumption
}
}常用 Token 计算公式
英文文本
Token count ≈ characters ÷ 4
Token count ≈ words × 1.33中文文本
Token count ≈ characters × 1.3 ~ 1.5在线计算工具
| 工具 | 网址 | 说明 |
|---|---|---|
| OpenAI Tokenizer | platform.openai.com/tokenizer | 官方工具 |
| AiToken | aitoken.fly.dev | 在线计算 |
天枢AI-TokenHub 平台 Token 计费
计费组成
| 类型 | 说明 | 计费 |
|---|---|---|
| Prompt Tokens | 输入文本消耗 | ✅ 计费 |
| Completion Tokens | 输出文本消耗 | ✅ 计费 |
| Total Tokens | 总消耗 | ✅ 计费 |
计费示例
python
# User input
user_input = "Please write a poem about spring"
# Assume it converts to 14 tokens
# Model output
model_output = "Spring breeze brushes the face, all things revive.\nFlowers bloom everywhere, greenery abounds."
# Assume output converts to 28 tokens
# Total: 14 + 28 = 42 tokens平台 Token 计算页面
在 天枢AI-TokenHub 后台的「用量统计」页面,可以查看:
- 每日/每周/每月 Token 消耗明细
- 各模型的 Token 消耗占比
- Token 消耗趋势图表
如何节省 Token
1. 精简 Prompt
python
# ❌ Not recommended - redundant description
messages = [
{"role": "user", "content": "Please act as a very professional and experienced programmer to help me write a piece of code. The function of this code is to calculate the sum of two numbers. Please write it in detail, including comments and error handling"}
]
# ✅ Recommended - concise and clear
messages = [
{"role": "user", "content": "Write a Python addition function with error handling"}
]2. 使用系统消息
将固定的角色设定放在系统消息中:
python
messages = [
{"role": "system", "content": "You are a professional Python programmer"},
{"role": "user", "content": "Write a sorting algorithm"}
]3. 控制回复长度
明确指定回复格式和长度:
python
{"role": "user", "content": "Explain quantum computing in one sentence"}4. 多轮对话优化
python
# ❌ Repeating context every time
messages = [
{"role": "user", "content": "Explain what machine learning is, including its definition, principles, and application scenarios"},
{"role": "assistant", "content": "Machine learning is..."},
{"role": "user", "content": "Please explain the definition, principles, and application scenarios of machine learning in more detail"}
]
# ✅ Leverage historical context
messages = [
{"role": "user", "content": "Explain what machine learning is, including its definition, principles, and application scenarios"},
{"role": "assistant", "content": "Machine learning is..."},
{"role": "user", "content": "Give a concrete example"} # Automatically understands context
]Token 与上下文窗口
Token 数量影响上下文
Context window: 4096 tokens
Input: 3000 tokens
Available output: 4096 - 3000 = 1096 tokens
Input: 4000 tokens
Available output: 4096 - 4000 = 96 tokens ⚠️ Almost no room for output平台上下文窗口
| 模型 | 上下文窗口 | 说明 |
|---|---|---|
| GPT-4o | 128K Tokens | 128,000 |
| Claude 3.5 Sonnet | 200K Tokens | 200,000 |
| Gemini 3 Pro | 1M Tokens | 1,000,000 |
常见问题
Q: 为什么我的输入很短但 Token 很多?
可能原因:
- 包含特殊字符或代码
- 中文标点符号较多
- 包含 URL 或邮箱地址
Q: Token 计算有误差吗?
不同模型使用不同的 Tokenizer,计算结果可能略有差异。建议以 API 响应返回的 usage 字段为准。
Q: 如何准确计算 Token?
最准确的方式是通过 API 响应获取:
python
response = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "Hello"}]
)
# Get the exact count from the response
print(f"Total Tokens: {response.usage.total_tokens}")