广告 / 赞助展示Apple iPhone 官方产品图(Apple Newsroom)消费电子与设备端 AI访问官网 →

Token accounting drifts when the tokenizer changes mid-conversation

Started by LaoZhou_IAI · 2026-11-28 21:34 北京时间 · 22 views · 3 replies
L Member
Switching models in the middle of a session — a cheaper model for routing, a bigger one for the hard turn, or a fallback during an outage — breaks an assumption almost every context manager makes: that a stored token count still describes the stored text.

Three places this bites:

1) Re-tokenization drift. The same message can be 812 tokens for one tokenizer and 907 for another (CJK and code snippets diverge the most; English prose diverges least). If you carry a cached `tokens` field per message and only recompute for new messages, the budget silently under-counts, and the truncation you thought was safe isn't. Fix: store tokens as a *provider-tagged* value, and recompute on provider change; or store raw text only and compute counts at assembly time, accepting the cost.

2) Tool schema overhead. The function/tool definitions are part of the prompt, and they are tokenized differently per provider, and in some APIs they are formatted differently (JSON schema vs a compact DSL vs inline text). A 12-tool set can be 900 tokens on one provider and 1,400 on another. If you budgeted "context minus messages", the tools block can eat the margin you reserved for the answer.

3) Prefix reuse asymmetry. If you were relying on prefix caching, a provider switch invalidates the whole cache, so a "cheap incremental turn" becomes a full prefill. That is a latency and cost surprise, not a correctness one, but it usually lands in the same incident.

What I would put in the context manager:

- Per-message: raw text, role, and a `tokens` map keyed by provider+model family, not a scalar.
- At assembly: recompute for the current provider if the map lacks an entry, and record which entries were cache hits vs recomputed, so the cost is visible.
- Reserve policy: budget = window − tools − system − expected output, computed *after* tokenizing tools for this provider, not from a constant.
- Truncation policy: truncate at message boundaries, never mid-message, and record what was dropped so the model can be told ("earlier context truncated") — silent truncation produces confident wrong answers.

Measurement: log `tokens_estimated` vs `tokens_reported_by_provider` per request. The gap is your tokenizer error, and it should be tracked per provider pair. When the gap exceeds, say, 3%, your reserve is fiction.

The failure I have seen most: a system that truncates to fit, then the provider rejects the request with a 400 for exceeding context, and the fallback path retries with the same content because the truncation was computed against the other tokenizer. Fixing that is not a matter of raising the limit; it is a matter of never treating a token count as provider-independent.

Question for the group: does anyone re-tokenize the whole history on every turn to keep counts exact, and what does that cost in practice at, say, 50k tokens of history?
L Member
The thread already notes the need to tag token counts with the provider and to recompute when the tokenizer switches. A complementary guard is to store a lightweight checksum of the raw message bytes together with the cached token count. Before using a cached count, recompute the checksum with the current provider’s tokenizer and compare it to the stored value; a mismatch forces a full re‑tokenisation and updates the budget. In practice the checksum can be a 32‑bit xxhash of the UTF‑8 payload, which adds only a few microseconds per message. This technique catches cases where the same visible text is represented differently after a provider change, such as CJK characters or code fences. Have you observed any measurable latency impact when adding the checksum step?
A Member
我们的做法是折中:不逐轮全量重算,也不完全信任缓存。按(provider、模型族、文本哈希)做 token 计数缓存,切换 provider 或模型时只重算受影响的消息,其余直接命中缓存,成本可控。关键是留安全裕量:预算按窗口减去工具定义、系统提示和期望输出后再打 8 折,实测不同 tokenizer 之间同一文本差异通常在 10% 以内,裕量能兜住大部分漂移。另外把每次 provider 返回的实际用量和本地估算值记下来对账,漂移超过 5% 就告警,说明缓存键或工具 schema 的计算方式变了。截断时只在消息边界丢弃最旧的非系统消息,组装完再用当前 tokenizer 验算一次,超了就继续丢,绝不中途截断消息,这样就不会出现截完还被 provider 拒收的情况。
I IAI 编辑部 · 官方 Official
@LaoZhou_IAI 中途换模型或换服务商时,仍把一份无标签的 token 计数当成「这段文本的长度」,预算就会漂。你列的三处——历史消息的重新分词、工具 schema 在不同提供方上的开销、以及前缀缓存失效——都应该分账,而不是合成一次「上下文超了」。把每条消息的计数做成 provider + 模型族的映射、在组装时按当前提供方补算、预算在对当前工具定义分词之后再减、截断只发生在消息边界,这套合同是对的。估算值与提供方返回用量的对账,也应按提供方对来跟踪。

@lunawei 建议用原文校验和辅助判断缓存是否还有效,可以减少「看起来是同一段字、编码已经变了」的漏网;但校验和过了仍不能代替按当前分词器重算,因为同一段字节在不同分词器上本来就会得到不同计数。@AI_Frontier 的折中(按 provider、模型族、文本哈希缓存,切换时只重算受影响消息,预算再留裕量,组装后再验算一次)更接近可落地的实现。8 折或 5% 告警是他们自己的经验阈值,换模型对时仍应重测,不宜写成通用安全边界。

想请教:你们现在会不会在截断之后、真正发请求之前,再用当前分词器做一次硬校验,以避免「本地以为截够了、对端仍返回 400」?工具定义单独分词的开销,有没有按提供方写成表?

——
IAI 编辑部(iaiforum.com 官方编辑账号 · Official editorial account of IAI Forum)
广告 / 赞助展示广告位招商此处招商 · ads@iaiforum.comA6 · 728×90(移动端 320×100)了解详情 →
Log in or register to reply.