Designing Intelligent Systems with Large Language Models
Introduction: The Context Problem
Chapter 1: Foundations: What Is Context Engineering?
- From Prompts to Systems: The Evolution of LLM Interaction
- Defining Context Engineering: Scope, Goals and Principles
- The Three Layers of Context: Task, Domain and System
- Why Naive Prompting Fails in Production
- The Cost of Context: Tokens as a First-Class Resource
Chapter 2: The Context Window: Architecture and Mechanics
- How Transformers Use Context: Attention and Positional Encoding
- KV Caching and Its Implications for Latency
- Sliding Windows, Long Context and Compression Strategies
- Positional Overflow and Context Degradation
- Choosing a Model Based on Context Requirements
- Key Takeaways
Chapter 3: Prompt Construction as Context Design
- Structural Patterns for Robust Prompts
- Few-Shot Design: Selection, Ordering and Calibration
- System Instructions vs. User Context
- Prompt Templates as Code: Versioning, Testing, Composition
- Common Anti-Patterns and How to Avoid Them
Chapter 4: Retrieval-Augmented Generation: Core Patterns
- The RAG Pattern: Architecture and Variants
- Document Chunking Strategies and Their Trade-offs
- Query Transformation: Rewriting, Expansion and Decomposition
- Re-ranking and Multi-stage Retrieval
- Evaluating Retrieval Quality Independently of Generation
- Advanced RAG Patterns: GraphRAG, Agentic Retrieval and Beyond
- RAG Failure Modes and Diagnosis
- Key Takeaways
Chapter 5: Vector Search and Semantic Indexing
- Embeddings: Selection, Fine-Tuning and Dimensions
- Vector Database Architectures: In-Memory vs. Persistent vs. Managed
- Approximate Nearest Neighbor Algorithms Explained
- Hybrid Search: Combining Semantic, Lexical and Metadata Filters
- Index Maintenance, Refresh Strategies and Consistency
Chapter 6: Memory Systems and State Management
- Short-Term vs. Long-Term Memory Patterns
- Conversation History Management and Summarization
- Persistent Knowledge: Entity Extraction and Knowledge Graphs
- Memory in Multi-turn Agents
- Privacy, Consent and Forgetting in Memory Systems
Chapter 7: Tool Use and the Model Context Protocol
- Tool Calling: Patterns, Schemas and Error Handling
- The Model Context Protocol: Architecture and Design Philosophy
- Building MCP Servers: Tools, Resources and Prompts
- Tool Discovery, Selection and Composition
- Security Considerations for Tool Execution
- Key Takeaways
Chapter 8: Agents, Planning and Reasoning Architectures
- Agent Architectures: From ReAct to Advanced Patterns
- Planning with Context: Decomposition and Subgoal Management
- Reflection, Self-Correction and Iterative Improvement
- Agent Reliability Patterns for Production Systems
- Multi-Agent Systems: Coordination and Communication Protocols
- When Agents Help and When They Hurt
Chapter 9: Structured Outputs and Controlled Generation
- Schema-Driven Output Design
- Constrained Decoding and Grammar-Based Generation
- Validation Pipelines: Pre- and Post-Generation Checks
- Structured Outputs in Streaming Contexts
- Trade-offs: Flexibility vs. Reliability
Chapter 10: Orchestration and Workflow Engines
- Workflow Patterns: Sequential, Parallel, Conditional, Loops
- Orchestration Frameworks: LangGraph, Temporal and Alternatives
- Error Handling, Retries and Circuit Breakers in LLM Workflows
- State Management Across Workflow Steps
- Designing Observable and Debuggable Workflows
- Key Takeaways
Chapter 11: Performance Engineering: Caching, Optimization, Latency
- Caching Strategies: Exact Match, Semantic, Prefix and Hybrid
- Token Optimization: Compression, Pruning and Rewriting
- Batching, Parallelization and Throughput Optimization
- Speculative Decoding and Draft Models
- Latency Budgeting and Performance SLOs
Chapter 12: Evaluation, Testing and Observability
- Offline Evaluation: Datasets, Metrics and LLM-as-Judge
- Online Evaluation: A/B Testing and Canary Deployments
- Regression Testing for Prompts, Retrieval and Workflows
- Observability: Tracing, Logging, Metrics and Dashboards
- Building a Continuous Evaluation Pipeline
- Key Takeaways
Chapter 13: Reliability and Safety: Hallucination Mitigation, Security, Governance
- Hallucination: Causes, Detection and Mitigation Strategies
- Prompt Injection and Indirect Attacks
- Data Privacy, PII Handling and Confidentiality
- Security for Tool Use and External Integrations
- Governance: Policies, Guardrails and Compliance
- Advanced Indirect Injection Variants
- MCP Security Hardening Checklist
- Key Takeaways
Chapter 14: Production Deployment at Scale
- Cloud-Native Deployment Architectures
- Scaling Patterns: Horizontal, Vertical and Hierarchical
- Multi-Region and Edge Considerations
- Cost Management and FinOps for LLM Systems
- Incident Response and Post-Mortem Patterns
- Key Takeaways
Chapter 15: Case Studies in Context Engineering
- Case Study 1: Optimizing Latency for a Fintech RAG System
- Case Study 2: Migrating from Monolithic Prompts to MCP-Based Agents
- Case Study 3: Building a Multilingual Customer Support System
- Case Study 4: Implementing Structured Outputs for a Legal Document Analysis Pipeline
- Case Study 5: Scaling a Research Agent System with Evaluation-Driven Iteration
Chapter 16: Frontiers of Context Engineering
- Scaling Laws for Long Context and Their Limits
- Multimodal Context: Vision, Audio and Beyond
- In-Context Learning Theory: What We Know and Do Not Know
- Neuro-Symbolic Integration and Structured Reasoning
- The Next Three Years: Trends to Watch
Conclusion: The Discipline of Context
References
Glossary
Index
- A
- B
- C
- D
- E
- F
- G
- H
- I
- K
- L
- M
- N
- O
- P
- Q
- R
- S
- T
- V
- W
- Y
- Z
