LESSON 105 / 215·2 min readIntermediate

Token optimization, prompt compression, and context trimming

Should knowConcept lesson
01 Understand02 Recall03 Prepare

Words made simple

A few useful meanings before you read the explanation.

Token
A small piece of text processed by a model. It may be a whole word, part of a word, or punctuation.
RAG
Find relevant information first, then give it to a model to help answer the question. The answer is only as reliable as the information and checks used.
Grounding
Connecting an answer to supplied evidence, such as a policy document or a verified database result.
More words from this lesson
Agent
An application that uses a model to choose its next action within defined limits. It can use tools and continue through several steps.
Latency
How long a request takes to return a result. Lower latency usually means a shorter wait for the user.

🟠 SHOULD KNOW · Optimize after establishing an acceptable quality and security baseline.

Overview

  • Token optimization removes unnecessary model input/output while preserving task information.
  • Prompt compression shortens instructions or evidence; context trimming selects only relevant history and sources.
  • Shorter is useful only if important conditions survive.

Why this matters

Large prompts and long outputs increase cost, latency, and distraction.

An everyday example

Give an employee the relevant pages and a clear brief instead of a box of unrelated paperwork.

A technical example

Send claim status and missing-document codes rather than the full account record; cap requested explanation length.

How it works

  1. Measure token breakdown
  2. Remove repetition
  3. Select evidence
  4. Preserve exceptions/tool pairs
  5. Reserve output
  6. Rerun quality tests.

Architecture and flow

Measured prompt → relevance/minimization → bounded context → model → quality check.

The insurance assistant example

Use three applicable policy sections rather than a whole 100-page document for each question.

When to use it

High-volume chat, RAG, and agent tasks with growing histories.

When to avoid it

Do not compress away permissions, coverage limits, source references, or user-confirmed facts.

Interview answer

“I reduce irrelevant tokens and output verbosity, then verify that completeness and grounded accuracy remain acceptable.”

Benefits, trade-offs, and production considerations

A cheap answer requiring repeat attempts can cost more per successful task. Token reduction should be measured alongside task success, not pursued alone.

PAUSE & RECALL

Can you explain it without the notes?

Token optimization, prompt compression, and context trimming

Use your own words. Explain an example and one limitation before checking the explanation.

Check your explanation

Token optimization removes unnecessary model input/output while preserving task information. Prompt compression shortens instructions or evidence; context trimming selects only relevant history and sources. Shorter is useful only if important conditions…

CONNECT IT TO YOUR INTERVIEW

Turn this concept into a clear answer.

Practice this concept
YOUR NEXT STEP

Keep connecting the ideas.

Next lesson: Small versus large models and practical routing

Adapted from 13_AI.MD · “Token optimization, prompt compression, and context trimming”. Examples are study fragments; framework behavior can depend on your version.