Context Engineering

Sources checked on 2026-07-21. Context windows, pricing, caching, conversation continuation, and compaction capabilities vary by provider, API, and model. For live limits, consult the official documentation, counting interfaces, and usage fields returned by the request.

Course overview

Context engineering is the engineering discipline of selecting, organizing, compressing, and tracing the full working state for a model decision. It covers more than a user prompt: stable instructions, tool definitions, MCP and tool results, retrieved evidence, message history, structured state, and memory. A prompt primarily answers “what to do”; context must also answer “what to rely on, what state the work is in, and which information is trustworthy and permitted.” Filling a window with an entire database or chat history does not automatically improve understanding. Irrelevant, conflicting, stale, and untraceable information reduces reliability and raises cost.

Where this course fits

This course follows Prompt Engineering and precedes LLM API Integration. It is a shared foundation for RAG, Agent memory, tool-result handling, and long-running conversation management. Complete Prompt Engineering first, then pass the context pack built here to LLM API Integration for a model call.

Learning objectives

  • Distinguish tokens, context windows, and budgets; never present character-count estimates as real token counts.
  • Select evidence by relevance, trustworthiness, freshness, and permission, while preserving provenance.
  • Design clear context partitions and ordering so that instructions, data, and state do not blur together.
  • Distinguish conversation records, structured state, long-term memory, and retrieved material.
  • Use just-in-time loading, trimming, summarization, provider compaction, and caching while verifying that important information was not lost.
  • Build long-context tests for position effects, conflicts, omissions, and prompt injection.

Prerequisites

  • Read JSON and simple Python.
  • Understand the task, input, and output contract in a prompt.
  • Embedding or RAG knowledge helps, but is not required to begin this course.
  1. Context Engineering: One Guide Is Enough: Build a panoramic view through What–Why–How and learn the four practices of writing, selecting, compressing, and isolating.
  2. Tokens, Context Windows, and Budgets: Understand capacity, consumption, and reserve headroom.
  3. Selection, Relevance, and Provenance: Decide which information deserves a place in the window.
  4. Organization, Order, and Trust Boundaries: Make the boundaries between instructions, evidence, and state legible to the model.
  5. Conversation History, State, and Memory: Manage a multi-turn task instead of replaying chat indefinitely.
  6. Trimming, Summarization, Compression, and Caching: Reduce redundancy without losing key constraints.
  7. Long-Context Failure Modes and Evaluation: Test position, conflict, omission, and contamination.
  8. Context Pack Project and Self-Test: Implement a deterministic budget selector.

Hands-on project

Complete the Context Pack project. It consists of the deterministic selector, versioned fixture, output schema, and unit tests. In order, it applies permission, trust, date, deduplication, and budget gates, and records an explicit reason for every exclusion.

Mastery criteria

  • Draw the context sources and trust boundaries for an Agent request.
  • Allocate a budget across inputs, outputs, and safety headroom, then calibrate estimates with a provider’s real token and usage data.
  • Explain why the most relevant chunk may not be the most trustworthy or most current.
  • Distill conversation history into verifiable task state while retaining commitments and open questions.
  • Design regression tests for evidence at the beginning, middle, and end; conflicting sources; and summary omissions.
  • Run all 18 offline tests and show that required context fails closed when it cannot pass a permission or budget gate, and that deduplication cannot cross section, permission, or trust boundaries.

Relationships to other courses

Key references