Token Cost Engineering · 10 / 13

Semantic Layer: Double Distillation

Format tax is cut. Next look at the content itself. The most common engineering mistake in RAG and long-document work: treat the context window as a dump—stuff every Few-Shot example and retrieved doc in, and hope the model sorts it out. It works. It shouldn't.

O(N²)lost-in-the-middledynamic Few-ShotLLMLingua-2
Two sins of dump-style context

First: expensive and slow. Transformer self-attention is O(N²): double the prompt length, and compute grows 4×. Longer prompts mean longer Prefill and higher time to first token—users lose patience before they see the first character.

Second: quality can get worse. When signal drowns in filler, you get the lost-in-the-middle effect. Like a person reading a long article: they focus at the start (what's this about?), pay attention at the end (here's the conclusion), and skim the big middle blob—eyes pass, brain doesn't. If your carefully picked references land in the middle, the model may never really read them.

Interactive Demo · How attention gets diluted

The color strip simulates attention strength across Prompt positions (green = strong, gray = weak). Drag the length and watch the middle sag.

6k Tokens
StartMiddleEnd
More context is not always better. Put critical information at the start or the end; leave the middle for content you wouldn't mind losing.
Strategy 1 · Dynamic Few-Shot—don't hard-code

Take Text-to-SQL: to cover every business case, some people hard-code 20 SQL examples into the Prompt—4,000+ Tokens total. Every user question, the model “reviews” all 4,000 first: burns Tokens and runs slow. Better approach:

1

Store the 20 examples in a vector database.

2

When the user asks “last month's sales,” use semantic retrieval to pull only the Top-3 finance-related examples.

3

Final Prompt drops from 4,000 Tokens to 500.

-87.5%
Token cost
3x+
Response speed
Higher
SQL accuracy
(no irrelevant-example noise)
Strategy 2 · Compress long docs before you feed them

Financial research, meeting notes—docs packed with “correct filler”: disclaimers, repeated background, spoken padding. Feed that to the AI and you're paying PhD rates to read spam.

Fix: after RAG retrieval and before inference, insert an LLMLingua-2 middleware. It doesn't chop words blindly: BERT bidirectional attention sees both sides of context, pinpoints core semantics (entities, numbers, key verbs), and strips redundant noise. Compress 5–20×; Prefill that took 1 second drops to 50 ms—high-concurrency throughput jumps a full order of magnitude.

Double distillation: only a high-density Prompt earns high-quality Attention
Dynamic Few-Shot + document compression: two funnels filter noise. Only a high-density Prompt earns high-quality Attention. (Figure: from the author's original share deck)
DistillationTargetMethodTypical gain
First passFew-Shot examplesVector retrieval picks Top-K dynamically4,000 → 500 Tokens
Second passRetrieved documentsLLMLingua-2 semantic compression5–20× compression; Prefill 1s → 50ms
Key Takeaways

Double the prompt = 4× the compute: O(N²) is why long context is both expensive and slow.

Lost-in-the-middle: put critical info at the start or end; leave the middle for content you wouldn't mind losing.

Don't hard-code Few-Shot—store in a vector DB and retrieve by question: save 87.5% and get more accuracy.

Run long docs through LLMLingua-2 before inference: high-density Prompt for high-quality Attention—don't lose users while they wait.

Source: Adapted from the author's internal team share “AI Token Cost Engineering Strategy” hands-on section “02|Semantic Layer.” For lost-in-the-middle, see Chroma's Context Rot research; compression benchmarks at LLMLingua. Review context-window basics in Harness Core · Context Window.