A beginner-friendly guide to Context Engineering, window budgeting, rule framing, and knowledge snapshot management for AI agents.
context engineering is the art and science of constructing the exact context window payload required for maximum model accuracy and minimal token noise
- The Simple Idea
- Prompt Engineering vs Context Engineering
- The Anatomy of a Context Window
- Context Engineering Techniques
- My Learning Notes
- Common Misunderstandings
- Related Concepts
Prompt Engineering focuses on wording single user instructions.
Context Engineering manages the entire dynamic context payload fed to an LLM on every step of an agentic loop: system rules, workspace snapshots, Knowledge Items (KIs), active document states, tool output summaries, and memory frames.
System Rules + Workspace State + Active Files + KI Knowledge + Step History = Engineered Context
| Dimension | Prompt Engineering | Context Engineering |
|---|---|---|
| Focus | Wording a prompt string | Managing total context state & tokens |
| Scope | Single instruction | Multi-turn agent execution loop |
| Data Sources | User text input | ASTs, Knowledge Items, files, git diffs, logs |
| Optimization | Output clarity | Token density, relevance, zero distraction |
- System Identity & Constraints: Core agent rules, non-negotiable guidelines, and OS metadata.
- Repository Standards:
AGENTS.mdand repository-specific coding rules. - Knowledge Items (KIs): Relevant architectural summaries retrieved from persistent storage.
- Environment State: Active file path, cursor position, git branch.
- Condensed Execution History: Concise observations of completed tool calls.
- Rule Framing: Structuring rules with explicit priority hierarchy (
CRITICAL RULE: NEVER do X). - Dynamic File Slicing: Including exact line ranges (
L10-L45) rather than dumping full 5,000-line files. - Silent Log Inspection: Processing background outputs silently and injecting concise 2-line summaries into context.
- Context Compaction: Periodically truncating old tool outputs while preserving core system rules and target goals.
Context window bloat is the primary cause of model confusion and high API costs.
Context engineering principle:
every token in the context window must earn its place — maximize signal, eliminate noise
"Just throw the whole codebase into a 2M token window."
Massive context windows increase latency, cost, and introduce attention degradation ("lost in the middle"). Precise context engineering yields significantly better accuracy.
Previous: graph-engineering
Next: capability-engineering
Related: agent-memory, tool-calling