Token 完全科普:LLM 调用的消耗与计费

Token 完全科普:LLM 调用消耗与计费指南

一、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 + " + )
经验法则 1 个中文字符 ≈ 1.5–2 个 Token  |  1 个英文单词 ≈ 1–1.5 个 Token  |  1000 个 Token ≈ 约 750 个英文单词 ≈ 约 500 个中文汉字

为什么需要 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 的返回值)
多轮对话的隐藏成本 每次新对话,全部历史消息会被重新发送。第 10 轮对话时,前 9 轮的问答已累计成为大量输入 Token。这就是为什么长对话越来越贵。

2.3 输出 Token(Output / Completion Tokens)

模型生成的回复文本。输出 Token 按实际生成数量计算,受 max_tokens 参数限制。

2.4 完整消耗流程

┌─────────────────────────────────────────────────┐
│  一次 API 调用的 Token 账单                        │
├─────────────────────────────────────────────────┤
│  输入 Token(你发给模型的)                          │
│  ├── 系统提示词(System Prompt)                    │
│  ├── 用户消息(当前问题)                            │
│  ├── 对话历史(之前的多轮问答)                       │
│  └── 工具/文件上下文(如有)                          │
├─────────────────────────────────────────────────┤
│  输出 Token(模型生成的回复)                         │
│  └── 模型回答的全部内容                              │
├─────────────────────────────────────────────────┤
│  总消耗 = 输入 Token + 输出 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
关键认知 输出 Token 的单价通常是输入 Token 的 3–10 倍。因此,控制模型回复长度(设置合理的 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音频输入价格更高

省钱技巧

Batch 调用(半价)

不要求实时响应的任务可提交批量请求,输入和输出单价均为实时推理的 50%。适合离线分析、数据标注等场景。

上下文缓存

高频重复的系统提示词或文档可缓存,命中缓存时仅输入 Token 享折扣。适合固定 Prompt 的多次调用。

免费额度

新用户开通阿里云百炼平台即送各模型 100 万 Token 免费额度(90 天有效),还可领取超 7000 万 Token。

合理选择模型

简单任务用 Qwen-Long(最低价),复杂推理用 Qwen-Max。不要用旗舰模型处理日常闲聊。

价格参考 以上价格为阿里云百炼平台中国内地(北京地域)公开定价,实际价格以官方最新公告为准。国际部署(新加坡)价格约为内地的 3–5 倍。
官方定价页: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

七、省钱最佳实践

  1. 精简系统提示词:去掉不必要的背景说明,每条多出的 Token 都会在每次对话中重复消耗
  2. 控制对话轮次:长对话的历史累积是隐藏的成本大头,必要时可定期”重置对话”
  3. 设置合理的 max_tokens:不要默认设 4096 或 8192,按实际回复长度需求设定
  4. 简单任务降级模型:日常问答用 Qwen-Long(0.5 元/百万Token),复杂推理才用 Qwen-Max
  5. 批量任务用 Batch 模式:离线处理大批量数据时,Batch 模式可节省 50% 费用
  6. 启用流式输出:用户可中途停止生成,避免模型产出不需要的冗长回复
  7. 高频问答做缓存:相同问题直接返回缓存结果,零 Token 消耗
  8. 利用免费额度:开通百炼平台领取 7000 万+ Token 免费额度,先试用再付费

核心要点总结

Token 本质
LLM 处理文本的最小单位,中文约 1-2 字/Token,英文约 1 词/Token
消耗公式
总消耗 = 输入 Token + 输出 Token;总费用 = 输入 × 输入单价 + 输出 × 输出单价
输入来源
系统提示词 + 用户消息 + 对话历史 + 工具/文件上下文
输出特点
单价通常是输入的 3-10 倍,思考模式额外增加推理 Token
千问价格
Long 最低 ¥0.5/百万Token 输入;Plus 主力 ¥0.8 输入;Max 旗舰 ¥2.5 输入
省钱核心
精简 Prompt + 控制对话轮次 + 选对模型 + Batch 半价 + 利用免费额度

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 TextToken CountToken Breakdown
artificial intelligence~2-3art + ificial + intelligence
Hello World~2-3Hello + World
transformer~2-3trans + former
1234561-3123 + 456
print("hello")~5-6print + ( + " + hello + " + )
Rule of Thumb 1 Chinese character ≈ 1.5–2 tokens  |  1 English word ≈ 1–1.5 tokens  |  1000 tokens ≈ ~750 English words ≈ ~500 Chinese characters

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)
Hidden cost of multi-turn conversations Every new turn, all historical messages are re-sent. By round 10, the previous 9 rounds have accumulated into a massive input token count. This is why long conversations get progressively more expensive.

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

$Total Cost = (Input Tokens × Input Price) + (Output Tokens × Output Price)

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
Key Insight Output token pricing is typically 3–10× higher than input. So controlling response length (setting a reasonable 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:

ModelInput (per M tokens)Output (per M tokens)Features
Qwen-Long¥0.5¥2.0Long-text specialist, best value
Qwen3.5-Plus¥0.8¥4.8Main model, multimodal, 0-128K context
Qwen3.5-Plus (128K-256K)¥2.0¥12.0Extended input range
Qwen3.5-Plus (256K-1M)¥4.0¥24.0Million-token context range
Qwen3-Max¥2.5¥10.0Flagship model, 0-32K context
Qwen3-Max (32K-128K)¥4.0¥16.0Mid-long text range
Qwen3-Max (128K-252K)¥7.0¥28.0Ultra-long text range
Qwen3-Omni-Flash¥1.8 (text)¥6.9Multimodal (audio/image/video input)
Qwen3-Omni-Flash (audio input)¥15.8¥12.7Higher audio input pricing

Cost-Saving Tips

Batch Calling (50% off)

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.

Context Caching

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.

Free Quota

New users of Alibaba Cloud Bailian Platform receive 1 million tokens per model (90 days) plus over 70 million additional free tokens.

Choose the Right Model

Use Qwen-Long (lowest price) for simple tasks; Qwen-Max for complex reasoning. Don’t use the flagship model for casual chat.

Pricing Reference Prices above are public pricing for Alibaba Cloud Bailian Platform (Beijing region). Actual prices are subject to the latest official announcements. International deployment (Singapore) is approximately 3–5× higher.
Official pricing: aliyun.com/product/tongyi  |  Bailian Platform: aliyun.com/product/bailian

5. Additional Factors Affecting Token Consumption

FactorImpactDescription
Thinking ModeOutput tokens increase significantlyInternal reasoning process is also counted as output, potentially doubling or more
Function CallingInput tokens increaseTool definitions and return values are counted as input tokens
Multimodal InputHigher unit priceImages, audio, and video are billed at their own rates, usually much higher than plain text
max_tokens settingCaps outputSetting it too high leads to unnecessary consumption; set a reasonable value based on actual needs
StreamingDoesn’t reduce totalBut 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

  1. Streamline system prompts: Remove unnecessary context; every extra token is consumed in every call
  2. Control conversation rounds: Long conversation history accumulation is a hidden cost driver; reset conversations periodically
  3. Set reasonable max_tokens: Don’t default to 4096 or 8192; set based on actual response length needs
  4. Downgrade model for simple tasks: Use Qwen-Long (¥0.5/M tokens) for daily Q&A, Qwen-Max only for complex reasoning
  5. Use Batch mode for bulk tasks: Processing large data offline? Batch mode saves 50%
  6. Enable streaming: Users can stop mid-generation, avoiding unnecessary lengthy responses
  7. Cache high-frequency Q&A: Return cached results directly for identical questions — zero token cost
  8. Use free quotas: Activate the Bailian Platform to claim 70M+ free tokens for trial

Key Takeaways

Token Basics
The smallest unit of text processing in LLMs; ~1-2 Chinese characters per token, ~1 English word per token
Consumption Formula
Total = Input Tokens + Output Tokens; Cost = Input × Input Price + Output × Output Price
Input Sources
System prompt + user message + conversation history + tool/file context
Output Characteristics
Usually 3-10× more expensive than input; thinking mode adds extra reasoning tokens
Qwen Pricing
Long: ¥0.5/M input; Plus: ¥0.8/M input; Max: ¥2.5/M input (China mainland)
Saving Core
Streamline prompts + control rounds + choose the right model + Batch 50% off + use free quotas

Based on Alibaba Cloud Tongyi (Qwen) official documentation and LLM token pricing principles