Token 完全科普:LLM 调用的消耗与计费
从零理解 Token 是什么、大模型调用过程中 Token 如何消耗、以及以千问(Qwen)系列为例的计费全貌
一、Token 是什么?
Token 是大语言模型(LLM)处理文本的最小基本单位。可以把它理解为模型”阅读”和”写作”时使用的”原子词汇”——模型不会直接处理汉字或英文字母,而是将它们拆解为 Token 序列。
Token 切分示例
| 原始文本 | Token 数量 | Token 切分结果 |
|---|---|---|
人工智能(中文) | 约 2-4 个 | 人工 + 智能 |
Hello World(英文) | 约 2-3 个 | Hello + World |
transformer(英文长词) | 约 2-3 个 | trans + former |
123456(数字) | 1-3 个 | 123 + 456 |
print("hello")(代码) | 约 5-6 个 | print + ( + " + hello + " + ) |
为什么需要 Token?
- 模型只能处理数字:LLM 内部是神经网络,所有输入必须先转化为数值向量,Token 就是文本到数字的桥梁
- 计费的基础单元:API 按 Token 数量收费,用多少付多少,公平透明
- 上下文窗口的度量:模型能一次”记住”的内容上限以 Token 数衡量(如 128K Token)
二、LLM 调用过程中 Token 如何消耗?
一次完整的 LLM 调用,Token 消耗发生在三个环节:
2.1 系统提示词(System Prompt)
开发者预设的”角色设定”,如”你是一个专业的法律顾问”。每次调用都会重复计入输入 Token。系统提示词越长,每次对话的固定成本越高。
system_prompt = "你是一个地质建模专家,请用专业术语回答。"
# 这条提示词约 10-15 个 Token,每次对话都会消耗
2.2 输入 Token(Input / Prompt Tokens)
用户提出的问题或给模型的所有文本内容,包括:
- 用户消息正文
- 对话历史(多轮对话时,之前的问答会重新发送)
- 上传的文件内容(如 PDF 摘要、代码文件)
- 工具调用结果(Function Calling 的返回值)
2.3 输出 Token(Output / Completion Tokens)
模型生成的回复文本。输出 Token 按实际生成数量计算,受 max_tokens 参数限制。
2.4 完整消耗流程
┌─────────────────────────────────────────────────┐
│ 一次 API 调用的 Token 账单 │
├─────────────────────────────────────────────────┤
│ 输入 Token(你发给模型的) │
│ ├── 系统提示词(System Prompt) │
│ ├── 用户消息(当前问题) │
│ ├── 对话历史(之前的多轮问答) │
│ └── 工具/文件上下文(如有) │
├─────────────────────────────────────────────────┤
│ 输出 Token(模型生成的回复) │
│ └── 模型回答的全部内容 │
├─────────────────────────────────────────────────┤
│ 总消耗 = 输入 Token + 输出 Token │
│ 总费用 = 输入 Token × 输入单价 + 输出 Token × 输出单价 │
└─────────────────────────────────────────────────┘
三、Token 计费的基本公式
计算示例
假设使用千问 Qwen-Long 模型(输入 0.5 元/百万Token,输出 2 元/百万Token):
# 用户提问:"请解释什么是 Token"(约 300 Token)
# 模型回复了详细解释(约 900 Token)
输入费用 = 300 / 1,000,000 × 0.5 = ¥0.00015
输出费用 = 900 / 1,000,000 × 2.0 = ¥0.0018
总费用 = ¥0.00195(约 0.2 分钱)
# 一天调用 1000 次 ≈ ¥1.95
# 一天调用 10 万次 ≈ ¥195
max_tokens)比精简输入更省钱。
多轮对话的 Token 累积
第 1 轮:输入 500 Token → 输出 800 Token → 累计 1300
第 2 轮:输入 500+1300=1800 Token → 输出 600 Token → 累计 3700
第 3 轮:输入 500+3700=4200 Token → 输出 500 Token → 累计 8400
...
第 10 轮:输入可能已超过 20000 Token
# 每次对话增长,输入 Token 呈线性增长
四、千问(Qwen)系列计费详解
阿里云千问系列按 Token 计费,价格因模型、输入长度、部署区域和调用模式而异。以下为 2026 年中国内地(北京地域)主要模型价格参考:
| 模型 | 输入单价(/百万Token) | 输出单价(/百万Token) | 特点 |
|---|---|---|---|
| Qwen-Long | ¥0.5 | ¥2.0 | 长文本专用,性价比最高 |
| Qwen3.5-Plus | ¥0.8 | ¥4.8 | 主力模型,多模态,0-128K 上下文 |
| Qwen3.5-Plus (128K-256K) | ¥2.0 | ¥12.0 | 超长输入区间 |
| Qwen3.5-Plus (256K-1M) | ¥4.0 | ¥24.0 | 百万级上下文区间 |
| Qwen3-Max | ¥2.5 | ¥10.0 | 旗舰模型,0-32K 上下文 |
| Qwen3-Max (32K-128K) | ¥4.0 | ¥16.0 | 中长文本区间 |
| Qwen3-Max (128K-252K) | ¥7.0 | ¥28.0 | 超长文本区间 |
| Qwen3-Omni-Flash | ¥1.8 (文本) | ¥6.9 | 多模态(含音频/图片/视频输入) |
| Qwen3-Omni-Flash (音频输入) | ¥15.8 | ¥12.7 | 音频输入价格更高 |
省钱技巧
不要求实时响应的任务可提交批量请求,输入和输出单价均为实时推理的 50%。适合离线分析、数据标注等场景。
高频重复的系统提示词或文档可缓存,命中缓存时仅输入 Token 享折扣。适合固定 Prompt 的多次调用。
新用户开通阿里云百炼平台即送各模型 100 万 Token 免费额度(90 天有效),还可领取超 7000 万 Token。
简单任务用 Qwen-Long(最低价),复杂推理用 Qwen-Max。不要用旗舰模型处理日常闲聊。
官方定价页:aliyun.com/product/tongyi | 百炼平台:aliyun.com/product/bailian
五、影响 Token 消耗的额外因素
| 因素 | 影响 | 说明 |
|---|---|---|
| 思考模式(Thinking) | 输出 Token 大幅增加 | 模型内部推理过程也计入输出 Token,可能翻倍甚至更多 |
| Function Calling | 输入 Token 增加 | 工具定义和返回结果都计入输入 Token |
| 多模态输入 | 单价更高 | 图片、音频、视频按各自单价计费,且通常远高于纯文本 |
| max_tokens 设置 | 限制输出上限 | 设置过大会导致非必要的高消耗,建议根据实际需求设定合理值 |
| 流式输出(Streaming) | 不减少总消耗 | 但可中途停止生成,避免不必要输出 |
思考模式的 Token 开销
# 普通模式
输入 500 Token → 输出 800 Token → 总消耗 1300 Token
# 思考模式(开启深度推理)
输入 500 Token → 思考过程 1200 Token + 最终回答 600 Token
→ 输出 1800 Token → 总消耗 2300 Token
# 费用可能是普通模式的 2-3 倍
六、如何计算和监控 Token 消耗
6.1 API 返回值中查看
每次调用 API 后,响应中会包含 usage 字段,直接显示本次消耗:
{
"usage": {
"prompt_tokens": 350, // 输入 Token
"completion_tokens": 820, // 输出 Token
"total_tokens": 1170 // 总消耗
}
}
6.2 使用 Tokenizer 预先估算
from transformers import AutoTokenizer
# 加载千问的 Tokenizer
tokenizer = AutoTokenizer.from_pretrained(
"Qwen/Qwen2.5-7B-Instruct", trust_remote_code=True
)
# 估算输入 Token 数
text = "请用通俗的语言解释什么是 Token"
tokens = tokenizer.encode(text)
print(f"输入 Token 数: {len(tokens)}")
# 输出: 输入 Token 数: ~15-20
6.3 成本计算函数
def calc_cost(input_tokens, output_tokens,
input_price_per_million=0.8,
output_price_per_million=4.8):
"""计算千问 3.5-Plus 的单次调用费用"""
input_cost = (input_tokens / 1_000_000) * input_price_per_million
output_cost = (output_tokens / 1_000_000) * output_price_per_million
return round(input_cost + output_cost, 6)
# 示例
cost = calc_cost(500, 1200)
print(f"费用: ¥{cost}") # 费用: ¥0.00616
七、省钱最佳实践
- 精简系统提示词:去掉不必要的背景说明,每条多出的 Token 都会在每次对话中重复消耗
- 控制对话轮次:长对话的历史累积是隐藏的成本大头,必要时可定期”重置对话”
- 设置合理的 max_tokens:不要默认设 4096 或 8192,按实际回复长度需求设定
- 简单任务降级模型:日常问答用 Qwen-Long(0.5 元/百万Token),复杂推理才用 Qwen-Max
- 批量任务用 Batch 模式:离线处理大批量数据时,Batch 模式可节省 50% 费用
- 启用流式输出:用户可中途停止生成,避免模型产出不需要的冗长回复
- 高频问答做缓存:相同问题直接返回缓存结果,零 Token 消耗
- 利用免费额度:开通百炼平台领取 7000 万+ Token 免费额度,先试用再付费
核心要点总结
Complete Token Guide: LLM Consumption & Pricing
Understand what tokens are, how they are consumed during LLM calls, and a complete pricing overview using the Qwen model series as an example
1. What Is a Token?
A Token is the smallest basic unit that a Large Language Model (LLM) uses to process text. Think of it as the “atomic vocabulary” the model uses to “read” and “write” — the model doesn’t process Chinese characters or English letters directly, but breaks them into token sequences.
Tokenization Examples
| Original Text | Token Count | Token Breakdown |
|---|---|---|
artificial intelligence | ~2-3 | art + ificial + intelligence |
Hello World | ~2-3 | Hello + World |
transformer | ~2-3 | trans + former |
123456 | 1-3 | 123 + 456 |
print("hello") | ~5-6 | print + ( + " + hello + " + ) |
Why Tokens?
- Models only process numbers: LLMs are neural networks; all input must be converted to numerical vectors first. Tokens bridge text to numbers.
- Unit of billing: APIs charge by token count — pay for what you use, transparent and fair.
- Context window metric: The maximum amount of content a model can “remember” at once is measured in tokens (e.g., 128K tokens).
2. How Tokens Are Consumed During an LLM Call
A complete LLM call consumes tokens across three stages:
2.1 System Prompt
The developer-defined “role setting,” such as “You are a professional legal advisor.” It is re-counted every call into input tokens. The longer the system prompt, the higher the fixed cost per conversation.
system_prompt = "You are a geological modeling expert. Use professional terms."
# This prompt is ~10-15 tokens, consumed every conversation
2.2 Input Tokens (Prompt Tokens)
All text content sent to the model, including:
- User message body
- Conversation history (prior Q&A rounds are re-sent in multi-turn chats)
- Uploaded file content (e.g., PDF excerpts, code files)
- Function calling results (tool return values)
2.3 Output Tokens (Completion Tokens)
The text the model generates in response. Output tokens are counted based on actual generation, limited by the max_tokens parameter.
2.4 Complete Consumption Flow
┌─────────────────────────────────────────────────┐
│ Token Bill for One API Call │
├─────────────────────────────────────────────────┤
│ Input Tokens (what you send to the model) │
│ ├── System Prompt │
│ ├── User Message (current question) │
│ ├── Conversation History (prior Q&A rounds) │
│ └── Tool/File Context (if any) │
├─────────────────────────────────────────────────┤
│ Output Tokens (model's response) │
│ └── Full content of model's answer │
├─────────────────────────────────────────────────┤
│ Total = Input Tokens + Output Tokens │
│ Cost = Input × Input Price + Output × Output Price│
└─────────────────────────────────────────────────┘
3. Basic Token Pricing Formula
Calculation Example
Using Qwen-Long (input ¥0.5/million tokens, output ¥2.0/million tokens):
# User asks: "Explain what a token is" (~300 tokens)
# Model responds with detailed explanation (~900 tokens)
Input cost = 300 / 1,000,000 × 0.5 = ¥0.00015
Output cost = 900 / 1,000,000 × 2.0 = ¥0.0018
Total cost = ¥0.00195 (~0.2 Chinese cents)
# 1,000 calls/day ≈ ¥1.95
# 100,000 calls/day ≈ ¥195
max_tokens) saves more than trimming input.
Multi-Turn Token Accumulation
Round 1: Input 500 → Output 800 → Cumulative 1300
Round 2: Input 500+1300=1800 → Output 600 → Cumulative 3700
Round 3: Input 500+3700=4200 → Output 500 → Cumulative 8400
...
Round 10: Input may exceed 20,000 tokens
# Input tokens grow linearly with each turn
4. Qwen Series Pricing Breakdown
Alibaba Cloud’s Qwen series charges by token, with prices varying by model, input length, deployment region, and call mode. Below is the pricing reference for China mainland (Beijing region) as of 2026:
| Model | Input (per M tokens) | Output (per M tokens) | Features |
|---|---|---|---|
| Qwen-Long | ¥0.5 | ¥2.0 | Long-text specialist, best value |
| Qwen3.5-Plus | ¥0.8 | ¥4.8 | Main model, multimodal, 0-128K context |
| Qwen3.5-Plus (128K-256K) | ¥2.0 | ¥12.0 | Extended input range |
| Qwen3.5-Plus (256K-1M) | ¥4.0 | ¥24.0 | Million-token context range |
| Qwen3-Max | ¥2.5 | ¥10.0 | Flagship model, 0-32K context |
| Qwen3-Max (32K-128K) | ¥4.0 | ¥16.0 | Mid-long text range |
| Qwen3-Max (128K-252K) | ¥7.0 | ¥28.0 | Ultra-long text range |
| Qwen3-Omni-Flash | ¥1.8 (text) | ¥6.9 | Multimodal (audio/image/video input) |
| Qwen3-Omni-Flash (audio input) | ¥15.8 | ¥12.7 | Higher audio input pricing |
Cost-Saving Tips
For non-real-time tasks, submit batch requests. Both input and output pricing is 50% of real-time inference. Suitable for offline analysis and data labeling.
High-frequency system prompts or documents can be cached. When the cache is hit, input tokens get a discount. Ideal for repeated calls with fixed prompts.
New users of Alibaba Cloud Bailian Platform receive 1 million tokens per model (90 days) plus over 70 million additional free tokens.
Use Qwen-Long (lowest price) for simple tasks; Qwen-Max for complex reasoning. Don’t use the flagship model for casual chat.
Official pricing: aliyun.com/product/tongyi | Bailian Platform: aliyun.com/product/bailian
5. Additional Factors Affecting Token Consumption
| Factor | Impact | Description |
|---|---|---|
| Thinking Mode | Output tokens increase significantly | Internal reasoning process is also counted as output, potentially doubling or more |
| Function Calling | Input tokens increase | Tool definitions and return values are counted as input tokens |
| Multimodal Input | Higher unit price | Images, audio, and video are billed at their own rates, usually much higher than plain text |
| max_tokens setting | Caps output | Setting it too high leads to unnecessary consumption; set a reasonable value based on actual needs |
| Streaming | Doesn’t reduce total | But allows mid-generation stopping to avoid unnecessary output |
Thinking Mode Token Overhead
# Normal mode
Input 500 tokens → Output 800 tokens → Total 1300 tokens
# Thinking mode (deep reasoning enabled)
Input 500 tokens → Reasoning 1200 tokens + Final answer 600 tokens
→ Output 1800 tokens → Total 2300 tokens
# Cost may be 2-3× of normal mode
6. How to Calculate and Monitor Token Consumption
6.1 Check API Response
Every API response includes a usage field showing the current call’s consumption:
{
"usage": {
"prompt_tokens": 350, // Input tokens
"completion_tokens": 820, // Output tokens
"total_tokens": 1170 // Total
}
}
6.2 Pre-Estimate with Tokenizer
from transformers import AutoTokenizer
# Load Qwen's tokenizer
tokenizer = AutoTokenizer.from_pretrained(
"Qwen/Qwen2.5-7B-Instruct", trust_remote_code=True
)
# Estimate input token count
text = "Explain what a token is in simple terms"
tokens = tokenizer.encode(text)
print(f"Input token count: {len(tokens)}")
# Output: ~15-20
6.3 Cost Calculation Function
def calc_cost(input_tokens, output_tokens,
input_price_per_million=0.8,
output_price_per_million=4.8):
"""Calculate single-call cost for Qwen 3.5-Plus"""
input_cost = (input_tokens / 1_000_000) * input_price_per_million
output_cost = (output_tokens / 1_000_000) * output_price_per_million
return round(input_cost + output_cost, 6)
# Example
cost = calc_cost(500, 1200)
print(f"Cost: ¥{cost}") # Cost: ¥0.00616
7. Best Practices for Saving Tokens
- Streamline system prompts: Remove unnecessary context; every extra token is consumed in every call
- Control conversation rounds: Long conversation history accumulation is a hidden cost driver; reset conversations periodically
- Set reasonable max_tokens: Don’t default to 4096 or 8192; set based on actual response length needs
- Downgrade model for simple tasks: Use Qwen-Long (¥0.5/M tokens) for daily Q&A, Qwen-Max only for complex reasoning
- Use Batch mode for bulk tasks: Processing large data offline? Batch mode saves 50%
- Enable streaming: Users can stop mid-generation, avoiding unnecessary lengthy responses
- Cache high-frequency Q&A: Return cached results directly for identical questions — zero token cost
- Use free quotas: Activate the Bailian Platform to claim 70M+ free tokens for trial
