Skip to main content
← CONCEPTS / AI-MACHINE-LEARNING
An end-to-end domain learning journey

Perplexity-Based Token Pruning &
Entropy Context Compression.

Partition prompts into immutable Pinned Anchors (system prompts, tool schemas, safety rules) and compressible context, then prune low-surprisal tokens using a quantized 0.5B–1.5B SLM conditioned on the active user query.
Start the journey5 sections· Last modified Nov 27, 2026
FIRST PRINCIPLES. PRODUCTION SCALE.
The idea to keep with you

When 128k–1M token prompts exceed latency budgets, score 4k chunks in parallel with a 128-token overlap stride and prune below quantile threshold tau.

TL;DR30-second simplified summary
▾

Parallel SLM perplexity scoring measures conditional surprisal across 4k chunks with a 128-token overlap stride to avoid boundary spikes. Combined with query-contrastive PMI and a 450 ms fail-open breaker, it sustains >98% entity recall.

Before you begin · Building blocksAlready know these? Jump to Stage 01 ↓
Shannon Information & Token Surprisal
Know that a token's information content is I(x_i) = -log2 P(x_i | x_<i): predictable boilerplate carries near-zero bits, while rare entities and constraints carry high surprisal.
Transformer Prefill & KV-Cache Scaling
Understand that a 70B FP16 GQA-8 model consumes ~160 KB of KV cache per token, meaning 1M tokens requires 160 GB of GPU HBM.
Full walkthrough · 5 stages
01
L4 · FUNDAMENTALS

P · Problem & Why Naive Context Truncation Fails.

A 70B LLM with GQA-8 uses ~160 KB of FP16 KV cache per token. At 1M tokens, a single session needs 160 GB of KV cache—exceeding an 80 GB H100—and takes ~18–35 seconds just to prefill before emitting token #1.

If you naively truncate to the last 16k tokens, you amputate the system prompt, JSON tool definitions, and early API keys or user constraints—causing immediate tool-call schema failures and safety regressions.

Context Bottleneck

Raw 1M Prefill / Naive Truncation vs. Anchor-Pinned Perplexity Pruning

Before · Raw 1M / Naive Tail Cut
  • 160 GB KV cache per 70B session
  • 18–35s Time-To-First-Token
  • Tail cut drops tool schemas
After · Pinned Anchors + SLM Pruning
  • 1.92 GB KV cache (12k tokens)
  • <120 ms parallel SLM scoring
  • 0% anchor loss; >98% entity recall
02
L4 · FUNDAMENTALS

R · Representation: Pinned Anchors & Conditional Surprisal.

Every prompt is partitioned into Class A Pinned Anchors (system prompt, tool schemas, safety invariants, pinned facts) with weight w = infinity, and Class B Compressible Segments (RAG passages, tool stdout, old turns).

For Class B tokens, a 0.5B–1.5B INT8 SLM computes conditional surprisal I(x_i) = -log2 P(x_i | x_<i). Tokens the SLM predicts with >95% probability carry <0.07 bits of information and are safe to drop; unexpected tokens (UUIDs, latency numbers, stack traces) carry 6–12 bits and are kept.

Token Classification

Anchor Partitioning & Query-Aware Perplexity Graph

1M Input ContextIngressPinned Anchors(w=inf)Immutable0.5B INT8 SLMScorerEntropy8k-16k ContextWindowOutputSystem & Tool SchemasRAG & History Chunks0% compressionTop-S(x_i|q) tokens
03
L5 · APPLIED DESIGN

I · Invariant & Safe Python Implementation.

A critical implementation bug when scoring 1M tokens in parallel 4k chunks is boundary perplexity inflation: token 0 of every chunk has no left context, so its surprisal spikes artificially.

The fix is a 128-token overlap stride between adjacent 4,096-token chunks, discarding scores from the first 64 tokens of chunk k > 0 (only ~3.1% compute overhead) before running O(N) threshold selection.

Parallel Scoring

Overlap-Stride Chunk Scoring & Quantile Assembly

04
L5 · APPLIED DESIGN

M · Mechanics at Scale: Hierarchical Trees & KV Eviction.

Token-level perplexity pruning excels at 8x–20x compression, but 1M -> 8k (125x) needs structural abstraction over old turns.

Aging 4k-token leaves are asynchronously distilled into 512-token branch summaries and a 128-token root spine, while H2O Heavy-Hitter Oracle retains attention sinks (indices 0..7) and high-attention tokens in GPU HBM.

Compression Regimes

Perplexity Pruning vs. Abstractive Trees vs. Hybrid

Engineering Decision Framework
Decision Exercise · Make the Call

An agent coding session has 180k tokens of old build logs plus a 2k-token JSON tool schema, and p95 SLM scoring latency spikes to 470 ms. Which action protects both correctness and SLO?

05
L6 · SCALE & TRADE-OFFS

E · Edge Cases, Assumptions, When NOT to Use & Glossary.

Assumptions & Limitations: Assumes an 8k–16k target window, quantized 0.5B–1.5B SLM workers co-located with LLM inference pods, and deterministic anchor delimiters.

When NOT to Use It: Do not run perplexity pruning on short <8k prompts (adds unnecessary 90 ms overhead), cryptographic/legal verbatim contracts where single-word omissions alter liability, or dense minified code where every symbol has high entropy.

Resilience FSM

450 ms Compression Circuit Breaker State Machine

p95 > 450 ms30s cooldownprobes < 200 msCLOSED (SLM + TreeActive)OPEN (Anchors +12k Tail)HALF_OPEN (5%Probe)
Practice · Explain the consequences

Practice: Make the trade-off yourself.

Question 1 of 4
Score: 0/4

Why must system prompts and JSON tool schemas be locked as Pinned Anchors before perplexity pruning?

Recommended Learning Flow