Building AI Agents: From Design Patterns to Production
Most AI agent demos work flawlessly, until real users arrive. The gap between a compelling prototype and a dependable system is where this book begins. Framing agent development as an engineering discipline, it centers on the core loop of Perceive, Plan, Act, and Observe as the foundation for building agents that are robust, interpretable, and scalable.
As a companion to Antonio Gulli’s Agentic Design Patterns, Building AI Agents carries the field’s emerging architectural vocabulary from concept into practice. Where the earlier volume defines the patterns, this book implements them: showing how to translate agent design principles into reliable, production-ready systems.
Organized around four proven architectural patterns -- ReAct, Chain-of-Thought, Reflection, and Plan-and-Execute -- the book takes a hands-on, pattern-first approach. Each chapter includes working code and contributes to the development of Atlas, a unified research and coding assistant that evolves from a minimal script into a production-grade multi-agent system.
Coverage spans tool integration, memory and state management, multi-agent orchestration, and system evaluation. Implementations are demonstrated across leading frameworks, including LangGraph, CrewAI, OpenAI’s Agents SDK, and Google’s ADK, with designs that generalize across major model providers such as OpenAI, Gemini, Claude, and Llama. The emphasis throughout is on transferable patterns rather than vendor-specific solutions.
This book is intended for software engineers integrating agents into production systems, AI/ML practitioners moving beyond chat-based interfaces, technical leads evaluating architectural tradeoffs, and advanced students working at the frontier of applied AI. Readers should be comfortable with Python and have a foundational understanding of large language models; all other concepts are developed in context.
Foreword....8
A Strategic Forecast (From a Top Industry Mind)....10
Prologue....12
Preface: Why Agents, Why Now....14
What This Book Is....14
What This Book Is Not....15
How to Read This Book....15
Who This Book Is For....15
Contents....17
1 Anatomy of an Agent....27
Introduction....27
Agents....27
Beyond Completions....28
The Four Patterns That Actually Matter....29
Tools, Memory, and State....30
When NOT to Use an Agent....32
Setup Lab: Your Development Environment....33
A Minimal Tool-Calling Loop (30 Lines)....35
The ReAct Pattern, Step by Step....37
Project: Atlas v0.1A Raw ReAct Agent....38
Key Takeaways....39
Conclusion....39
2 Prompt Architecture for Agents....40
Introduction....40
Prompts....40
Why Agent Prompts Are Different....41
The Four-Layer Prompt Architecture....42
Persona Design....45
Few-Shot Examples in System Prompts....46
Dynamic Context Injection....47
Prompt Versioning and Regression Testing....48
Version Control....48
Regression Testing....49
Extended Thinking and Effort Controls....49
Project: Atlas v0.2Prompt AB Testing....51
Online-Only Examples....52
Key Takeaways....52
Conclusion....53
3 Tools, Skills, and Structured Outputs....54
Introduction....54
Function Calling Across Providers....54
OpenAI....55
Google Gemini....55
Anthropic Claude....56
Structured Outputs with Pydantic....57
Using Structured Outputs with OpenAI....57
Using Structured Outputs with Gemini....58
From Tools to Skills....58
The Skill Base Class....59
Building Reusable Skills....60
WebSkill....60
FileSkill....61
CodeSkill....63
The Skill Registry....64
Putting It All Together....65
Tool Chaining and Error Handling....66
Retry with Backoff....66
Fallback Strategies....66
Declarative Skills (Markdown)....67
The Philosophy of Functional Skills....68
The Atomic Principle....68
The Result Architecture....68
Deterministic Safety....69
Project: Atlas v0.3A CLI Research Agent with Pluggable Skills....69
Online-Only Examples....70
Key Takeaways....70
Conclusion....71
4 Handoffs and Routines....72
Introduction....72
Handoffs and Routines....72
When Handoffs are Enough (and When They're Not)....73
OpenAI Agents SDKPrimary Implementation....73
Defining Specialist Agents....74
Running the Agent....75
A Preview of Guardrails....75
Google ADKSame Pattern, Different Framework....76
How the APIs Differ....77
Project: Atlas v0.4Customer Support Triage System....78
Online-Only Examples....78
Key Takeaways....78
Conclusion....79
5 Stateful Agent Graphs....80
Introduction....80
Graphs and Agents....80
Why Graphs?....81
LangGraph Fundamentals....81
Defining State....81
Building a Basic Two-Node Graph....82
Running the Graph....83
Error Recovery Patterns....83
Retry Nodes....84
Human-in-the-Loop Checkpoints....84
Persistence: SQLite and Postgres....85
Sidebar: Smolagents (HuggingFace)....86
Project: Atlas v0.5Self-correcting Code Review Agent....87
Online-Only Examples....87
Key Takeaways....87
Conclusion....88
6 Multi-agent Collaboration....89
Introduction....89
CrewAIPrimary Implementation....89
Defining Agents....89
Defining Tasks....91
Defining Crew....92
Execution Processes....93
The Two-Agent Debate Protocol....93
Microsoft Agent FrameworkSidebar....94
Enterprise Collaborative Agents....95
Microsoft Copilot Cowork....96
Google Gemini Enterprise Agent Platform....96
Anthropic Claude Enterprise....96
OpenAI Frontier....97
Project: Atlas v0.6Automated Newsletter Crew....97
Online-Only Examples....97
Key Takeaways....98
Conclusion....98
7 Model Portability: One Agent, Many Models....99
Introduction....99
Model Portability....99
The Vendor Lock-in Trap....100
LiteLLMThe Universal Adapter....100
Tool Calling with LiteLLM....101
Running Agents Locally with Ollama....101
What Breaks with Smaller Models....102
DSPyProgramming Instead of Prompting....103
Compiling and Optimizing....103
Cost and Latency Comparison....104
Project: Atlas v0.7Multi-model Benchmark....105
Online-Only Examples....105
Key Takeaways....106
Conclusion....106
8 Open Protocols: MCP, A2A, A2UI, and AP2....107
Introduction....107
MCP, A2A, A2UI, AP2....107
When to Reach for Each Protocol....108
MCP: One Integration, Every Framework....109
Building an MCP Server....109
Connecting an MCP Client....111
A2A: Agent-to-Agent Protocol....111
Agent Cards....111
Sending Tasks via A2A....112
A2UI: Agent-to-User Interface....113
How it Solves the Security Problem....113
AP2: Agent Payments Protocol....114
AP2 in Code....115
Why this Matters Architecturally....117
Project: Atlas v0.8Federated Knowledge Agent....117
Online-Only Examples....117
Key Takeaways....117
Conclusion....118
9 Declarative Agent Skills....119
Introduction....119
The Agent Skills Protocol....119
Anatomy of a Production Skill....120
Using Scripts in Skills....121
Building a Skill Loader....122
Wiring it into Atlas....124
Auto-optimization of Declarative Skills....125
The Core Problem: Why Hand-Crafted Skills Fail....126
Best Practices for Skill Creators....129
Treat Skills as Code, Not Config....129
Project: Atlas v0.9Declarative Skill Loading....130
Online-Only Examples....130
Key Takeaways....132
Conclusion....132
10 Building Agents with Claude Code and Google Antigravity....133
Introduction....133
Two Tools, One Job....133
The Spec-First Discipline....134
The AGENTS.md File....134
The ROADMAP.md File....135
Writing the Spec Is a Collaboration....136
Google Antigravity: Practical Patterns....137
Installation....137
Rules, Workflows, and Skills....138
Skills....139
Planning Mode Versus Fast Mode....140
The Agent Manager....140
MCP: Connecting External Tools....141
Project-Centric Architecture....142
Isolated Project Permissions....142
Built-in Slash Commands....143
Gemini CLI: Antigravity for the Terminal....143
Running Multiple Models....144
Claude Code: Practical Patterns....145
Installation....145
Hooks: Automating the Discipline....145
Plan Mode....146
Choosing the Right Model....147
Dynamic WorkflowsMassively Parallel Subagents....148
MCP: Connecting External Tools....149
What Not to Do....149
Don't Skip the Spec....150
Don't Start with Full Autonomy....150
Don't Let the Agent Pick Its Own Tools....150
Don't Evaluate Once and Ship....150
Don't Give the Agent a Wall of Context with No Structure....150
Eval Best Practices....151
Write Evals Before the Agent, Not After....151
What Makes a Good Eval Case....151
The Regression Rule....152
Evals as a ROADMAP Signal....152
Running Evals with Claude Code and Antigravity....153
Autonomy Levels: Picking the Right Mode....153
Test Coverage: The Non-negotiable Floor....154
What High Coverage Actually Means for Agent Code....154
Use the Agent to Write the Tests....156
Coverage Gate in CI....157
The Coverage Conversation with the Agent....157
Generating Documentation with AI....158
Make Docs Part of the Contract....158
Claude Code: In-Session Documentation....158
Gemini CLI: Batch Docs from the Shell....159
Keep AI-Generated Docs Honest....159
Project: Atlas v0.10Spec-First Setup....160
Online-Only Examples....160
Key Takeaways....161
Conclusion....161
11 Memory and Agentic RAG....162
Introduction....162
Memory....163
The Three Memory Layers....163
Vector Stores: Chroma and Qdrant....164
Managed Memory: Mem0 and Zep....166
The Persistent Scratchpad....167
RAG....169
RAG Engineering: Beyond Basic Vectors....169
Agentic RAG....171
Sidebar: Knowledge Graphs Versus Vectors....172
Project: Atlas v0.11Personal Assistant with Memory....173
Online-Only Examples....174
Key Takeaways....174
Conclusion....174
12 Code Execution and Sandbox Agents....176
Introduction....176
Why Code Execution Is Special....176
E2BCloud Sandboxes....177
Cost and Latency Management....179
Browser and Computer Use Agents....179
The Rise of Coding Agents....182
The Field....182
SWE-Bench: The Standard Benchmark....182
Project: Atlas v0.12Data Analyst Agent....183
Online-Only Examples....184
Key Takeaways....184
Conclusion....184
13 Multimodal and Voice Agents....186
Introduction....186
Image and Document Understanding....186
Passing Images to Agents....187
Real-Time Voice Agents....189
Interruption Handling....191
Latency Budget....191
Gemini Live API....191
Designing Multimodal Workflows....193
Input Detection and Routing....193
Why Not Just Use One Model For Everything?....195
Transcription Versus Native Audio Processing....195
Project: Atlas v0.13 Voice-Driven Document Analyst....196
Online-Only Examples....197
Key Takeaways....197
Conclusion....198
14 Guardrails and Agent Safety....199
Introduction....199
Guardrails....199
Two Types of Guardrails....200
Input Guardrails....200
Intent Classification....200
Prompt Injection Detection....201
Output Guardrails....201
PII Filtering....201
Hallucination Check....202
NeMo Guardrails....203
Wiring Guardrails into the Agent Loop....204
Defense in Depth....206
The 2026 Agentic Threat Model....207
Indirect Prompt Injection....207
The Confused Deputy....207
Non-human Identity (NHI) Risks....208
Cascading Failures in Multi-Agent Systems....208
Zero-Trust Architecture for Agents....208
The OWASP Top 10 for Agentic Applications....208
Project: Atlas v0.14Guardrailed Customer Support....209
Online-Only Examples....209
Key Takeaways....209
Conclusion....210
15 Agent Harness Engineering....211
Introduction....211
The Model Is the Brain. The Harness Is the Body....211
Five Layers, One Principle....212
The Execution Boundary....213
Sandboxed Execution....214
Memory Persistence....215
Verification Loops: Model Proposes, Code Disposes....215
Context Pipelines and Progressive Disclosure....216
Project: Atlas v0.15Agent Execution Harness....217
Online-Only Examples....218
Key Takeaways....218
Conclusion....219
16 Always-On Agents: Daemon Loops, Watchdogs, and Graceful Recovery....220
Introduction....220
Request-Response Versus Daemon Agents....220
The Poll Loop....221
What Kills Long-Running Agents....222
The Watchdog Pattern....223
Event-Driven Versus Poll-Driven....224
Graceful Shutdown....226
Google Antigravity: Platform-Managed Always-On Agents....226
Scheduled Workflows....227
Scheduling from the Chat....228
Trigger-Based Workflows....228
Agent Manager: Persistent Monitoring Agents....229
Project: Atlas v0.16Directory Monitor....229
Online-Only Examples....230
Key Takeaways....231
Conclusion....231
17 Managed Agents: Let the Platform Run It....232
Introduction....232
What Managed Actually Means....232
Claude Managed Agents....233
Google Cloud Gemini Managed Agents....236
AWS Bedrock Managed Agents....238
Choosing Your Platform....240
Project: Atlas v0.17Cloud-Managed Research Agent....241
Online-Only Examples....241
Key Takeaways....242
Conclusion....242
18 Evaluation and Observability....244
Introduction....244
Trajectory Evaluation....244
Tracing with LangSmith....245
Manual Instrumentation....245
Arize PhoenixOpen-Source Alternative....246
Building an Eval Suite....246
LLM-as-a-Judge....249
Red-Teaming Your Agent....250
Project: Atlas v0.18Eval Harness for Code Review Agent....250
Online-Only Examples....250
Key Takeaways....251
Conclusion....251
19 Deployment, Async Agents, and Security....252
Introduction....252
Serving Agents with FastAPI....252
LangGraph Platform....253
Local-First: Ollama vLLM....254
Ollama (Development)....254
vLLM (Production GPU Server)....254
What to Watch for with Smaller Models....255
Async and Long-Running Agents....255
Event-Driven Pattern: Wake Up Work Sleep....255
Webhook Trigger....256
Durable Execution....257
Idempotent Tool Calls....257
Checkpointing....257
The Agentic Threat Model....258
Token Cost Management....258
Docker Deployment....259
Project: Atlas v0.19Async Agent Service....260
Online-Only Examples....260
Key Takeaways....261
Conclusion....261
20 Loop Engineering: The System Is the Loop....263
Introduction....263
The Third Discipline....263
Anatomy of an Engineered Loop....264
The Harness That Learns....267
Loop Primitives in the Tools....268
The Claude Code Equivalents....269
Project: Atlas v0.20The Self-correcting Fix Loop....270
Online-Only Examples....270
Key Takeaways....271
Conclusion....271
21 Capstone: AtlasThe Autonomous Engineering Assistant....273
Introduction....273
Architecture: Pick Your Stack....273
Option A: LangGraph MCP Chroma (Maximum Control)....274
Option B: CrewAI MCP Mem0 (Faster to Prototype)....276
Option C: Google ADK Gemini Vertex AI (Google Stack)....277
Option D: OpenAI Agents SDK GPT (OpenAI Stack)....278
Option E: Anthropic Claude Managed Agents (Anthropic Stack)....280
Agent Implementations....281
Planner Node....281
Coder Node (with CodeSkill)....282
Tester Node (E2B Sandbox)....283
Reviewer Node (with Guardrails)....284
MCP Integration....285
A2A Federation....286
The Frontend....286
Streaming Thinking Indicators....286
Progress Cards....287
Deployment....288
Eval Suite....288
The Full Stack....289
Online-Only Examples....289
Key Takeaways....290
What This Book Was Really About....290
22 Whats Next: Agentic AI in 2026 and Beyond....292
Introduction....292
Agentic Reasoning Models (o1, o3, R1)....292
Why This Breaks ReAct....293
The Two Flavors of Self-improvement....293
What Self-improvement Actually Looks Like in Practice....294
The Darwin Gdel Machine (2025)....294
Hyperagents (March 2026)....295
The Karpathy Loop (March 2026)....295
The 27% Problem: Self-evolving Agents and the Enterprise Ceiling....295
Zero Human Companies and Accessible Agents....297
Breaking the Memory Wall....297
Sparse Attention and Cache Impact Performance....298
The Open Challenges....299
Online-Only Examples....299
Key Takeaways....300
Final Words....300
Conclusion: Crossing the Canyon to Production....301
The Loop Is the Agent....301
Empowering Through Simplicity: Choosing the Right Architecture....303
Prompts Are Code Now....303
Where This Leaves You....303
Appendix A Loop Engineering....305
The Infinite Improvement Loop: Redefining > 1000 Productivity Gains with Agentic AI....305
From Vibe Coding to Business AI Automation: The > 100 Leap....308
Appendix B The Harness Engineer: Review Less, Ship More....309
Introduction....309
The Inversion....309
The Adversarial Fleet....310
learnThe Command That Compounds....311
The Claude Code Equivalent....312
Taking It Outside the Codebase....313
What You Absolutely Need to Review....314
Key Takeaways....315
Conclusion....315
Self-evolving Agents: Six Papers Worth Your Time....316
GEPAReflective Prompt Evolution Can Outperform Reinforcement Learning....317
The Provocation in the Title....317
How It Works....317
Results....319
What This Means for You....319
SkillOptExecutive Strategy for Self-evolving Agent Skills....320
The Problem with Rewriting a Skill from Scratch Every Time....320
How It Works....320
Results....322
What You Need to Know....323
AlphaEvolveA Coding Agent for Scientific and Algorithmic Discovery....323
The Paper that Beat a 56-Year-Old Algorithm....324
How It Works....324
Results....325
How This Impacts Your Work....326
OpenEvolveAn Open-Source Reproduction of the AlphaEvolve Recipe....327
The One You Can Actually Install....327
How It Works....327
Results....329
Try It Yourself....329
The Implications for You....330
Next-Generation Agentic Reinforcement Learning Systems Enable Self-evolving Agents....331
The Paper that Argues the Bottleneck Isnt the Algorithm....331
The Three Pillars They Propose....331
AReaL2.0: The Proof of Feasibility, Not the Finished Substrate....332
Whats in It for You....333
SkillSmithLearning to Compose Parametric Skills and Textual Knowledge....333
The Paper that Refuses to Pick a Side....333
How It Works....334
Results....335
What This Means for You....335
The Impact on Your Experience....335
Key Takeaways....336
Conclusion....337
References....338
Index....339