Token

模型处理文本的最小单位,不是字也不是词;所有大模型 API 都按它计费

Token 是模型看待文本的基本单位。模型并不直接处理字符,而是先用分词器(Tokenizer)把文本切成一串 Token,每个 Token 映射成一个整数 ID,再送进网络。你输入的每个字、模型输出的每个字,都要经过这道切分。

Token 到底是多大一块

它既不是字符也不是单词,而是统计上高频的片段:

  • 英文里,常见单词往往是一个 Token,长词或生僻词会被切成几块。粗略估算 1 个单词 ≈ 1.3 个 Token。
  • 中文因为字符集大、在训练语料中占比相对低,通常一个汉字就是一到两个 Token,取决于分词器。同样一段意思,中文消耗的 Token 常常比英文多
  • 空格、标点、换行都算。代码里的缩进也算。
  • 数字会被切得很碎,这也是模型算术不稳的原因之一。

实用估算(以中文为主的内容):1000 汉字大约 1500-2000 Token。要精确就用厂商提供的 tokenizer 实测,别拍脑袋。

为什么它直接决定成本

所有主流模型 API 都按 Token 计费,而且输入和输出分开计价,输出通常贵好几倍。这带来几个直接后果:

  • 长对话的成本随轮数增长得比想象中快——每轮都要把完整历史重新发一遍。
  • 让模型"简明作答"不只是体验问题,是省钱。
  • 塞长文档进 Prompt 前先算账:一份 5 万字的文档大约 8 万 Token,每问一次都要付一次。
  • 前缀缓存能让重复的输入部分打折,把固定内容放在最前面是很实在的优化。

它还决定了几个能力边界

  • 上下文窗口用 Token 计量,输入输出共用额度。
  • 速率限制通常也是按每分钟 Token 数(TPM)算,不只是请求数。
  • 模型对精确字符操作很弱——数一个词有几个字母、反转字符串这类任务它经常做错,因为它看到的是 Token 不是字符。

常见误解

"一个 Token 就是一个词"——只在英文里近似成立,中文和代码里差得远。

"中英文成本差不多"——同样内容中文往往更贵,做多语言产品时这笔账要单独算。

"只算输入就行"——输出单价通常是输入的 3 到 5 倍,让模型少说废话对账单的影响比优化输入更大。

英文原文解释(Dictionary of AI Coding)

The atomic unit a model reads and writes. Roughly word-sized but not exactly — common words are one token, rare or long ones split into several. Context window size, cost, and latency are all counted in tokens.

Text becomes tokens via a tokenizer: a fixed vocabulary of tens of thousands of fragments, learned before training, that splits any input into a sequence of vocabulary entries. The model never sees characters or words — every piece of text is converted to tokens on the way in, and next-token prediction produces output one token at a time on the way out.

As a rule of thumb, a token is about three-quarters of an English word, so a thousand tokens is roughly 750 words. Code is less predictable: common keywords and idioms tokenize compactly, while generated identifiers, hashes, base64 blobs, and minified output split into many tokens per "word". The pattern: text that appeared often in the tokenizer's source material gets short, efficient encodings; text that didn't gets chopped into many small pieces. A hash like a3f9c2e1 never appeared anywhere, so it splits into many tokens, while function is one. This is why a small-looking file full of unusual strings can occupy a surprising share of the context window.

Tokens are the unit everything else is measured in. Cost is per token — providers bill input tokens and output tokens separately. Speed is tokens per second, since output is generated one token at a time. And the context window is a fixed number of tokens, so the token count of your files decides how much fits.

什么时候会用到

估算 API 成本、设计上下文策略、排查速率限制时的基本单位。

例句

  • 这份文档八万 Token,每问一次都要付一次,先做检索别全塞。
  • 输出比输入贵好几倍,让它别啰嗦是最直接的省钱手段。
  • 同样的意思中文比英文费 Token,多语言的成本得分开算。

别混淆

别用「字数」估 Token。中文、代码、数字的切分比例差别很大,要精确就用官方 tokenizer 实测。

相关词