Leanpub Header

Skip to main content

Building AI Agents with the Claude Agent SDK

From Fundamentals to Production Deployments

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

Build AI agents that do more than generate text. This book shows you how to use the Claude Agent SDK to create autonomous systems that use tools, manage context, coordinate multiple agents, and solve complex tasks. With practical examples in Python and TypeScript, you will learn how to build secure, scalable agents ready for production.

Minimum price

$19.00

$29.00

You pay

Author earns

$

Also available for 1 book credit with a Reader Membership

PDF
EPUB
WEB
APP
154
Pages
About

About

About the Book

The Claude Agent SDK transforms Anthropic's Claude Code agent harness into a programmable library, giving developers access to the same autonomous tool execution, context management, and multi-agent orchestration that powers one of the most capable AI coding tools in existence. This book takes you from the fundamentals of AI agent architecture through advanced production patterns, with complete code examples in both Python and TypeScript. Whether you are building internal developer tools, customer-facing automation, or enterprise-grade multi-agent systems, you will learn the design patterns, security practices, and operational strategies that separate prototypes from production.

Bundle

Bundles that include this book

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 420 engineers and researchers. 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

From Fundamentals to Production Deployments

Introduction: The Agent Revolution

  1. What Are AI Agents and Why They Matter
  2. From Claude Code CLI to Agent SDK
  3. What You Will Build
  4. How to Read This Book

Chapter 1: AI Agent Fundamentals

  1. The Anatomy of an AI Agent
  2. Tool Use and Function Calling
  3. The Agent Loop
  4. Memory Architectures for Agents
  5. Planning and Reasoning Patterns
  6. Agent vs. Workflow: When to Use Each
  7. Chapter Summary
  8. Exercises
  9. Interview Questions

Chapter 2: SDK Architecture and Environment Setup

  1. The SDK Stack: Client SDK vs. Agent SDK
  2. Installation and Authentication
  3. Project Configuration and CLAUDE.md
  4. The Subprocess Model
  5. Multi-Language Support: Python and TypeScript
  6. Chapter Summary
  7. Exercises
  8. Interview Questions

Chapter 3: Core APIs and the Agent Loop

  1. The query() Function
  2. ClaudeSDKClient for Multi-Turn Conversations
  3. Message Types and Lifecycle
  4. Turn Management and Budget Controls
  5. Session Persistence and Resumption
  6. The Agent Loop in Action: First Complete Example
  7. Chapter Summary
  8. Exercises
  9. Interview Questions

Chapter 4: Built-In Tools and Tool Use

  1. File Operations: Read, Write, Edit
  2. Search and Discovery: Glob, Grep
  3. Command Execution: Bash and Monitor
  4. Web Tools: WebSearch and WebFetch
  5. User Interaction: AskUserQuestion
  6. Permission Modes and Tool Access Control
  7. Parallel vs. Sequential Tool Execution
  8. Chapter Summary
  9. Exercises
  10. Interview Questions

Chapter 5: Custom Tools and MCP Integration

  1. Defining Custom Tools in-Process
  2. Tool Result Formats
  3. MCP Server Integration
  4. Tool Search and Context Optimization
  5. Building a Complete Custom Tool: Weather Service Example
  6. Chapter Summary
  7. Exercises
  8. Interview Questions

Chapter 6: Memory Systems and Context Management

  1. The Context Window: Sizes, Limits, and Budgets
  2. Automatic Context Compaction
  3. CLAUDE.md: Project-Level Memory
  4. The Memory Tool
  5. Retrieval-Augmented Generation with Agents
  6. Context Optimization Strategies
  7. Chapter Summary
  8. Exercises
  9. Interview Questions

Chapter 7: Prompt Engineering for Agents

  1. System Prompts vs. User Prompts in Agent Context
  2. Writing Effective Agent System Prompts
  3. CLAUDE.md Mastery
  4. XML Structuring and Few-Shot Examples
  5. Extended Thinking and Effort Levels
  6. Prompt Caching Across Sessions
  7. Chapter Summary
  8. Exercises
  9. Interview Questions

Chapter 8: Subagents and Multi-Agent Systems

  1. Why Subagents: Context Isolation and Parallelism
  2. Programmatic AgentDefinition
  3. Automatic vs. Explicit Invocation
  4. Subagent Inheritance and Isolation
  5. Detecting and Resuming Subagents
  6. Dynamic Workflows for Large-Scale Orchestration
  7. Common Multi-Agent Patterns
  8. Chapter Summary
  9. Exercises
  10. Interview Questions

Chapter 9: Hooks and Human-in-the-Loop Patterns

  1. Hook Architecture and Execution Pipeline
  2. Lifecycle Events Reference
  3. Matcher Patterns and Filtering
  4. Blocking and Transforming Tool Calls
  5. Human Approval Workflows
  6. Async Hooks for Observability
  7. Anti-Patterns: Hook Pitfalls
  8. Chapter Summary
  9. Exercises
  10. Interview Questions

Chapter 10: Structured Outputs and Data Extraction

  1. Why Structured Outputs Matter
  2. Defining Output Schemas
  3. Multi-Tool Structured Extraction
  4. Error Handling for Schema Validation
  5. Best Practices and Anti-Patterns
  6. Chapter Summary
  7. Exercises
  8. Interview Questions

Chapter 11: Testing, Debugging, and Evaluation

  1. The Challenge of Testing Agents
  2. Unit Testing Tools and Hooks
  3. Promptfoo Integration
  4. MLflow Tracing and Evaluation
  5. Debugging Strategies
  6. Building Evaluation Datasets
  7. Continuous Evaluation in CI/CD
  8. Chapter Summary
  9. Exercises
  10. Interview Questions

Chapter 12: Observability and Performance Optimization

  1. The Three Signals: Metrics, Traces, Logs
  2. OpenTelemetry Integration
  3. Cost Tracking and Budget Management
  4. Performance Optimization Strategies
  5. Prompt Caching for Cost Reduction
  6. Observability Tooling Comparison
  7. Redacting PII and Secrets from Telemetry
  8. Chapter Summary
  9. Exercises
  10. Interview Questions

Chapter 13: Security and Sandboxing

  1. The Agent Threat Model
  2. Built-In Security Features
  3. Sandbox Runtime: bubblewrap and sandbox-exec
  4. Container Isolation Strategies
  5. Credential Management
  6. Filesystem Security
  7. Network Security
  8. Defense in Depth Checklist
  9. Chapter Summary
  10. Exercises
  11. Interview Questions

Chapter 14: Deployment, Scaling, and Production Operations

  1. Hosting Patterns
  2. Container Providers
  3. Session Persistence in Production
  4. Scaling Strategies
  5. Multi-Tenant Isolation
  6. Monitoring and Alerting in Production
  7. Known Limitations and Workarounds
  8. Chapter Summary
  9. Exercises
  10. Interview Questions

Chapter 15: Real-World Case Studies and Enterprise Architecture

  1. Case Study: Automated Code Review Pipeline
  2. Case Study: RAG-Powered Customer Support Agent
  3. Case Study: Research Assistant with Parallel Subagents
  4. Enterprise Integration Patterns
  5. Managed Agents vs. Self-Hosted SDK
  6. Comparison with Alternative Frameworks
  7. The Future of Agent Development
  8. Chapter Summary
  9. Exercises
  10. Interview Questions

Conclusion: Your Agent Journey Ahead

  1. Key Takeaways
  2. Building Your First Production Agent
  3. The Evolving Landscape
  4. Resources and Community

References

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