← AI Agents path

πŸ€– Using AI & AI Agents

Chapter 16 of 24

πŸ“¦ Chapter 16: Context Engineering

Managing context vs prompt engineering; types, strategies, and common failures

Context engineering is managing what information the agent has over time β€” unlike prompt engineering, which focuses on static instructions. Context includes instructions, knowledge (RAG, memory), tools and their results, conversation history, and user preferences. Strategies: scratchpad (session notes outside the context window), long-term memory, summarization and trimming, multi-agent context sharing, sandboxes for heavy computation, and runtime state objects. Avoid poisoning (validate and quarantine), distraction (summarize), confusion (fewer, relevant tools per task), and clash (prune conflicting instructions).

What goes into the context (and why it matters)

Multiple sources β†’ one context window

Instructions (system prompt, tool descriptions)
Knowledge (RAG results, long-term memory)
Tools (definitions + last tool results)
Conversation history
User preferences (if retrieved)
↓ context engineering: add / trim / summarize
Context window (limited size)
↓
LLM reads context β†’ plans next action or replies

Types of context

  • Instructionsβ€” System prompt, few-shot examples, tool descriptions.
  • Knowledgeβ€” Facts, RAG results, long-term memory.
  • Toolsβ€” Definitions + results of tool calls.
  • Conversation historyβ€” Ongoing dialogue (grows over time).
  • User preferencesβ€” Stored likes/dislikes for personalization.

Common context failures & fixes

Poisoning→ Validate before storing; quarantine bad context.
Distraction→ Summarize and trim; keep focus on recent/relevant.
Confusion→ Limit tools per task (e.g. RAG over tool list).
Clash→ Prune or override conflicting instructions.

Example: Book a trip to Paris

Simple agent: "When would you like to go?" Context-engineered agent: checks calendar, recalls preferred airline and budget from memory, sees available booking tools, then says: "You’re free the first week of October. Shall I search for direct flights on [Preferred Airline] within [Budget]?"