Leanpub Header

Skip to main content

Context Engineering for AI Agents

Context Engineering for AI Agents / Retrieval, Memory & Compression for AI Agents

Context Engineering for AI Agents

Prompt engineering got you a demo; context engineering ships it. This hands-on book teaches you to decide what goes into the model's context window — retrieval, memory, assembly, compression, caching — by building ctxkit, a real Python context

engine, one chapter at a time.

Minimum price

$19.00

$29.00

You pay

Author earns

$

Also available for 1 book credit with a Reader Membership

PDF
About

About

About the Book

For two years the advice was the same: learn prompt engineering. Craft the perfect instruction and the model does what you want. That advice was never wrong — but at production scale it quietly stopped being the bottleneck.

The models got bigger context windows and better instruction-following, and the hard part moved. When an agent fails in production today, it's almost never because someone chose the wrong adjective in a prompt. It fails because the *context*

was wrong: the retrieved documents were stale, the memory was truncated at the worst possible moment, the tool definitions were ambiguous, or the window was so stuffed with low-signal tokens that the model lost the plot.

Deciding what goes into the context window — in what order, at what cost, on every single turn — is now the central engineering discipline of building with LLMs. It has a name: context engineering. And unlike prompt engineering, it's a real

engineering problem: retrieval systems, memory stores, compression under a token budget, caching, measurement, and cost/latency trade-offs.

This book teaches it the way technical skills should be taught: by building. Across 16 chapters you construct ctxkit, a small but real Python context engine that grows one chapter at a time — retrieval, agentic multi-hop search, layered

memory, budget-aware assembly and compression, structured tool context, evals for context quality, and caching. By the end you've built the thing, not just read about it, and you recognize the same patterns inside every serious agent

framework.

What you'll build, chapter by chapter:

- The context window itself: tokens, budgets, and why position matters

- Retrieval done right: chunking, embeddings, a vector index, and agentic multi-hop search

- Memory systems: short-term, long-term, and episodic — with summary, vector, and knowledge-graph backends

- Assembly and compression that keep the highest-value context under a hard token budget

- Tool definitions and structured context that models actually parse reliably

- Long-horizon sessions that don't drift or forget

- Evals, cost accounting, and prompt caching to ship it for real

Who it's for: engineers building with LLMs — AI engineers, backend and platform engineers, anyone who has shipped a prompt to production and watched it behave nothing like the demo. You should be comfortable reading Python and have called an

LLM API before. No machine-learning background required; we treat the model as a component with a context window, a cost, and a latency budget, and engineer around it.

Prompt engineering got you a demo. Context engineering ships it.

Author

About the Author

CAIO INCAU

aio Incau é Engineering Manager com experiência liderando times de engenharia de software em larga escala. No dia a dia, combina gestão de pessoas com profundidade técnica para entregar produtos que impactam milhões de usuários. Começou a usar Claude Code por curiosidade, virou adepto por produtividade, e escreveu este livro para que outros desenvolvedores não precisem descobrir tudo sozinhos.                

                                                                                                                              

Contents

Table of Contents

  • Preface
  • Chapter 1 — Why Context Engineering Is the New #1 Job
  • Chapter 2 — Anatomy of the Context Window: Tokens, Budgets, and Attention
  • Chapter 3 — The Context Pipeline: Retrieval, Assembly, and Compression
  • Chapter 4 — Beyond Prompt Engineering: What Changes at Scale
  • Chapter 5 — Retrieval Foundations: Embeddings, Chunking, and Indexes
  • Chapter 6 — Agentic Retrieval: Query Planning and Tool-Driven Search
  • Chapter 7 — Memory Systems: Short-Term, Long-Term, and Episodic
  • Chapter 8 — Memory Architectures: Summaries, Vector Stores, and Knowledge Graphs
  • Chapter 9 — Context Assembly: Ordering, Prioritization, and Templates
  • Chapter 10 — Compression and Summarization Under a Token Budget
  • Chapter 11 — Tool Definitions and Structured Context
  • Chapter 12 — Managing Multi-Turn and Long-Horizon Context
  • Chapter 13 — Evaluating Context Quality
  • Chapter 14 — Cost, Latency, and Caching of Context Pipelines
  • Chapter 15 — Final Project: Building a Complete Context Engine
  • Appendix — Patterns Reference and Templates

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