Leanpub Header

Skip to main content

Building Pragmatic AI Agents That Use Tools and APIs

Building Systems That Use Tools and APIs with DSPy, Pydantic AI, Claude SDK, OpenAI Agents SDK, and Google ADK

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

Most AI systems can talk, but few can actually do. This book shows you how to build AI agents that reliably use tools, call APIs and automate real workflows. Using DSPy, Pydantic AI, the Claude Agent SDK, the OpenAI Agents SDK and Google ADK, you'll learn practical patterns for building reliable agents that work in 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
206
Pages
About

About

About the Book

AI agents are everywhere, or at least everyone claims they are. But building an agent that reliably uses external tools, APIs, and databases to complete real tasks is a fundamentally different engineering challenge than writing prompts or fine-tuning models. This book bridges the gap between hype and reality. It walks you through five production-grade frameworks (DSPy, Pydantic AI, Claude Agent SDK, OpenAI Agents SDK, Google ADK), showing how each one approaches tool use, orchestration, and safety with runnable code examples, architectural comparisons, and hard-won lessons from teams deploying agents at scale.

On “War Stories” and Illustrative Scenarios: Throughout this book, you will encounter anecdotes framed as consulting experiences. These are composite scenarios built from documented production issues, community forums, and engineering postmortems across the agent ecosystem. They are intended for pedagogical illustration to demonstrate real failure modes and debugging patterns rather than as specific verifiable case studies of named organizations. The underlying technical lessons, however, reflect genuine production challenges that teams face when deploying agents at scale.

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 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

A Unified Guide to DSPy, OpenAI Agents SDK, Claude Agent SDK, Google ADK, and Beyond

  1. About This Book
  2. Copyright and Dedication

Chapter 1: Introduction: The Agent Revolution: Why Tools Change Everything

  1. A Tuesday Morning in September 2025
  2. What “Agentic AI” Actually Means (And What It Doesn’t)
  3. The Single Biggest Factor in Agent Success
  4. Why Tools Change Everything
  5. The Framework Landscape: Five Philosophies
  6. What This Book Will Do
  7. What This Book Won’t Do
  8. How to Read This Book

Chapter 2: The Anatomy of an AI Agent

  1. The Agent Loop: Observe, Reason, Act
  2. A Concrete Example: The Airline Agent
  3. Function Calling: The Foundational Primitive
  4. The Spectrum of Autonomy
  5. Deterministic vs. Probabilistic Control Flow
  6. Context Window Management: The Hidden Bottleneck
  7. A Composite Scenario: The Infinite Loop
  8. The MCP Layer: The Universal Language
  9. Chapter Summary

Chapter 3: Designing Tools That Agents Can Use Well

  1. The Anatomy of a Good Tool Definition
  2. Error Handling: Making Failures Informative
  3. Structured Outputs: When Tools Return Data, Not Just Text
  4. The Context Window Tax of Tool Descriptions
  5. Composite Scenario: The 50-Tool Agent (Context Window Overload Pattern)
  6. Tool Design Case Studies
  7. Anti-Patterns to Avoid
  8. Chapter Summary

Chapter 4: DSPy: Programming LLM Pipelines, Not Prompts

  1. The DSPy Philosophy: Signatures Over Prompts
  2. Building the Airline Agent: A Complete Walkthrough
  3. The ReAct Loop Internals: What DSPy Does Under the Hood
  4. Trajectory Inspection: Debugging the Agent’s Thinking
  5. Escalation: The File-Ticket Pattern
  6. Optimization: DSPy’s Secret Weapon
  7. Composite Scenario: Optimizing a Failing RAG Pipeline (DSPy Optimization Pattern)
  8. DSPy’s Design Philosophy: Why Signatures Over Prompts?
  9. Why DSPy’s Optimizers Work: The Mechanics of MIPROv2
  10. DSPy vs. Traditional Prompt Engineering: A Side-by-Side Comparison
  11. Debugging DSPy Agents: A Walkthrough
  12. DSPy in Production: Real-World Deployments
  13. DSPy’s Limitations: When to Avoid It
  14. DSPy vs. Pydantic AI: A Deeper Comparison
  15. When to Use DSPy (and When Not To)
  16. DSPy and MCP
  17. Inference-Time vs. Optimization-Time Trade-offs
  18. Chapter Summary

Chapter 5: Pydantic AI: Type-Safe Agents the Python Way

  1. Core Concepts: Agent as a Typed Container
  2. Five Execution Pathways
  3. Tools: @agent.tool vs @agent.tool_plain
  4. Dependency Injection via RunContext
  5. Structured Outputs with Pydantic Models
  6. Usage Limits and Cost Control
  7. Model Settings and Configuration
  8. Concurrency Limiting and Backpressure
  9. Streaming Modes: Three Levels of Visibility
  10. Self-Correction and Retry Budgets
  11. Observability: Logfire and OpenTelemetry
  12. Declarative Configuration: YAML Agent Specs
  13. Runs vs. Conversations: Message History
  14. Durable Execution: Surviving API Failures
  15. A Composite Scenario: Migrating from Flask to Pydantic AI
  16. Pydantic AI vs. OpenAI Agents SDK: A Deeper Comparison
  17. Pydantic AI vs. OpenAI Agents SDK: Guardrails Compared
  18. Pydantic AI’s Durable Execution: Production-Grade Reliability
  19. When to Use Pydantic AI (and When Not To)
  20. Chapter Summary

Chapter 6: Claude Agent SDK: In-Process Tools and Built-in Execution

  1. The query() Function: Your Entry Point
  2. Built-in Tools: The Complete Toolset
  3. Permission Modes: Controlling Autonomy
  4. Custom Tools: The @tool Decorator and In-Process MCP Servers
  5. Error Handling: isError vs Exceptions
  6. Tool Annotations: Behavioral Metadata
  7. Hooks: Intercepting Agent Behavior at Key Points
  8. Subagents: Spawning Specialized Agents from Within a Run
  9. Sessions and Multi-Turn Conversations
  10. MCP Server Integration
  11. Third-Party Provider Support
  12. SDK vs. Claude Code CLI vs. Managed Agents
  13. Claude Agent SDK’s In-Process MCP Servers: Why It Matters
  14. Claude Agent SDK’s Permission Modes: A Deep Dive
  15. Claude Agent SDK’s Hooks: Deterministic Control Over Probabilistic Behavior
  16. When to Use the Claude Agent SDK (and When Not To)
  17. Chapter Summary

Chapter 7: OpenAI Agents SDK: Lightweight Orchestration with Handoffs

  1. The Three Primitives: Agents, Handoffs, Guardrails
  2. Function Tools with Automatic Schema Generation
  3. Constraining Arguments with Pydantic Field
  4. Tool Timeouts and Error Handling
  5. Tool Context and Dependencies
  6. Hosted Tools: OpenAI’s Built-in Capabilities
  7. Tool Search for Deferred Loading
  8. Agents as Tools: A Second Multi-Agent Pattern
  9. Handoffs: The Primary Multi-Agent Pattern
  10. Guardrails: Input, Output, and Tool Validation
  11. Sandbox Agents: Isolated Execution
  12. Tracing and Observability
  13. Sessions: Persistent Memory
  14. MCP Server Integration
  15. Multi-Provider Support
  16. OpenAI Agents SDK’s Handoff Mechanism: How It Actually Works
  17. OpenAI Agents SDK vs. Pydantic AI: Runtime Overhead Comparison
  18. OpenAI Agents SDK’s Sandbox Agents: Security Through Isolation
  19. When to Use the OpenAI Agents SDK (and When Not To)
  20. Chapter Summary

Chapter 8: Google ADK: Graph-Based Workflows for Enterprise Scale

  1. Template Workflow Agents: The Foundation
  2. State Management: Shared State Namespace
  3. Custom Tools: FunctionTool and Beyond
  4. ADK 2.0: Graph-Based Workflows (Workflow Runtime)
  5. Dynamic Workflows: Code-Based Logic
  6. Collaborative Workflows: Coordinator Agents and Subagents
  7. Skills: Loading Domain Expertise on Demand
  8. Evaluation and Testing
  9. A2A Protocol: Cross-Framework Communication
  10. Deployment Options
  11. Multi-Language Support
  12. ADK for Android
  13. Google ADK’s Workflow Runtime vs. Template Agents: When to Use Which
  14. ADK’s Collaborative Workflows: Coordinator Agents and Subagents
  15. ADK’s Skills Toolset: On-Demand Domain Expertise
  16. Google ADK’s Multi-Language Support: Why It Matters
  17. When to Use Google ADK (and When Not To)
  18. Chapter Summary

Chapter 9: Cross-Framework Patterns: What Works Everywhere

  1. Pattern 1: Tool Design Principles That Are Framework-Agnostic
  2. Pattern 2: Error Handling Strategies: Three Levels
  3. Pattern 3: Observability and Tracing
  4. Pattern 4: Memory and Context Management
  5. Pattern 5: Security Considerations
  6. Pattern 6: Performance Optimization
  7. Pattern 7: The Human-in-the-Loop Spectrum
  8. Cross-Framework Migration: What Changes When You Switch
  9. Cost Comparison: Token Costs Per Framework/Model
  10. Community Health and Ecosystem Maturity
  11. Chapter Summary

Chapter 10: Productionizing Agent Systems

  1. Testing Strategies: A Multi-Layer Approach
  2. Cost Management: Seven Strategies
  3. Deployment Patterns
  4. Monitoring and Observability
  5. A/B Testing Agent Configurations
  6. Scaling Considerations
  7. Composite Scenario: From Prototype to 10k Daily Requests (Production Scaling Pattern)
  8. Security Hardening and Compliance
  9. CI/CD Integration for Agent Systems
  10. Cost/Latency Benchmarking Tables
  11. State Management Deep Dive
  12. Chapter Summary

Chapter 11: The Future of Agent Tool Use

  1. MCP Ecosystem Growth: What’s Certain
  2. Multi-Agent Collaboration Standards: What’s Emerging
  3. Fine-Tuning vs. Prompting for Tool Use: The Convergence
  4. Hardware Acceleration and Low-Latency Agents
  5. Speculation: What’s Next?
  6. The Economic Argument: When Do Agents Pay for Themselves?
  7. What’s Genuinely Uncertain
  8. Chapter Summary

Conclusion: Choosing Your Path

  1. The Decision Matrix: A Refined Guide
  2. The Pragmatic Approach: Seven Principles
  3. A Decision Framework: When to Use What
  4. The Economics of Agents
  5. The Framework Landscape: A Snapshot
  6. The Big Picture: Agents as Software Engineering

Glossary of Key Terms

Index

References

  1. Framework Documentation
  2. MCP Ecosystem
  3. MCP Security
  4. Academic Papers
  5. Industry Analysis and Additional Resources

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