Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.
Python agentaianthropicclaude-codecompressioncontext-engineeringcontext-windowcursorfastapilangchainllmmcpopenaiprompt-engineeringproxypythonragtoken-optimizationtokenstypescript
fix(kompress): keep words past the tokenizer's truncation point (#4328)
Vaibhav Hajarebb1338482906
diff --git a/headroom/transforms/kompress_compressor.py b/headroom/transforms/kompress_compressor.pyindex 3cd98a241..08c74e33f 100644--- a/headroom/transforms/kompress_compressor.py+++ b/headroom/transforms/kompress_compressor.py@@ -87,6 +87,8 @@ _KOMPRESS_MUST_KEEP_RE = re.compile(r"|(?i:\b(?:and|or|nor|xor)\b)")_KOMPRESS_MUST_KEEP_ENV = "HEADROOM_KOMPRESS_MUST_KEEP"+#: Tokenizer budget per chunk. Words past it are never scored (#4309).+_KOMPRESS_MAX_TOKENS = 512KOMPRESS_BACKEND_ENV = "HEADROOM_KOMPRESS_BACKEND"KOMPRESS_ONNX_FILENAME_ENV = "HEADROOM_KOMPRESS_ONNX_FILENAME"@@ -142,6 +144,39 @@ def _add_kompress_must_keep_words(kept_ids.add(word_idx + chunk_start)+def _add_truncated_words(+ kept_ids: set[int],+ word_ids: list[int | None],+ chunk_words: list[str],+ chunk_start: int,+ token_count: int,+) -> None:+ """Keep the words the tokenizer's token cut took away.++ Chunks are cut by words (``chunk_words``) but tokenized with+ ``truncation=True, max_length=512``. Words past token 512 get no+ ``word_id``, so they are never scored and never make ``kept_ids`` -- they+ are dropped silently, regardless of importance, and the loss is+ positional: whatever sits at the end of the chunk disappears (#4309).++ The cut can also land inside a word. Its visible prefix counts as+ covered, so the model scores part of a word and may drop the whole of+ it, taking the unseen suffix with it. The last covered word is kept as+ well whenever the chunk reached the token limit.+ """+ covered = max((wid for wid in word_ids if wid is not None), default=-1) + 1+ first_dropped = covered+ if token_count >= _KOMPRESS_MAX_TOKENS and covered > 0:+ first_dropped = covered - 1+ for local_idx in range(first_dropped, len(chunk_words)):+ kept_ids.add(local_idx + chunk_start)+++def _real_token_count(attention_row: Any) -> int:+ """Number of real (non-padding) tokens in an attention-mask row."""+ return sum(1 for mask in attention_row if mask)++_TABULAR_ROW_SPLIT_RE = re.compile(r"\s{2,}")@@ -2059,6 +2094,7 @@ class KompressCompressor(Transform):input_ids = encoding["input_ids"]attention_mask = encoding["attention_mask"]word_ids = encoding.word_ids(batch_index=0)+ token_count = _real_token_count(attention_mask[0])# One row, so no padding: every token in it is real.scored = _words_seen_whole(word_ids, len(word_ids), len(chunk_words))if scored == 0:@@ -2157,6 +2193,11 @@ class KompressCompressor(Transform):continueif bool(mask_list[idx]):kept_ids.add(wid + chunk_start)+ # The words past the tokenizer's token cut were never+ # scored, so the model cannot have kept them. A forced+ # ratio is a hard budget the caller asked for, so only the+ # model-decides path takes them back (#4309).+ _add_truncated_words(kept_ids, word_ids, chunk_words, chunk_start, token_count)# Hard override: always keep must-keep tokens regardless of model score.# Numbers, error names, paths, and flags carry meaning agents cannot@@ -2591,6 +2632,17 @@ class KompressCompressor(Transform):for wid, score in word_scores.items():if score > self.config.score_threshold:kept_ids_per_text[text_idx].add(wid + chunk_start)+ # The words past the tokenizer's token cut were never+ # scored, so the model cannot have kept them. A forced+ # ratio is a hard budget the caller asked for, so only+ # the model-decides path takes them back (#4309).+ _add_truncated_words(+ kept_ids_per_text[text_idx],+ word_ids,+ chunk_words,+ chunk_start,+ _real_token_count(attention_mask[batch_idx]),+ )_add_kompress_must_keep_words(kept_ids_per_text[text_idx], chunk_words, chunk_startdiff --git a/tests/test_kompress_mps_serialization.py b/tests/test_kompress_mps_serialization.pyindex e3afff808..45eb39731 100644--- a/tests/test_kompress_mps_serialization.py+++ b/tests/test_kompress_mps_serialization.py@@ -51,6 +51,10 @@ def test_single_and_batch_device_copies_hold_execution_slot(monkeypatch):def __init__(self, row_lengths):self.row_lengths = row_lengths+ def __getitem__(self, index):+ # Real attention masks expose each row for padding-aware counts.+ return [1] * self.row_lengths[index]+def to(self, _device):acquired = semaphore.acquire(blocking=False)if acquired:diff --git a/tests/test_kompress_must_keep.py b/tests/test_kompress_must_keep.pyindex 953cdc04c..b0e991ab2 100644--- a/tests/test_kompress_must_keep.py+++ b/tests/test_kompress_must_keep.py@@ -192,3 +192,123 @@ class TestMustKeepCompression:)assert result.compressed.split() == ["alpha", "0x7fff2038"]+++class _TruncatingTok:+ """Tokenizer that models ``truncation=True, max_length=512``.++ Every word costs ``tokens_per_word`` tokens, which is how a chunk of+ token-heavy words (logs, IDs, paths, numbers) runs past the token limit+ before its word budget is used up. Words past the limit get no+ ``word_id`` at all, exactly as the real tokenizer reports them. The cut+ can also land inside a word: that word keeps the tokens that fit and+ the rest of it never reaches the tokenizer.+ """++ def __init__(self, tokens_per_word: int = 1, max_length: int = 512):+ self.tokens_per_word = tokens_per_word+ self.max_length = max_length++ def __call__(self, chunk_words, **kw):+ batch_words = (+ chunk_words if (chunk_words and isinstance(chunk_words[0], list)) else [chunk_words]+ )+ word_ids = []+ for words in batch_words:+ ids: list[int] = []+ used = 0+ for idx in range(len(words)):+ room = self.max_length - used+ if room <= 0:+ break+ take = min(self.tokens_per_word, room)+ ids.extend([idx] * take)+ used += take+ word_ids.append(ids)+ return _Enc(+ input_ids=[[0] * len(ids) for ids in word_ids],+ attention_mask=[[1] * len(ids) for ids in word_ids],+ _word_ids=word_ids,+ )+++class TestTruncatedWordsCompression:+ """Words past the tokenizer's 512-token cut are kept, not dropped (#4309).++ Chunks are cut by words (``chunk_words``) but tokenized with+ ``truncation=True, max_length=512``. A chunk of token-heavy words hits+ the token limit well before its word budget, so the tail of the chunk had+ no score and was dropped silently from the default proxy path+ (``target_ratio=None``).+ """++ # One chunk is `chunk_words` words long, so 350 words is exactly one.+ CHUNK_WORDS = 350++ def _words(self, count: int) -> list[str]:+ return [f"word{i}" for i in range(count)]++ def _install(self, monkeypatch, tokens_per_word):+ monkeypatch.setattr(+ kc,+ "_load_kompress",+ lambda *a, **k: (_Model(), _TruncatingTok(tokens_per_word), "onnx"),+ )+ monkeypatch.setattr(kc, "_model_device_type", lambda *a, **k: "cpu")+ monkeypatch.delenv(_KOMPRESS_MUST_KEEP_ENV, raising=False)++ def _compress(self, monkeypatch, tokens_per_word):+ self._install(monkeypatch, tokens_per_word)+ words = self._words(self.CHUNK_WORDS)+ compressor = KompressCompressor(KompressConfig(enable_ccr=False, min_input_words=10))+ monkeypatch.setattr(compressor, "_should_batch_single_content", lambda *a, **k: False)+ kept = compressor.compress(" ".join(words)).compressed.split()+ return kept, words++ def test_words_past_the_truncation_point_survive(self, monkeypatch):+ # Two tokens per word: the 350-word chunk needs 700 tokens, so the+ # tokenizer cuts it at 512. The 256 words it covered were scored, the+ # other 94 were never seen and used to vanish from the output.+ kept, words = self._compress(monkeypatch, tokens_per_word=2)+ # 256 words were covered; the cut landed at a word boundary here, but+ # the last covered word is kept too, because the cut may have fallen+ # inside it.+ assert kept == [words[0]] + words[255:]++ def test_the_word_the_cut_landed_in_is_kept(self, monkeypatch):+ # Three tokens per word: the cut at 512 falls inside word 170, whose+ # last token never reached the tokenizer. The model only saw part of+ # that word, so dropping it would take its unseen suffix with it.+ kept, words = self._compress(monkeypatch, tokens_per_word=3)+ assert kept == [words[0]] + words[170:]++ def test_fully_covered_chunk_is_unchanged(self, monkeypatch):+ # One token per word: the chunk fits in 512 tokens, so nothing was+ # truncated and the model's own choice stands. It kept the first word+ # and dropped the rest.+ kept, words = self._compress(monkeypatch, tokens_per_word=1)+ assert kept == [words[0]]++ def test_batch_path_keeps_truncated_words_too(self, monkeypatch):+ self._install(monkeypatch, 2)+ words = self._words(self.CHUNK_WORDS)+ compressor = KompressCompressor(KompressConfig(enable_ccr=False, min_input_words=10))+ monkeypatch.setattr(compressor, "_should_use_sequential_fallback", lambda: False)++ [result] = compressor.compress_batch([" ".join(words)], batch_size=8)++ assert result.compressed.split() == [words[0]] + words[255:]++ def test_a_forced_ratio_still_gets_its_budget(self, monkeypatch):+ # A forced ratio selects scored words within each token window.+ self._install(monkeypatch, 2)+ words = self._words(self.CHUNK_WORDS)+ compressor = KompressCompressor(KompressConfig(enable_ccr=False, min_input_words=10))+ monkeypatch.setattr(compressor, "_should_batch_single_content", lambda *a, **k: False)++ kept = compressor.compress(" ".join(words), target_ratio=0.5).compressed.split()++ # Main now scores the formerly unseen tail in another window.+ # Windows score 255 and 95 complete words, each applying the+ # requested ratio independently with rounding down (127 + 47).+ assert kept == words[:127] + words[255:302]