Prompt Engineering Codex · CH 11

Chapter 11: Prompt Compression — Getting More From Fewer Tokens

⏱️ 4 min read·Updated Jul 2026

Why Token Efficiency Matters

At low volume, token costs are invisible. At scale, they define your margin.

A system that makes 10,000 AI calls per day with 500-token prompts at $15/1M output tokens costs approximately $75/day in prompt costs alone. Compressing that prompt to 200 tokens — without losing quality — cuts that cost to $30/day. That is $16,425/year saved on prompt costs alone.

Token efficiency also improves performance: shorter prompts leave more room in the context window for the model's reasoning. Less context clutter means better focus.

---

The Five Compression Techniques

Technique 1: Instruction Consolidation

Many prompts contain multiple instructions that say the same thing in different ways.

Before (112 words):

code
Please make sure you write code that is clean and well-organized. The code should follow best practices and be easy to read. Make sure you use proper variable names that are descriptive and explain what they are used for. The code should be structured in a logical way that is easy to follow. Please also make sure that you include comments where appropriate to explain what the code is doing, especially for complex sections.

After (34 words):

code
Write clean, well-structured code with descriptive variable names. Add inline comments for complex logic. Follow standard best practices.

The 78-word reduction produces identical output quality.

Technique 2: Implicit Knowledge Leverage

Do not explain things the model already knows deeply. Your prompt is not documentation for the model — it is instruction.

Before (overcautious explanations):

code
In React, components can either be class components or functional components. Modern React uses functional components with hooks. When writing a React component, make sure to use the useState hook for state management and the useEffect hook for side effects.

After (leverages model knowledge):

code
Write a modern React functional component with hooks. [actual task description]

Technique 3: Example Compression

Examples are valuable but often verbose. Use minimal examples that still capture the pattern.

Before (full example, 97 words):

code
For example, if I give you a sentence like "The quick brown fox jumped over the lazy dog," you should identify all the nouns (fox, dog) and verbs (jumped) and output them in a JSON format like this: {"nouns": ["fox", "dog"], "verbs": ["jumped"]}.

After (compressed example, 29 words):

code
Output JSON: {"nouns": [], "verbs": []}.
Example: "The fox jumped" -> {"nouns": ["fox"], "verbs": ["jumped"]}

Technique 4: Template Variables Instead of Inline Context

When a prompt has dynamic sections, use template variables and inject them programmatically.

Before (monolithic prompt):

code
You are reviewing code written by a junior developer named Sarah who works on the checkout team at an e-commerce company called ShopCo. The code is for a payment processing feature. Sarah has been at the company for 6 months and is learning TypeScript.

After (template with variables):

code
Code review for: {{reviewer_level}} developer, {{team}} team, {{language}} code.
Focus: {{focus_area}}.
Code: {{code}}

The template is shorter AND reusable across many different code reviews.

Technique 5: Format Shorthand

Define shorthand notations for repeated format patterns.

Before (repeated format instructions):

code
For each finding, write the line number, then a colon, then the severity level (which should be one of: Critical, High, Medium, or Low), then another colon, then a description of the issue in one sentence.

After (define format once):

code
Format each finding as: LINE:SEVERITY:ISSUE / FIX
Example: 42:High:Unescaped user input used in SQL query / Use parameterized queries

---

Measuring Compression Quality

Compression only has value if it maintains output quality. Test compressed prompts against the original:

javascript
async function measureCompressionQuality(originalPrompt, compressedPrompt, testInputs) {
  const results = await Promise.all(testInputs.map(async input => {
    const [original, compressed] = await Promise.all([
      callModel(originalPrompt, input),
      callModel(compressedPrompt, input),
    ]);
    return { input, original, compressed };
  }));
  
  const tokenReduction = (
    (originalPrompt.split(' ').length - compressedPrompt.split(' ').length) / 
    originalPrompt.split(' ').length * 100
  ).toFixed(1);
  
  console.log('Token reduction: ' + tokenReduction + '%');
  console.log('Compare outputs:', results);
}

A compressed prompt that degrades output quality on even 1 of your test cases is not worth using.

---

When NOT to Compress

Some prompts should not be compressed:

  • Safety-critical constraints: Never compress security rules, data handling requirements, or guardrails. The risk of ambiguity is too high.
  • Highly ambiguous tasks: Tasks that need examples to disambiguate should keep those examples.
  • First-time prompt testing: Compress after you have confirmed the full prompt works correctly.
The target is compression of filler and redundancy — not compression of clarity.

Finished reading this chapter?

Mark it completed to update your AI Builder skill profile score on your dashboard.

📋

Download Printable Chapter Checklist

Get a print-friendly, formatted checklist of workspace actions to apply this chapter offline.