AI / Core OpenAI Codex Application Fundamentals Interview Questions
How do you implement multi-agent systems using the OpenAI Agents SDK?
Multi-agent systems decompose complex tasks across multiple specialised agents that collaborate via handoffs. Each agent focuses on what it does best, improving overall quality and maintainability compared to a monolithic agent trying to do everything.
from agents import Agent, Runner, handoff import asyncio # Define specialised agents: code_agent = Agent( name="CodeWriter", instructions="Write clean, well-documented Python code. Focus on correctness.", model="gpt-5.5", ) review_agent = Agent( name="CodeReviewer", instructions="Review code for bugs, security issues, and performance problems.", model="gpt-5.5", ) doc_agent = Agent( name="DocWriter", instructions="Write clear docstrings and README documentation.", model="gpt-5.4-mini", # cheaper model for docs ) # Orchestrator agent with handoffs: orchestrator = Agent( name="Orchestrator", instructions="""Manage the coding workflow: 1. Use CodeWriter to implement features 2. Use CodeReviewer to review and fix issues 3. Use DocWriter to document the final code """, handoffs=[ handoff(code_agent), handoff(review_agent), handoff(doc_agent), ], model="gpt-5.5", ) async def run_pipeline(task: str): result = await Runner.run( orchestrator, task, max_turns=20, # prevent infinite loops ) return result.final_output # Run: output = asyncio.run(run_pipeline( "Implement a thread-safe LRU cache class with comprehensive tests" )) print(output)
Handoff patterns: an agent can hand off to a more specialised sub-agent mid-task (unidirectional), or the orchestrator can send tasks to parallel workers and aggregate results. The SDK handles the state transfer between agents so each agent receives the full context it needs.
More Related questions...