<- all tokdocs

Four Context Engineering Techniques That Cut LLM Costs by 90%

Watch on TikTok

View on TikTok ->

Context engineering is about controlling exactly which tokens reach your LLM. This video walks through four specific techniques that reduce token counts dramatically without losing the information your model actually needs.

Technique 1: Compress Statistically, Keep Anomalies

Start with a hundred log entries totaling 19,000 characters. Ninety-nine of them say "status info, everything is normal." One, buried at position 67, is a fatal error: database crashed. Your LLM needs that one line, not the other 99.

The compression logic: keep the first three entries for structural context, keep the last two for recency, scan for anything containing "error," "warning," or "fatal" and always preserve those, then find statistical outliers where values spike above normal.

Title card showing "Context Engineering with 4 lines of logic" and a diagram comparing before (1000 tokens = $100) and after (15 tokens = $12)

Result: six entries, 1,100 characters. The fatal error at position 67 is still there. That is a 94% reduction with zero information loss.

After view showing compressed server logs with 6 entries and 1,155 characters, approximately 280 tokens. The fatal "DATABASE CRASHED" error at line 5 is preserved in red

Technique 2: Factor Out Repeated Fields

You have 2,000 files from a code search. Every single one includes type: file and language: python repeated in its metadata. That is the same two fields written 2,000 times.

Factor out the constants. Mention type: file and language: python once at the top in a _constants object, then list just the file names. Thousands of tokens saved on pure repetition.

After view showing 2,000 files compressed by factoring out constants. The _constants object lists type and language once, then files are just names: app.py, auth.py, config.py, etc.

Technique 3: Stabilize Your Prompt Prefix

Your system prompt says "You are a helpful assistant. Today is January 28th, 2025." Tomorrow it says January 29th. The beginning of your prompt changed by one word. Cache broken. Full reprice.

Move the date to the end of your prompt. The beginning stays identical every day. Anthropic gives 90% off cached tokens. OpenAI gives 50% off. Same prompt, large savings, just by reordering.

Technique 4: Cache Before You Compress

You compressed 1,000 files down to 15. The user asks about a JWT authentication file. It was not in your top 15. Normally you would say "I don't have that anymore."

Instead: before compressing, save the original 1,000 files in a local cache. Give your LLM a retrieval tool. When the user asks about JWT, the model calls that tool, searches the local cache, and pulls back the file. Nothing is ever lost.

Diagram showing the "Reversible Compression" pattern: compress files, cache the originals locally, then retrieve on demand when the LLM needs a file that was compressed away

You can compress aggressively because retrieval is always possible.

Key Takeaways

  • Compress logs and data statistically while preserving anomalies, errors, and outliers
  • Factor out repeated metadata fields into a single constants block
  • Keep the beginning of your system prompt stable to maximize cache hit rates
  • Always cache original data before compressing so the LLM can retrieve anything on demand

Resources

Published May 25, 2026. Writeup generated from a favorited TikTok.