This document describes common architectural patterns for building applications with the OpenAI Agents Python SDK. These patterns are proven approaches for structuring agent-based systems, drawn from real-world implementations and best practices. Understanding these patterns will help you design scalable, maintainable, and effective agent architectures.
Pattern Categories
Agent Organization Patterns - How to organize agents
Workflow Patterns - How agents work together
Data Flow Patterns - How data flows through the system
Integration Patterns - How to integrate with external systems
Deployment Patterns - How to deploy agent applications
agent = Agent(
name="assistant",
instructions="You are a helpful assistant for customer support",
tools=[search_knowledge_base, create_ticket],
)
result = await Runner.run(agent, user_input)
Pros:
Simple to implement
Easy to understand
Low overhead
Cons:
Limited scalability
Hard to maintain for complex domains
Single point of failure
2. Specialist Pattern
Description: Multiple specialized agents, each handling a specific domain.
Description: Workflow loops until a condition is met.
When to use:
Iterative processes
Refinement tasks
Unknown number of iterations
Example:
Python
refiner = Agent(name="refiner", instructions="Refine the output")
result = await Runner.run(agent, input)
for i inrange(5): # Max 5 iterationsif is_satisfactory(result.final_output):
break
result = await Runner.run(refiner, result.final_output)
Pros:
Handles iterative processes
Can refine results
Flexible iteration count
Cons:
Can be slow
Risk of infinite loops
Harder to predict runtime
4. Fan-Out/Fan-In Workflow
Description: Fan-out to multiple agents, then fan-in to combine results.
When to use:
Parallel subtasks
Multiple perspectives
Aggregation needed
Example:
Python
# Fan-out
agents = [
Agent(name="agent1", instructions="Perspective 1"),
Agent(name="agent2", instructions="Perspective 2"),
Agent(name="agent3", instructions="Perspective 3"),
]
results = await asyncio.gather(*[
Runner.run(agent, input) for agent in agents
])
# Fan-in
aggregator = Agent(name="aggregator", instructions="Combine perspectives")
final_result = await Runner.run(
aggregator,
"\n".join([r.final_output for r in results])
)
Pros:
Parallel execution
Multiple perspectives
Flexible aggregation
Cons:
Higher cost
Need good aggregation logic
Potential for inconsistent results
5. Human-in-the-Loop Workflow
Description: Workflow pauses for human intervention at key points.
When to use:
Critical decisions
Approval required
Quality gates
Example:
Python
@function_tool(needs_approval=True)defsensitive_operation() -> str:
return"Operation performed"
agent = Agent(
name="agent",
instructions="Use sensitive tool when needed",
tools=[sensitive_operation],
)
result = await Runner.run(agent, input)
if result.interruptions:
# Human reviewsfor interruption in result.interruptions:
if should_approve(interruption):
state = result.to_state()
state.context.approve_tool(interruption)
result = await Runner.run(agent, state)
Pros:
Human oversight
Quality control
Risk mitigation
Cons:
Slower execution
Requires human availability
Potential for bottlenecks
Data Flow Patterns
1. Request-Response Pattern
Description: Single request, single response.
When to use:
Simple queries
One-off tasks
Stateless operations
Example:
Python
result = await Runner.run(agent, "What is the capital of France?")
print(result.final_output)
Pros:
Simple
Stateless
Easy to cache
Cons:
No conversation history
Limited context
Not suitable for complex tasks
2. Conversational Pattern
Description: Multi-turn conversation with history.
asyncfor event in Runner.run_streamed(agent, input):
ifisinstance(event, RunItemStreamEvent):
ifisinstance(event.item, MessageOutputItem):
for content in event.item.raw_item.content:
if content.type == "text":
print(content.text, end="", flush=True)
# Agent Organization: Hierarchical
triage -> supervisors -> specialists
# Workflow: Human-in-the-Loop for approvals# Data Flow: Conversational# Integration: API Gateway# Deployment: Container
Summary
Architectural patterns provide proven approaches for building agent systems. Key takeaways:
Single Agent - Simple, single-purpose
Specialist - Domain-specific agents
Hierarchical - Supervised specialists
Collaborative - Agents working together
Parallel - Multiple agents simultaneously
Linear Workflow - Sequential steps
Branching - Conditional paths
Loop - Iterative refinement
Fan-Out/Fan-In - Parallel then aggregate
Human-in-the-Loop - Human approval points
Request-Response - Single request
Conversational - Multi-turn dialogue
Streaming - Real-time updates
Batch - Bulk processing
Event-Driven - Reactive processing
API Gateway - Web API layer
Microservices - Independent services
Sidecar - Add-on to main app
Plugin - Extensible modules
Proxy - Legacy integration
Serverless - Cloud functions
Container - Docker deployment
Kubernetes - Orchestration
Queue-Based - Async processing
Edge - Local deployment
Choose patterns based on your requirements, scale, and constraints. Combine patterns as needed for complex systems. Avoid anti-patterns that lead to unmaintainable code.