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-03
Most AI systems can talk, but very few can actually do. This book is about closing that gap. Building Pragmatic AI Agents That Use Tools and APIs takes you beyond prompt tricks and prototype demos into the real engineering challenge of making models reliably call tools, orchestrate workflows, and interact with external systems under constraints like latency, failure, and evolving APIs. Through…

Minimum price

$19.00

$29.00

You pay

Author earns

$

Also available for 1 book credit with a Reader Membership

PDF
EPUB
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.

Forward-Looking Disclaimer: This book was written with the agent framework landscape as it exists in mid-2026. Model versions, pricing tiers, API surfaces, and feature availability change rapidly. Where this manuscript references specific model names (for example, “GPT-5” or “Claude Sonnet 4”), these are illustrative projections based on publicly announced roadmaps and should be treated as such. All framework documentation URLs and code examples have been verified against live sources at the time of writing, but API surfaces may evolve. The engineering principles, patterns, and trade-off analyses presented here remain valid regardless of which specific model versions or framework releases you are using.

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.

Share this book

Bundle

Bundles that include this book

Author

About the Author

Steve T. Publications

Steve T. is a cybersecurity leader, researcher, and engineer with more than 20 years of experience across application security, infrastructure security, vulnerability management, software development, and secure engineering practices. Having built his career alongside the growth of the modern internet, he has worked through multiple generations of technology, evolving security threats, and changing development methodologies.

He is currently part of the advanced research organization at a leading cybersecurity company, where he focuses on emerging threats, security innovation, and the practical application of research. His work involves investigating new attack techniques, evaluating emerging technologies, conducting deep technical analysis, and helping organizations better understand and manage complex security risks.

In addition to his research responsibilities, Steve leads a team of senior engineers and subject matter experts who create technical books, training programs, and educational resources for security professionals. Through this work, he helps engineers, developers, architects, and security practitioners strengthen their skills and build more secure systems.

Steve's technical expertise spans software development, reverse engineering, web application security, penetration testing, security architecture, incident response, vulnerability research, operating system internals, and secure software development. His ability to analyze systems at both the source code and binary levels enables him to bridge the worlds of software engineering, security research, and practical defense.

Over the course of his career, Steve has worked with organizations across a wide range of industries, helping them identify, assess, and remediate security weaknesses in critical applications and infrastructure. He is recognized for combining deep technical expertise with a pragmatic approach to security, focusing on solutions that are effective, sustainable, and aligned with business goals.

Through his work in research, engineering, leadership, and education, Steve continues to contribute to the advancement of cybersecurity and the development of secure, resilient technology systems.

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