Special Topic · Token Cost Engineering: Make the Numbers Work

Semantic Layer: Double Distillation

Lost-in-the-middle: the more you stuff in, the less it holds onto what matters. Dynamic Few-Shot cuts 4000 to 500; LLMLingua-2 compresses 5–20×

THE QUESTION THIS PAGE ANSWERS

ANSWER FIRST

What is the key idea behind “Semantic Layer: Double Distillation”?

Lost-in-the-middle: the more you stuff in, the less it holds onto what matters. Dynamic Few-Shot cuts 4000 to 500; LLMLingua-2 compresses 5–20×

DECISION RULE

Read cost as a shape, not a single number. Break a request into input, output, retries, tools, and waiting time. The shape of usage usually tells you which design choice is expensive and where a smaller change can help.

TRY NEXT

Measure one real request before you optimize an imagined average.

WATCH FOR

A cheaper call that quietly creates more retries, latency, or review work.

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.

The complete interaction cost of “Two sins of dump-style context”

“First: expensive and slow.” is a reminder that AI cost is not one price multiplied by one call. Input, output, retries, tools, waiting time, and human cleanup together decide what a task really costs.

Find what the bill repeats

The key variables behind “Second: quality can get worse.” are usually repeated context, oversized output, retries after failure, and calls that do not produce useful progress. Removing wasted Tokens can reduce cost, latency, and concurrency pressure at the same time.

A cheaper call can make the whole workflow more expensive

Start with “Run long docs through LLMLingua-2 before inference: high-density Prompt for high-quality Attention—don't lose users while they wait” and keep a small table for input, output, retries, tools, and human review. Compare quality before and after optimizing instead of looking at one price in isolation.

From “Two sins of dump-style context” to “Interactive Demo · How attention gets diluted”

“Two sins of dump-style context” grounds the problem in “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…”. “Interactive Demo · How attention gets diluted” then moves it toward “The color strip simulates attention strength across Prompt positions (green = strong, gray = weak). Drag the length and watch the middle sag”. Together, they show that the lesson is not just a conclusion to remember, but a claim with conditions.

Carry the judgment into the next situation

When analyzing cost, map the complete interaction first, then find repeated input, wasted output, and retries. A cheap individual call does not make the whole task cheap.

  • “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…
  • “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
  • “The closing point”: 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

The final “The closing point” brings the discussion to “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”. The useful thing to carry forward is knowing which judgments must be revisited when input, scale, or risk changes.

Mark as learned Your reading progress updates automatically
← PreviousNext →

Keep reading

The next useful article in the thread.

ARTICLE DISCUSSION

Leave one useful thought here.

Keep the idea that clicked, the question that stayed open, or a small note for the next learner.

Discussing Semantic Layer: Double Distillation Token Cost Engineering: Make the Numbers Work
3discussionsArticle discussion · synced with the Circle
View in the learning circle
AM
Asha MorganContent editor
INSIGHTField note

I turned one judgment from this article into a small experiment I could run today. Knowing what to observe next is more useful than simply remembering the conclusion.

ARTICLE DISCUSSION7 helpful
LH
Lin HarperIndie developer
INSIGHTInsight

After reading this, I first looked for the conditions behind the idea instead of copying the method into a project. That order made the later trade-offs much clearer.

ARTICLE DISCUSSION5 helpful
KM
Kiki MooreProduct operations
QUESTIONQuestion

When this judgment reaches real work, which constraint should be added first? I am curious which step matters most between reading and the first practical attempt.

ARTICLE DISCUSSION4 helpful