Lesson 2 of 6 in Agents in Production · 9m

Context engineering

Long-context models still don't use every token equally. Information in the middle of a long prompt is used less reliably than information at the ends [1], and Anthropic describes context rot: as the context grows, recall degrades. They frame context as a finite attention budget and the job as finding the smallest set of high-signal tokens that gets the outcome you want [2].

Techniques

  • Just-in-time retrieval: keep lightweight references (file paths, queries, links) and load the data with tools only when needed, rather than stuffing everything in up front.

  • Compaction: when a conversation nears the limit, summarise it and continue in a fresh window. Clearing old tool results is the safest first step.

  • Structured note-taking: the agent writes notes to memory outside the context window and reads them back later.

  • Subagents: delegate focused work to subagents with clean contexts, and have them return condensed results [2].

Key takeaways

  • More context isn't always better; relevance is.
  • Load data just in time through tools.
  • Compact, take notes and delegate for long-running work.
Sources
  • Liu, N. F. et al. (2023). Lost in the Middle: How Language Models Use Long Contexts. TACL 2024 · arXiv:2307.03172.
  • Anthropic (2025). Effective context engineering for AI agents. Anthropic Engineering, September 2025.

Keep going with Agents in Production

This is one of 6 lessons in the course. Enroll to get the rest of the course, with an AI tutor that knows the material, flashcards and a certificate when you finish.

Enroll for free

More free lessons from this course