Leanpub Header

Skip to main content

Context Engineering

Designing Intelligent Systems with Large Language Models

This book is 100% completeLast updated on 2026-07-26

Go beyond prompts and learn how to build AI systems that hold up in production. This book shows how to make context the foundation of reliable LLM applications, covering practical patterns, real trade-offs and proven engineering techniques with clear examples you can put to work right away.

Minimum price

$19.00

$29.00

You pay

Author earns

$

Also available for 1 book credit with a Reader Membership

PDF
EPUB
WEB
APP
232
Pages
About

About

About the Book

This book teaches you how to design, build, and operate production systems powered by large language models by treating context as a first-class architectural concern. You will learn the theory behind how LLMs use context, the patterns that work in real systems, the trade-offs between competing approaches, and the engineering practices that separate fragile prototypes from reliable products. The material is organized to take you from foundational concepts through advanced production techniques, with complete code examples and detailed explanations of every major pattern in modern AI system design.

Author

About the Author

Steve Publications

Steve is a technology professional with more than 20 years of experience in software development, server infrastructure, cybersecurity, vulnerability research and reverse engineering. Throughout his career, he has designed, secured, analyzed and tested complex software and infrastructure, with a particular focus on understanding how systems fail and how they can be made more secure.

Outside of work, Steve enjoys sharing knowledge with the technology community. He collaborates with researchers, industry experts and technology professionals to write practical books covering software development, cybersecurity, cloud computing, networking, DevOps, artificial intelligence and enterprise technologies. His books focus on practical learning through clear explanations, real-world examples and hands-on exercises. With more than two decades of industry experience, his goal is to help IT professionals, students and technology enthusiasts build useful skills and stay current in a rapidly changing industry.

We believe readers deserve to know how our books are created. Most of our authors are not native English speakers, so we use AI to help translate, proofread manuscripts, fix grammar, improve sentence structure and make technical explanations easier to read. AI is used as an editing tool only. It does not replace the research, technical knowledge or hands-on experience behind our books. Some of our authors also prefer to remain anonymous for privacy or professional reasons. In those cases, we publish their work under a different name. The author's name may be different, but the quality of the content and our review process remain the same.

Every book is written, reviewed and maintained by experienced technology professionals, with contributions from our private technical community of more than 400 engineers and researchers from Ukraine, Belarus and Russia. We spend far more time validating technical accuracy and keeping our content up to date than generating text. We are always interested in working with experienced professionals who have deep expertise in a particular technology or domain. If you would like to publish a book with us or help review an existing manuscript, we'd love to hear from you. Send us a message describing your area of expertise. We are especially interested in niche technologies, specialized skills and emerging topics that are underrepresented in existing technical literature.

If you look through the contents of our books, you'll see practical examples, detailed explanations and material that is regularly updated. Our goal is to publish books that professionals can actually rely on, not low-effort AI-generated content. If you ever feel that one of our books does not meet that standard, Leanpub offers a 60-day money-back guarantee. Feel free to request a refund if you are not satisfied with your purchase.

Contents

Table of Contents

Designing Intelligent Systems with Large Language Models

Introduction: The Context Problem

Chapter 1: Foundations: What Is Context Engineering?

  1. From Prompts to Systems: The Evolution of LLM Interaction
  2. Defining Context Engineering: Scope, Goals, and Principles
  3. The Three Layers of Context: Task, Domain, and System
  4. Why Naive Prompting Fails in Production
  5. The Cost of Context: Tokens as a First-Class Resource

Chapter 2: The Context Window: Architecture and Mechanics

  1. How Transformers Use Context: Attention and Positional Encoding
  2. KV Caching and Its Implications for Latency
  3. Sliding Windows, Long Context, and Compression Strategies
  4. Positional Overflow and Context Degradation
  5. Choosing a Model Based on Context Requirements
  6. Key Takeaways

Chapter 3: Prompt Construction as Context Design

  1. Structural Patterns for Robust Prompts
  2. Few-Shot Design: Selection, Ordering, and Calibration
  3. System Instructions vs. User Context
  4. Prompt Templates as Code: Versioning, Testing, Composition
  5. Common Anti-Patterns and How to Avoid Them

Chapter 4: Retrieval-Augmented Generation: Core Patterns

  1. The RAG Pattern: Architecture and Variants
  2. Document Chunking Strategies and Their Trade-offs
  3. Query Transformation: Rewriting, Expansion, and Decomposition
  4. Re-ranking and Multi-stage Retrieval
  5. Evaluating Retrieval Quality Independently of Generation
  6. Advanced RAG Patterns: GraphRAG, Agentic Retrieval, and Beyond
  7. RAG Failure Modes and Diagnosis
  8. Key Takeaways

Chapter 5: Vector Search and Semantic Indexing

  1. Embeddings: Selection, Fine-Tuning, and Dimensions
  2. Vector Database Architectures: In-Memory vs. Persistent vs. Managed
  3. Approximate Nearest Neighbor Algorithms Explained
  4. Hybrid Search: Combining Semantic, Lexical, and Metadata Filters
  5. Index Maintenance, Refresh Strategies, and Consistency

Chapter 6: Memory Systems and State Management

  1. Short-Term vs. Long-Term Memory Patterns
  2. Conversation History Management and Summarization
  3. Persistent Knowledge: Entity Extraction and Knowledge Graphs
  4. Memory in Multi-turn Agents
  5. Privacy, Consent, and Forgetting in Memory Systems

Chapter 7: Tool Use and the Model Context Protocol

  1. Tool Calling: Patterns, Schemas, and Error Handling
  2. The Model Context Protocol: Architecture and Design Philosophy
  3. Building MCP Servers: Tools, Resources, and Prompts
  4. Tool Discovery, Selection, and Composition
  5. Security Considerations for Tool Execution
  6. Key Takeaways

Chapter 8: Agents, Planning, and Reasoning Architectures

  1. Agent Architectures: From ReAct to Advanced Patterns
  2. Planning with Context: Decomposition and Subgoal Management
  3. Reflection, Self-Correction, and Iterative Improvement
  4. Agent Reliability Patterns for Production Systems
  5. Multi-Agent Systems: Coordination and Communication Protocols
  6. When Agents Help and When They Hurt

Chapter 9: Structured Outputs and Controlled Generation

  1. Schema-Driven Output Design
  2. Constrained Decoding and Grammar-Based Generation
  3. Validation Pipelines: Pre- and Post-Generation Checks
  4. Structured Outputs in Streaming Contexts
  5. Trade-offs: Flexibility vs. Reliability

Chapter 10: Orchestration and Workflow Engines

  1. Workflow Patterns: Sequential, Parallel, Conditional, Loops
  2. Orchestration Frameworks: LangGraph, Temporal, and Alternatives
  3. Error Handling, Retries, and Circuit Breakers in LLM Workflows
  4. State Management Across Workflow Steps
  5. Designing Observable and Debuggable Workflows
  6. Key Takeaways

Chapter 11: Performance Engineering: Caching, Optimization, Latency

  1. Caching Strategies: Exact Match, Semantic, Prefix, and Hybrid
  2. Token Optimization: Compression, Pruning, and Rewriting
  3. Batching, Parallelization, and Throughput Optimization
  4. Speculative Decoding and Draft Models
  5. Latency Budgeting and Performance SLOs

Chapter 12: Evaluation, Testing, and Observability

  1. Offline Evaluation: Datasets, Metrics, and LLM-as-Judge
  2. Online Evaluation: A/B Testing and Canary Deployments
  3. Regression Testing for Prompts, Retrieval, and Workflows
  4. Observability: Tracing, Logging, Metrics, and Dashboards
  5. Building a Continuous Evaluation Pipeline
  6. Key Takeaways

Chapter 13: Reliability and Safety: Hallucination Mitigation, Security, Governance

  1. Hallucination: Causes, Detection, and Mitigation Strategies
  2. Prompt Injection and Indirect Attacks
  3. Data Privacy, PII Handling, and Confidentiality
  4. Security for Tool Use and External Integrations
  5. Governance: Policies, Guardrails, and Compliance
  6. Advanced Indirect Injection Variants
  7. MCP Security Hardening Checklist
  8. Key Takeaways

Chapter 14: Production Deployment at Scale

  1. Cloud-Native Deployment Architectures
  2. Scaling Patterns: Horizontal, Vertical, and Hierarchical
  3. Multi-Region and Edge Considerations
  4. Cost Management and FinOps for LLM Systems
  5. Incident Response and Post-Mortem Patterns
  6. Key Takeaways

Chapter 15: Case Studies in Context Engineering

  1. Case Study 1: Optimizing Latency for a Fintech RAG System
  2. Case Study 2: Migrating from Monolithic Prompts to MCP-Based Agents
  3. Case Study 3: Building a Multilingual Customer Support System
  4. Case Study 4: Implementing Structured Outputs for a Legal Document Analysis Pipeline
  5. Case Study 5: Scaling a Research Agent System with Evaluation-Driven Iteration

Chapter 16: Frontiers of Context Engineering

  1. Scaling Laws for Long Context and Their Limits
  2. Multimodal Context: Vision, Audio, and Beyond
  3. In-Context Learning Theory: What We Know and Do Not Know
  4. Neuro-Symbolic Integration and Structured Reasoning
  5. The Next Three Years: Trends to Watch

Conclusion: The Discipline of Context

References

Glossary

Index

  1. A
  2. B
  3. C
  4. D
  5. E
  6. F
  7. G
  8. H
  9. I
  10. K
  11. L
  12. M
  13. N
  14. O
  15. P
  16. Q
  17. R
  18. S
  19. T
  20. V
  21. W
  22. Y
  23. Z

Get the free sample chapters

Click the buttons to get the free sample in PDF or EPUB, or read the sample online here

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earned over $15 million writing, publishing and selling on Leanpub.

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub