Leanpub Header

Skip to main content

OPENAI AGENTS SDK AND AGENTIC APPLICATION ENGINEERING

A Researcher’s and Developer’s Textbook on Agent Architecture, Tool Orchestration, the Responses API, and Migration from the Assistants API

OPENAI AGENTS SDK AND AGENTIC APPLICATION ENGINEERING
This book is 100% completeLast updated on 2026-11-21

Build intelligent applications. Orchestrate AI agents. Engineer reliable workflows.

Discover how to design, implement, evaluate, and deploy modern agentic applications using the OpenAI Agents SDK and Responses API.

This comprehensive 19-chapter textbook explores agent architecture, function tools, multi-agent orchestration, sessions, memory, guardrails, tracing, evaluation, and migration from the legacy Assistants API.

Featuring five practical engineering projects, structured explanations, implementation guidance, and research-oriented learning, this book helps developers, students, and researchers move beyond simple AI interactions toward reliable agentic software systems.

RESEARCH • DESIGN • IMPLEMENT • EVALUATE • DEPLOY

Minimum price

$12.99

$19.99

You pay

Author earns

$

Also available for 1 book credit with a Reader Membership

PDF
EPUB
About

About

About the Book

About the Book

OpenAI Agents SDK and Agentic Application Engineering: A Researcher's and Developer's Textbook on Agent Architecture, Tool Orchestration, the Responses API, and Migration from the Assistants API is a comprehensive technical textbook for understanding, designing, implementing, evaluating, and deploying modern agentic AI applications.

Artificial Intelligence is evolving beyond conventional question-and-answer interactions toward intelligent applications capable of using tools, managing context, coordinating specialist agents, processing structured information, and executing multi-step workflows.

Building such applications requires more than connecting a language model to a prompt. It requires a systematic understanding of agent architecture, execution runtimes, API integration, tool contracts, state management, security boundaries, testing, observability, and responsible engineering.

This book uses the OpenAI Agents SDK as its primary agent-development framework and explores the Responses API as a foundation for modern model interactions. It also examines architectural choices between SDK-managed agent orchestration and direct API integration.

A dedicated chapter explains the historical Assistants API architecture and the engineering considerations involved in migrating legacy implementations to modern development approaches.

What Does This Book Cover?

Across 19 comprehensive chapters, readers explore:

  • Foundations of Agentic AI: Intelligent applications, agent environments, goals, tools, actions, and the agent lifecycle.
  • Agent Architecture and Design: Model configuration, instructions, context, planning, task decomposition, memory, and architectural patterns.
  • OpenAI Platform Fundamentals: API authentication, model selection, HTTP concepts, SDK configuration, rate limits, and credential management.
  • Responses API: Request structures, response items, tool calling, conversation continuity, structured outputs, and streaming.
  • OpenAI Agents SDK: Installation, project structure, agent configuration, execution, and reproducible development environments.
  • Agents and Runner Execution: Agent definitions, runtime context, synchronous and asynchronous execution, streaming events, and run management.
  • Tools and API Integration: Function tools, parameter schemas, input validation, external services, retries, idempotency, and permission boundaries.
  • Multi-Agent Orchestration: Specialist agents, handoffs, agents-as-tools, manager-style orchestration, routing, delegation, and shared context.
  • Sessions and Memory: Conversation state, session management, persistence, context windows, summarisation, privacy, and data retention.
  • Guardrails and Safe Execution: Input and output validation, human approval, prompt injection defence, sensitive actions, and failure containment.
  • Tracing and Observability: Execution traces, spans, debugging, logging, metrics, dashboards, and privacy-aware monitoring.
  • Testing and Evaluation: Unit testing, integration testing, evaluation datasets, task success, tool-use quality, regression testing, security, and performance analysis.
  • Assistants API Migration: Legacy architecture, concept mapping, migration planning, tool refactoring, regression testing, and post-migration validation.
Five Practical Agent Engineering Projects

The book goes beyond conceptual explanations by introducing five application-oriented projects:

  1. Personal Research and Knowledge Agent: Research planning, search tools, evidence capture, summarisation, and structured reports.
  2. Document Analysis and Data Extraction Agent: Document processing, structured extraction, validation, confidence checks, and human review.
  3. Multi-Agent Software Engineering Workflow: Architect, developer, and reviewer agents working through coordinated development tasks.
  4. Enterprise Workflow and Approval Agent: Business rules, role-based access, approval gates, audit trails, and exception handling.
  5. Customer Support and Service Desk Agent: Knowledge retrieval, request classification, specialist routing, response generation, and human escalation.
What Makes This Book Valuable?

This textbook combines research-oriented thinking with practical software engineering.

Instead of treating agents as unrestricted autonomous systems, it presents them as engineered applications operating within explicit responsibilities, permissions, validation rules, and measurable performance criteria.

The learning approach emphasizes:

  • Understanding the architecture before implementation.
  • Designing explicit and testable tool interfaces.
  • Selecting suitable orchestration strategies.
  • Managing state and conversational continuity.
  • Evaluating reliability and execution quality.
  • Protecting credentials and sensitive information.
  • Implementing human oversight for consequential actions.
  • Documenting experiments and maintaining reproducibility.
Who Should Read This Book?

This book is particularly suitable for:

  • Computer Science, Information Technology, and AI students.
  • Undergraduate and postgraduate researchers.
  • Python developers and software engineers.
  • AI application developers and automation engineers.
  • Software architects and technical leads.
  • API integration and enterprise application developers.
  • Technical educators and professional trainers.
  • Researchers exploring multi-agent systems and human-AI interaction.
Learning Outcomes

After studying this book, readers should be able to design agentic application architectures, configure agents using the OpenAI Agents SDK, integrate model capabilities through the Responses API, develop function tools, implement multi-agent workflows, manage sessions and state, apply guardrails, evaluate agent performance, troubleshoot execution through tracing, and plan migrations from legacy Assistants API implementations.

They will also gain experience in designing research agents, document-processing agents, multi-agent software workflows, enterprise approval systems, and customer support applications.

OpenAI Agents SDK and Agentic Application Engineering is designed as both an academic learning resource and a practical engineering reference for readers who want to move from experimenting with AI models to building reliable, secure, and maintainable agentic applications.

RESEARCH • DESIGN • IMPLEMENT • EVALUATE • DEPLOY

Author

About the Author

Anshuman Mishra

Anshuman Kumar Mishra, M.Tech (Computer Science) Assistant Professor, Doranda College, Ranchi University

Prolific Author of 50+ Books on AI, Machine Learning & Computer Science | 20+ Years Experience

Anshuman Kumar Mishra is a dedicated educator, researcher, and highly prolific author with over 20 years of experience in Computer Science and Information Technology. Holding an M.Tech in Computer Science from BIT Mesra, he brings a rare combination of academic depth and practical teaching expertise.

Currently serving as Assistant Professor at Doranda College under Ranchi University, he has mentored thousands of students, helping them build strong foundations in programming, data science, and artificial intelligence. His student-centric teaching style emphasizes conceptual clarity, hands-on practice, and real-world application.

Anshuman is a prolific author with more than 50 books published across a wide spectrum of computer science and emerging technology domains. From foundational programming languages to advanced topics in Artificial Intelligence, Machine Learning, Reinforcement Learning, Decision Theory, and Computer Vision — his books are widely appreciated by students, educators, and professionals for their clear explanations, strong theoretical foundation, and practical approach.

His extensive body of work reflects his deep commitment to making complex subjects accessible and meaningful for learners at all levels. He is particularly recognized for creating well-structured learning paths that help readers progress from beginner to advanced levels with confidence.

Driven by the mission to democratize quality technical education, Anshuman continues to write and update books that bridge the gap between academic theory and industry practice.

When not teaching or writing, he actively follows and explores new developments in AI, Quantum Machine Learning, and Ethical Intelligence systems.

Contents

Table of Contents

CONTENTS Chapter and section page numbers refer to the pagination of this complete edition. CHAPTER 1 — INTRODUCTION TO AGENTIC AI AND INTELLIGENT APPLICATIONS 14 1.1 Evolution of Artificial Intelligence Applications 14 1.2 From Generative AI to Agentic AI 17 1.3 Definitions: Agent, Environment, Tool, Goal, and Action 18 1.4 Core Characteristics of an AI Agent 20 1.5 Agent Lifecycle: Observe, Reason, Act, and Evaluate 22 1.6 Agentic Systems in Research, Industry, and Education 24 1.7 Benefits, Limitations, and Engineering Challenges 25 1.8 Practical Implementation: A Bounded Python Agent Simulation 27 1.9 Engineering Architecture: From Demonstration to Application 34 1.10 Design Checklist for a First Agentic Application 35 1.11 Chapter Summary 35 1.12 Key Terms 36 1.13 Review Questions 37 1.14 Practical and Research Assignments 37 CHAPTER 2 — AGENT ARCHITECTURE AND DESIGN PRINCIPLES 39 2.1 Fundamental Components of an Agentic System 39 2.2 Model, Instructions, Context, and Runtime 43 2.3 Planning, Task Decomposition, and Decision-Making 46 2.4 Tool Use and External System Interaction 49 2.5 State, Memory, and Context Management 51 2.6 Deterministic Workflows versus Model-Driven Workflows 54 2.7 Architectural Patterns and Design Trade-offs 57 2.8 Chapter Summary 59 CHAPTER 3 — OPENAI PLATFORM AND API FUNDAMENTALS 63 3.1 OVERVIEW OF THE OPENAI DEVELOPER PLATFORM 65 3.2 API AUTHENTICATION AND CREDENTIAL MANAGEMENT 68 3.3 MODELS, MODEL IDENTIFIERS, AND CAPABILITY SELECTION 71 3.4 REQUESTS, RESPONSES, AND JSON DATA STRUCTURES 73 3.5 HTTP, SDKS, ENVIRONMENT VARIABLES, AND CONFIGURATION 77 3.6 RATE LIMITS, ERRORS, RETRIES, AND TIMEOUTS 80 3.7 API SECURITY AND LEAST-PRIVILEGE PRINCIPLES 84 3.8 CHAPTER SUMMARY AND PRACTICAL EXERCISES 87 CHAPTER 4 — RESPONSES API: CORE CONCEPTS AND IMPLEMENTATION 92 4.1 ROLE AND POSITION OF THE RESPONSES API 93 4.2 REQUEST STRUCTURE AND INPUT TYPES 95 4.3 OUTPUT ITEMS, MESSAGES, AND RESPONSE HANDLING 98 4.4 TOOL CALLING AND FUNCTION-CALL CYCLES 101 4.5 CONVERSATION CONTINUITY AND STATE MANAGEMENT 105 4.6 STRUCTURED OUTPUTS AND SCHEMA-CONSTRAINED RESPONSES 107 4.7 STREAMING AND INCREMENTAL OUTPUT PROCESSING 109 4.8 ERROR HANDLING, TESTING, AND PRACTICAL EXERCISES 112 CHAPTER 5 — OPENAI AGENTS SDK: SETUP AND PROJECT STRUCTURE 119 5.1 Agents SDK Overview and Design Goals 119 5.2 Python Environment and Dependency Management 121 5.3 Installing and Verifying the SDK 122 5.4 Project Layout and Configuration Management 124 5.5 API Key Handling and Environment Setup 126 5.6 First Agent Application 127 5.7 Running, Inspecting, and Extending the Example 129 5.8 Version Pinning and Reproducible Setup 133 CHAPTER 6 — AGENTS, INSTRUCTIONS, MODELS, AND CONTEXT 138 6.1 The Agent Abstraction 138 6.2 Defining Agent Instructions and Responsibilities 140 6.3 Model Configuration and Generation Settings 141 6.4 Dynamic Instructions and Runtime Context 143 6.5 Input and Output Design 145 6.6 Typed Outputs and Data Validation 146 6.7 Agent Composition and Reusability 149 6.8 Design Exercises and Chapter Review 151 CHAPTER 7 — RUNNER EXECUTION AND AGENT LIFECYCLE 155 7.1 Runner Responsibilities and Execution Model 155 7.2 Starting and Managing an Agent Run 157 7.3 Run Results and Final Output 159 7.4 Tool Calls and Handoff Events During Execution 160 7.5 Synchronous and Asynchronous Execution 162 7.6 Streaming Events and Intermediate Results 164 7.7 Exceptions, Cancellation, and Recovery 166 7.8 Execution Tracing and Practical Exercises 168 CHAPTER 8 — TOOLS, FUNCTION SCHEMAS, AND INTEGRATIONS 173 8.1 The Role of Tools in Agentic Applications 175 8.2 Defining Function Tools 176 8.3 Tool Names, Descriptions, and Parameter Schemas 179 8.4 Input Validation and Output Normalisation 183 8.5 Connecting External APIs and Services 186 8.6 Tool Errors, Retries, and Idempotency 189 8.7 Permission Boundaries and Side-Effect Management 191 8.8 Tool Testing and Documentation 194 CHAPTER 9 — MULTI-AGENT ORCHESTRATION AND HANDOFFS 205 9.1 Motivation for Multi-Agent Systems 207 9.2 Specialist Agents and Responsibility Separation 210 9.3 Handoffs and Control Transfer 212 9.4 Agents-as-Tools and Manager-Style Orchestration 215 9.5 Routing, Delegation, and Result Aggregation 219 9.6 Shared Context and Inter-Agent Communication 221 9.7 Coordination Failures and Loop Prevention 224 9.8 Architecture Comparison and Practical Exercises 226 CHAPTER 10 — SESSIONS, CONVERSATION STATE, AND MEMORY 236 10.1 Conversation State and Continuity Requirements 238 10.2 SDK Session Concepts 240 10.3 Managing Context Across Multiple Turns 245 10.4 Short-Term State and Long-Term Memory Design 249 10.5 Persistence, Storage, and Data Retention 252 10.6 Context Window Management and Summarisation 256 10.7 Privacy, User Consent, and Data Minimisation 259 10.8 State Management Testing 262 CHAPTER 11 — GUARDRAILS, HUMAN APPROVAL, AND SAFE EXECUTION 274 11.1 Why Agentic Systems Need Guardrails 275 11.2 Input and Output Validation 277 11.3 Tool-Level and Workflow-Level Constraints 281 11.4 Human-in-the-Loop Approval Patterns 283 11.5 Handling Untrusted Inputs and Prompt Injection 286 11.6 Sensitive Actions and Permission Controls 288 11.7 Failure Containment and Safe Fallbacks 290 11.8 Safety Testing and Auditability 292 CHAPTER 12 — TRACING, DEBUGGING, AND OBSERVABILITY 300 12.1 Observability Requirements for Agentic Systems 301 12.2 Traces, Spans, and Execution Events 303 12.3 Inspecting Agent Decisions and Tool Activity 306 12.4 Debugging Incorrect or Incomplete Runs 307 12.5 Logging, Metrics, and Operational Dashboards 309 12.6 Privacy-Aware Logging and Secret Redaction 311 12.7 Reproducing and Diagnosing Failures 314 12.8 Observability Exercises 316 CHAPTER 13 — TESTING, EVALUATION, SECURITY, AND PERFORMANCE 321 13.1 Unit, Integration, and End-to-End Testing 322 13.2 Test Cases for Normal, Edge, and Adversarial Inputs 325 13.3 Evaluation Datasets, Rubrics, and Metrics 327 13.4 Task Success, Accuracy, and Tool-Use Quality 330 13.5 Latency, Token Use, and Cost Analysis 332 13.6 Reliability, Retries, and Regression Testing 334 13.7 Security, Privacy, and Threat Modelling 336 13.8 Evaluation Reports and Reproducibility 338 CHAPTER 14 — ASSISTANTS API: LEGACY ARCHITECTURE AND MIGRATION 346 14.1 Historical Overview of the Assistants API 347 14.2 Assistants, Threads, Messages, and Runs 349 14.3 Legacy Tools and Execution Patterns 351 14.4 Key Differences: Assistants API and Responses API 353 14.5 Mapping Legacy Concepts to Current Architecture 355 14.6 Migration Planning and Application-State Review 357 14.7 Refactoring Tools, Data, and Conversation Workflows 359 14.8 Regression Testing, Cutover, and Post-Migration Validation 362 14.9 Migration Risks, Limitations, and Lessons Learned 364 CHAPTER 15 — PROJECT 1: PERSONAL RESEARCH AND KNOWLEDGE AGENT 372 15.1 Project Objectives and Requirements 372 15.2 System Architecture and Component Design 374 15.3 Research Query Planning and Task Decomposition 376 15.4 Designing Search and Retrieval Tools 379 15.5 Source Evaluation and Evidence Capture 381 15.6 Summarisation and Structured Research Reports 383 15.7 Verification, Testing, and Error Analysis 386 15.8 Security, Limitations, and Project Extensions 388 CHAPTER 16 — PROJECT 2: DOCUMENT ANALYSIS AND DATA EXTRACTION AGENT 394 16.1 Problem Definition and Use Cases 395 16.2 Document Input and Processing Pipeline 396 16.3 Extraction Schema and Structured Outputs 398 16.4 Validation and Confidence Checks 401 16.5 Handling Missing, Conflicting, and Unclear Data 403 16.6 Human Review and Exception Queues 404 16.7 Evaluation Dataset and Accuracy Measurement 406 16.8 Deployment Considerations and Enhancements 408 CHAPTER 17 — PROJECT 3: MULTI-AGENT SOFTWARE ENGINEERING WORKFLOW 415 17.1 Project Scope and Engineering Requirements 416 17.2 Architect, Developer, and Reviewer Agent Roles 417 17.3 Task Routing and Agent Handoffs 419 17.4 Repository and Development Tools 422 17.5 Code Generation and Review Workflow 424 17.6 Test Execution and Defect Reporting 426 17.7 Human Approval and Change Control 428 17.8 Integration Testing and Project Evaluation 430 CHAPTER 18 — PROJECT 4: ENTERPRISE WORKFLOW AND APPROVAL AGENT 437 18.1 Enterprise Workflow Problem Definition 439 18.2 Workflow Modelling and Role-Based Access 441 18.3 Agent and Tool Architecture 443 18.4 Form Processing and Business-Rule Validation 446 18.5 Approval Gates and Human Escalation 449 18.6 Audit Trails, Notifications, and Exception Handling 452 18.7 Security, Privacy, and Governance 454 18.8 Deployment, Monitoring, and Acceptance Testing 456 CHAPTER 19 — PROJECT 5: CUSTOMER SUPPORT AND SERVICE DESK AGENT 464 19.1 Project Requirements and Service Boundaries 466 19.2 Knowledge Base and Retrieval Design 468 19.3 Intent Classification and Request Routing 472 19.4 Specialist Agent and Tool Integration 474 19.5 Response Drafting and Policy-Constrained Answers 477 19.6 Escalation to Human Support Staff 479 19.7 Evaluation: Resolution Quality and User Experience 481 19.8 Monitoring, Maintenance, and Future Extensions

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

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