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?
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?

