Skip to content

AI 103 · Module 6

Context Optimization

Handing the right information to the agent at the right time

Start Section 1
0 / 6 completed
Sync my progressJoin a session to keep progress across devices and share it with your instructor

Info

After you join, your completed lesson numbers are sent to your instructor together with your learner code, so they can see your progress. Use the codes your instructor gave you, and do not enter your name, company secrets or other personal data. Progress is kept with the rest of that session's data.

Learning objectives

  • Explain why an agent with Memory, Knowledge and Skills still needs Context Engineering
  • Optimize the context for one task using the flow Select, Rank, Compress, Assemble Context, Execute, Update Memory
  • Recognize the six context failure modes and how to address each
  • In the Context Optimization Challenge, classify information as Remove, Keep, Retrieve Later, Summarize or Store as Memory

The problem and the optimization pipeline

10 min

With Memory, Knowledge and Skills, why is Context Engineering still needed?

So far an agent can remember preferences (Memory), retrieve knowledge (Knowledge) and act by a method (Skills). But together these resources can far exceed what one task can hold.

Core question

With Memory, Knowledge and Skills in place, why does an agent still need Context Engineering?

Each run, the model can process only a limited amount of context; that limited space is called the Context Budget. More information is not better: irrelevant content dilutes the point, stale content misleads, and contradictory content makes results unstable.

  • Context Selection: which information should this task use?
  • Context Prioritization: which matters most and belongs in the most prominent place?
  • Context Compression: what can be shortened without losing the point?
  • Context Quality: is what goes in relevant, trustworthy, current and safe?

Info

The job of Context Engineering is to hand the right information to the agent, at the right time, in the right amount, not to pour every resource in at once.

Key takeaway: However many resources exist, the context available for one task is limited and must be chosen and arranged deliberately.

10 min

The Context Optimization Pipeline

Optimization is not a one-off action but a flow that runs on every task.

Context Optimization Pipeline
  1. Available Information: take stock of everything on hand
  2. Select: pick what this task truly needs
  3. Rank: order by importance and trustworthiness
  4. Compress: shorten long content into a summary, keeping the essentials
  5. Assemble Context: combine what will be handed to the agent this time
  6. Execute: the agent performs the task
  7. Update Memory: after confirmation, write results or preferences worth keeping into Memory
The question at each step
StepQuestion to ask
SelectWithout it, would the task go badly?
RankWhen sources conflict, which prevails? Which matter most?
CompressAfter shortening, are the key facts, numbers and constraints still there?
Assemble ContextAre order, labels and sources clear?
ExecuteDoes the result need human review before use?
Update MemoryIs this worth remembering? Is it correct? Does it contain anything that should not be stored?

Warning

Before writing to Memory, confirm the content is correct, still valuable later, and free of passwords, secrets and unnecessary personal data. Memory can also go stale or be wrong, so a person reviews it regularly.

Key takeaway: Every task runs through: take stock, select, rank, compress, assemble, execute, update memory.

Compression and failure modes

10 min

Compression, Summarization, Compaction and Refresh

When information is needed but too long, it can be compressed. Different techniques address different problems.

Common ways to tidy context
TechniqueDescriptionRisk
SummarizationCondense long content into key pointsA summary may drop exceptions, numbers or constraints
Context CompactionIn a long conversation, fold older exchanges into a compact state record to free spaceDetails squeezed out may be needed again later
Context RefreshRe-retrieve or reload the latest version of information to replace old contentIf the source itself is out of date, refreshing does not help

A summary keeps "what you need now", but it is a lossy conversion. High-risk content (amounts, dates, legal clauses, safety rules) should not rest on a summary alone; keep a traceable link to the original source and let a person verify.

Warning

Compressed content is new context too, and needs checking for relevance, authority, freshness and sensitivity. A person verifies high-impact information.
Check yourself

A contract is 30 pages long, and the agent needs only its payment terms and penalty clauses. What is the best handling?

A contract is 30 pages long, and the agent needs only its payment terms and penalty clauses. What is the best handling?

Key takeaway: Compression must keep key facts and a traceable source; a person verifies high-risk content against the original.

10 min

The six context failure modes

When context is poorly designed, an agent's failure usually falls into one of six kinds. Learn to recognize them so you know where to fix.

Six failure modes
Failure modeWhat happensCommon remedy
Too Much ContextToo much information; the point is diluted and the Context Budget exceededReselect and rank; switch to retrieval when needed
Too Little ContextNecessary information is missing, so the agent guessesAdd the necessary background, or ask the agent to say what is missing
Wrong ContextThe information is unrelated to the task or about the wrong subjectCheck the Select step and refilter by relevance
Stale ContextInformation is out of date, such as an old version or old priceLabel date and version, and run a Context Refresh
Conflicting ContextTwo pieces of information contradict each otherDefine Source Authority in advance; a person confirms when in doubt
Untrusted ContextContent of unknown origin or unauthorized is used as a basisLimit to trusted sources; a person verifies high-impact conclusions

Info

These six often appear together. When diagnosing, ask first: is it too much, too little, wrong, stale, conflicting or untrusted?
Check yourself

The agent cited a withdrawn 2022 price quote even though a new 2025 version exists in the knowledge base. Which failure mode is this?

The agent cited a withdrawn 2022 price quote even though a new 2025 version exists in the knowledge base. Which failure mode is this?

Key takeaway: Identify which of the six failure modes it is first, then decide which step to fix.

The optimization challenge

5 min

Context Optimization Challenge

The scenario: an agent is helping prepare a quarterly procurement report (a simulated scenario), but its context is already stuffed with all sorts of information. You must sort each item into one of five categories.

The five categories
CategoryMeaningExample (simulated)
RemoveUnrelated to this task, or should not be provided; take it outDiscussion from an unrelated project, a login password
KeepNecessary for this task; keep it in the contextThis report's requirements and deadline
Retrieve LaterMay be needed, but large or frequently updated; fetch when neededThe full procurement policy library, live prices
SummarizeValuable but too long; shorten to a summary before includingThree months of meeting transcripts
Store as MemoryStill valuable in future; save after confirmationThe manager's usual report format
  1. Read the scenario
    Read through the stuffed context list before sorting anything.
  2. Classify each item
    Choose one category per item and write one sentence of reasoning.
  3. Check for sensitivity
    Confirm that passwords, personal data and unauthorized information are removed and not saved into Memory. In practice, use simulated data only.
  4. Compare and discuss
    Compare with classmates. The same item may be sorted differently; what matters is that the reasoning holds up.
Check yourself

"Three months of meeting transcripts" are valuable for this report but very long. What is the best category?

"Three months of meeting transcripts" are valuable for this report but very long. What is the best category?

Key takeaway: Every item needs a category and a reason: Remove, Keep, Retrieve Later, Summarize or Store as Memory.

Recap and next step

5 min

Recap and next step

Done

Context Optimization hands the right information to the agent, at the right time, in the right amount: select, rank, compress, assemble, then update Memory from the result, with a person reviewing and staying responsible.
Optimization flow recap
  1. Available Information
  2. Select
  3. Rank
  4. Compress
  5. Assemble Context
  6. Execute
  7. Update Memory

From Memory, RAG, Chunking, Knowledge and Skills to Context Optimization, you have covered the core ideas of designing agent memory, knowledge and skills. The next step is to bring them together on an Agent Memory, Knowledge & Skills Canvas: a person defines the task, AI assists in executing it, and a person reviews and owns the result.

Key takeaway: The point of optimization is not to stuff in more, but to give the agent the right information at the right time, with a person accountable.

What next?