headroom/tests/test_tokenizer.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

86 lines
3.1 KiB
Python
Raw Normal View History

from __future__ import annotations
from typing import Any
from headroom.tokenizer import Tokenizer, count_tokens_messages, count_tokens_text
class FakeTokenCounter:
def __init__(self) -> None:
self.calls: list[tuple[str, Any]] = []
def count_text(self, text: str) -> int:
self.calls.append(("text", text))
return len(text.split())
def count_message(self, message: dict[str, Any]) -> int:
self.calls.append(("message", message))
return len(str(message.get("content", "")).split())
def count_messages(self, messages: list[dict[str, Any]]) -> int:
self.calls.append(("messages", messages))
return sum(len(str(msg.get("content", "")).split()) for msg in messages)
fix(tokenizers): price Claude against a real BPE (tiktoken o200k) not a char estimate (#2543) ## Description Claude's tokenizer is not public, so `get_tokenizer("claude-*")` returned `EstimatingTokenCounter(chars_per_token=3.5)` — a **character-ratio estimate**. The estimator's chars-per-token flips with the *detected content type* (JSON 3.2 / code 3.5 / English 4.0 / CJK 1.5), so: - the **same bytes** count differently before vs after compression → a fold could appear to *increase* tokens; - it **disagrees with the pipeline's** estimator on the same content. Calibration vs a real BPE (tiktoken `o200k_base`) on representative content: | content | char-est(3.5) / o200k | |---|---| | English | **1.28×** (overcount 28%) | | code | **0.83×** (undercount 17%) | | JSON | 0.97× | | diff | 1.02× | i.e. the estimate is off **−17% … +28%**, and the error is content-dependent (non-uniform) — which is exactly what produces impossible `tok_after > tok_before` deltas. This prices Claude against **tiktoken `o200k_base`** — a real, deterministic, **monotone** BPE. It's not Claude's exact vocab, but it's within ~10–20% and, crucially, **consistent before/after**, which is what compression ratios and context-pressure gating actually need. Closes # ## Type of Change - [x] Bug fix (non-breaking change that fixes an issue) - [x] Performance improvement - [ ] Breaking change - [ ] Documentation update - [ ] Code refactoring (no functional changes) ## Changes Made - `TiktokenCounter.__init__` accepts an explicit `encoding` override (so a model can be priced against a chosen encoding regardless of name-based resolution). - `_create_anthropic` now returns `TiktokenCounter(model, encoding="o200k_base")`, **failing open to the character estimator** if the tiktoken vocab can't be loaded (reuses the existing `load_encoding` timeout/guard). - Updated the `MODEL_PATTERNS` comment and `test_get_anthropic_model` to the new contract; added `test_claude_priced_with_real_bpe_not_char_estimate`. **Blast radius is contained:** `content_router` keeps its own module-level estimator for routing decisions, so only the **handler's reported counts** change. `tiktoken` is already a dependency. **Composes with #2542** (tokenizer-consistent before/after): that PR makes both endpoints use *one* tokenizer; this PR makes that one tokenizer a *real BPE*. Together they fully eliminate the inflated/phantom lines. This PR is based on `main` and is independently valid. ## Calibration note (please review) The one behavioral consumer of the handler's count is the background-compression gate (`original_tokens >= _background_compression_min_tokens`). Because o200k differs from the old estimate by ~±20% depending on content, that threshold now trips on slightly different requests. It's *more* accurate, but the threshold was tuned against the estimator — worth a re-check. No other gate consumes the handler count (routing uses `content_router`'s estimator). ## Testing - [x] Unit tests pass (`pytest`) - [x] Linting passes (`ruff check` + `ruff format --check`) - [x] Type checking passes (`mypy`) - [x] New tests added - [x] Manual testing performed ### Test Output ```text $ ruff check <changed files> && ruff format --check <changed files> All checks passed! 4 files already formatted $ mypy headroom/tokenizers/registry.py headroom/tokenizers/tiktoken_counter.py Success: no issues found in 2 source files $ pytest tests/test_tokenizer.py tests/test_tokenizers.py -q 55 passed, 14 skipped ``` ## Real Behavior Proof - **Unit:** `get_tokenizer("claude-opus-4-8")` → `TiktokenCounter(encoding_name="o200k_base")`; `count_text(sample)` == `tiktoken.get_encoding("o200k_base").encode(sample)` exactly; a `tool_result` shrunk 300→3 words drops 508→13 tokens (folds register). - **Live proxy** (Anthropic path, `--proxy-extension lossless_guard`): foldable request logs `tok_before=694 tok_after=231 tok_saved=463 transforms=turn_hook` — real o200k-scale numbers (vs the estimator's 607 for the same payload), clean fold, no inflation. - **Not tested:** exact agreement with Anthropic's *own* count_tokens API (o200k is a proxy; a follow-up could calibrate against it offline). GPT paths unchanged (already tiktoken). ## Review Readiness - [x] I have performed a self-review - [x] This PR is ready for human review ## Checklist - [x] My code follows the project's style guidelines - [x] I have performed a self-review of my code - [x] I have commented my code, particularly in hard-to-understand areas - [ ] Documentation — N/A (internal; docstrings updated) - [x] My changes generate no new warnings - [x] New and existing unit tests pass locally with my changes - [x] I did **not** edit `CHANGELOG.md` ## Additional Notes Follow-ups (not here): (1) extend the same real-BPE treatment to other private-tokenizer providers (Gemini/Cohere/Moonshot), each with its own calibration; (2) optional opt-in calibration against Anthropic's `count_tokens` API to true-up the ~10–20% absolute offset.
2026-07-24 15:06:43 -07:00
def test_claude_priced_with_real_bpe_not_char_estimate() -> None:
"""Claude has no public tokenizer, so we price it against a real BPE
(tiktoken o200k_base) instead of a content-adaptive character estimate
otherwise before/after counts drift between components and compressing text
can appear to *increase* tokens. A tool_result fold must always register as
a reduction; and when the vocab is available the count is the exact o200k
count (proving it is a real BPE, not a chars/token ratio)."""
from headroom.tokenizers import get_tokenizer
tok = get_tokenizer("claude-opus-4-8")
long_msg = [
{
"role": "user",
"content": [{"type": "tool_result", "tool_use_id": "t", "content": "alpha " * 300}],
}
]
short_msg = [
{
"role": "user",
"content": [{"type": "tool_result", "tool_use_id": "t", "content": "alpha " * 3}],
}
]
assert tok.count_messages(long_msg) > tok.count_messages(short_msg) # fold visible
try:
import tiktoken
enc = tiktoken.get_encoding("o200k_base")
except Exception: # vocab unavailable → estimator fallback; monotonicity above still holds
return
sample = "The quick brown fox jumps over the lazy dog. " * 10
assert tok.count_text(sample) == len(enc.encode(sample))
def test_tokenizer_delegates_to_counter() -> None:
counter = FakeTokenCounter()
tokenizer = Tokenizer(counter, model="gpt-4o")
assert tokenizer.model == "gpt-4o"
assert tokenizer.available is True
assert tokenizer.count_text("hello world") == 2
assert tokenizer.count_message({"role": "user", "content": "three word text"}) == 3
assert tokenizer.count_messages([{"content": "one two"}, {"content": "three"}]) == 3
assert counter.calls == [
("text", "hello world"),
("message", {"role": "user", "content": "three word text"}),
("messages", [{"content": "one two"}, {"content": "three"}]),
]
def test_tokenizer_convenience_functions() -> None:
counter = FakeTokenCounter()
messages = [{"content": "one"}, {"content": "two three"}]
assert count_tokens_text("alpha beta gamma", counter) == 3
assert count_tokens_messages(messages, counter) == 3
assert counter.calls == [
("text", "alpha beta gamma"),
("messages", messages),
]