Task 5.1
Context Optimization
Case facts blocks and position-aware ordering
📘What You Need to Know
The Finite Context Window Problem
Claude operates within a finite context window — approximately 200K tokens. As conversations grow, older information gets compressed, deprioritized, or lost entirely. Long support sessions, multi-step agent workflows, and extended code exploration all consume context until quality degrades.
Context optimization is not about fitting more text into the window. It is about ensuring the most important information survives compression and remains in high-recall positions where the model reliably attends to it.
Progressive Summarization Risk
Progressive summarization condenses earlier conversation into key points to free context space. This works for narrative flow — "customer called about billing, we discussed refund policy, awaiting decision." It fails catastrophically for reference data that must remain exact.
Each summarization round loses specificity. A detailed case becomes a vague summary after two rounds. By the third turn, the agent may forget the customer name, account number, order ID, and exact dollar amounts — the very data needed to resolve the issue.
# Original (turn 1)
"Customer John Smith (ACC-12345) reports order #98765
was charged $150.00 instead of promotional price $99.99.
Promotion code SUMMER2024 was applied at checkout."
# After summarization round 1
"Customer called about billing issue with promotion."
# After summarization round 2
"Customer has a billing issue."
# LOST: name, account, order number, amounts, promo codeNever summarize critical reference data. Progressive summarization is for narrative flow, not for facts that must stay exact.
Case Facts Blocks
Case facts blocks are immutable structured reference sections placed at the beginning of context. They contain all essential data as Field | Value pairs in a table format. They are never summarized, never compressed, and never modified during the conversation.
Place case facts at the START of context — the highest-recall position. When a support agent needs the order number on turn 15, it is still in the case facts block at the top, not buried in summarized conversation history.
## CASE FACTS (do not modify)
| Field | Value |
|------------------|--------------------|
| Customer | John Smith |
| Account ID | ACC-12345 |
| Order Number | #98765 |
| Charged Amount | $150.00 |
| Expected Amount | $99.99 |
| Promotion Code | SUMMER2024 |
| Issue Type | Billing overcharge |Lost-in-the-Middle Effect
Research shows that information in the middle of long contexts is less likely to be recalled than information at the beginning or end. Models attend strongly to recent messages (end) and initial instructions (beginning), but middle content — even important content — gets deprioritized.
Position-aware ordering places critical information at both the beginning AND end of context. Case facts go at the start. A brief restatement of active constraints goes at the end. Middle content is expendable narrative that can be summarized or trimmed.
- •Beginning: case facts block, system instructions, active constraints
- •Middle: conversation history, tool outputs (summarizable)
- •End: recent messages, restated active constraints, current task
- •Never place critical reference data solely in the middle
Lost-in-the-middle: place most important info at beginning AND end. Middle content is lowest-recall.
Trimming Verbose Outputs
Tool outputs and subagent results often arrive verbose — full API responses, complete file contents, lengthy search results. Trimming removes noise while retaining essential data before adding to context. Keep the answer; discard the exploration path.
Incremental codebase exploration (Glob → Grep → Read) is the tested pattern for avoiding context stuffing. List files first, search for patterns, then read only relevant files. Never dump entire codebases into context — it wastes tokens and buries critical details in the middle.
Context stuffing — loading entire codebases or full databases — is a Domain 5 anti-pattern. Be selective about what enters the window.
⚠️Exam Traps
Trap: Progressive summarization of critical customer details (names, IDs, amounts)
Why it's wrong: Each summarization round loses specific data points — agent forgets exact values needed to resolve issues.
Trap: Placing critical reference data only in the middle of long context
Why it's wrong: Lost-in-the-middle effect — models deprioritize middle content in long contexts.
Trap: Increasing max_tokens to fit more history instead of managing context
Why it's wrong: More tokens does not fix recall — it makes the lost-in-the-middle problem worse.
Trap: Loading entire codebases into context for exploration
Why it's wrong: Context stuffing dilutes attention — important details get buried and ignored.
✓Concept Check
A support agent forgets the customer's order number after 12 conversation turns. What is the best fix?
🔨Build Exercise
Beginner30 minutesDesign a Case Facts Block System
What you'll learn
- •Create immutable case facts templates for support scenarios
- •Apply position-aware ordering in long conversations
- •Demonstrate progressive summarization data loss