Leanpub Header

Skip to main content

Building AI Agents with the OpenAI Agents SDK

A Complete Guide from Fundamentals to Production-Ready Systems

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

Learn how to build intelligent, production-ready AI agents with the OpenAI Agents SDK. Through practical Python examples and step by step guidance, you'll master everything from the fundamentals to advanced multi-agent workflows, tools, and deployment.

Minimum price

$19.00

$29.00

You pay

Author earns

$

Also available for 1 book credit with a Reader Membership

PDF
EPUB
WEB
APP
125
Pages
About

About

About the Book

This book teaches you how to build robust, scalable AI agent applications using the OpenAI Agents SDK. Starting from the very first line of code, you will progress through agent configuration, tool integration, multi-agent orchestration, safety guardrails, observability, and production deployment patterns. Every concept is explained with complete, runnable Python examples and detailed walkthroughs. The book assumes basic Python knowledge but no prior experience with AI agents or the OpenAI platform. By the end, you will be able to design and ship production-grade agentic systems with confidence.

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

A Complete Guide from Fundamentals to Production-Ready Systems

Introduction: The Agent Paradigm

  1. What Is an AI Agent?
  2. Why the OpenAI Agents SDK?
  3. Core Primitives at a Glance
  4. How to Use This Book

Chapter 1: Getting Started with the Agents SDK

  1. Installation and Environment Setup
  2. Your First Agent
  3. Understanding the Runner
  4. The RunResult Object

Chapter 2: Agent Configuration and Prompt Engineering

  1. Defining an Agent
  2. Dynamic Instructions
  3. Model Selection and Configuration
  4. Structured Outputs with Pydantic
  5. Prompt Templates and Platform Prompts

Chapter 3: Building Tools for Your Agents

  1. Function Tools with @function_tool
  2. Advanced Function Tool Patterns
  3. Hosted OpenAI Tools
  4. Tool Namespaces and Deferred Loading
  5. Local Runtime Tools

Chapter 4: Running Agents and Managing State

  1. The Agent Loop Explained
  2. Manual Conversation Management
  3. Session Memory
  4. Server-Managed Conversations
  5. Streaming Events and Cancellation
  6. Error Handling and Recovery

Chapter 5: Context Management and Dependency Injection

  1. The Context Pattern
  2. Passing Context Through Tools
  3. Dynamic Instructions from Context
  4. Lifecycle Hooks
  5. Usage Tracking and Token Accounting
  6. Cloning and Copying Agents
  7. Tool Input for Nested Agent Runs

Chapter 6: Multi-Agent Systems with Handoffs

  1. The Handoff Pattern Explained
  2. Configuring Handoffs
  3. Structured Handoff Data
  4. Input Filters and History Management
  5. Manager Pattern (Agents as Tools)
  6. Handoff Prompt Engineering
  7. Multi-Level Delegation

Chapter 7: Guardrails and Safety Controls

  1. Input Guardrails
  2. Output Guardrails
  3. Tool Guardrails
  4. Building Practical Guardrails
  5. Guardrails in Multi-Agent Workflows

Chapter 8: Observability and Tracing

  1. Automatic Tracing
  2. The Trace Dashboard
  3. Custom Spans and Manual Traces
  4. Sensitive Data Controls
  5. Third-Party Integrations
  6. Flushing Traces
  7. Non-OpenAI Tracing

Chapter 9: Human-in-the-Loop and Approval Workflows

  1. The Approval Flow
  2. Approving and Rejecting Tool Calls
  3. Streaming with Approvals
  4. Durable State Serialization
  5. Building Interactive Approval Interfaces

Chapter 10: Advanced Multi-Agent Orchestration

  1. Agents as Tools Deep Dive
  2. Structured Input for Tool Agents
  3. Custom Output Extraction
  4. Streaming Nested Agent Runs
  5. Conditional Tool Enabling
  6. Complex Orchestration Topologies

Chapter 11: Model Context Protocol (MCP) Integration

  1. What Is MCP?
  2. Local MCP Servers
  3. Hosted MCP Tools
  4. MCP and Tool Search
  5. Practical MCP Integration Patterns
  6. Agent-Level MCP Configuration

Chapter 12: Production Deployment and Best Practices

  1. Performance Optimization
  2. Cost Management
  3. Security Best Practices
  4. Testing Strategies
  5. Deployment Architectures
  6. Real-World Case Study: Customer Service Bot

Conclusion: The Future of Agentic AI

  1. Key Takeaways
  2. Emerging Capabilities
  3. Your Next Steps

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