The Lightweight Framework for Collaborative AI Agents
This framework is a lightweight, powerful Python SDK (Software Development Kit) from the developers of GPT models, designed specifically for creating multi-agent workflows. Think of it as a clean, minimal foundation for making multiple AI agents collaborate on complex tasks.
As a software engineer, you're constantly looking for ways to automate tasks, build sophisticated logic, and integrate AI capabilities into your applications. This framework is a huge help for those goals
Simplifying Complex Workflows (Multi-Agent Systems)
The Problem
Many real-world problems (like generating code, analyzing documents, or managing customer service) require multiple distinct steps and decision points. Trying to cram all that logic into a single large language model (LLM) prompt is often fragile and inefficient.
The Solution
This framework lets you create specialized agents (e.g., a "Researcher Agent," a "Coder Agent," and a "Reviewer Agent"). You define their roles and tools, and the framework manages the handoff of tasks between them. This makes the overall system modular, easier to debug, and more robust.
Built-in Safety and Control (Guardrails)
The framework includes a concept of Guardrails. This is crucial for production systems. You can define rules, boundaries, and safety checks to ensure your agents operate within acceptable limits, preventing unwanted or unintended actions.
Tool Integration
Agents are most useful when they can interact with the real world. The framework facilitates integrating custom tools (like external APIs, database lookups, or file system access) so your agents can perform actions beyond just generating text.
Rapid Prototyping (PoC)
Because it's designed to be lightweight and minimal, it's perfect for quickly building a Proof of Concept (PoC). You can get a basic, collaborative AI system running in just a few lines of code, allowing you to test complex multi-step AI logic without getting bogged down in boilerplate code.
Getting this framework up and running is straightforward, assuming you already have Python and pip installed.
You install the framework using pip
pip install openai-agents-python
Since this framework relies on models, you'll need to set your API key as an environment variable. This is a common and secure practice for using external APIs
export OPENAI_API_KEY="YOUR_API_KEY_HERE"
Let's look at a simple example where we create two agents
a Planner Agent and a Coder Agent. The Planner's job is to define the task, and the Coder's job is to execute it.
Goal
Get an AI system to write a simple Python function that says "Hello World" and review the output.
from openai_agents import Agent, Handoff, Guardrail
import os
# 1. Initialize the Handoff Manager
# This manages the state and transition between agents
handoff_manager = Handoff()
# 2. Define the first Agent: Planner
# Role: Define the initial task
planner_agent = Agent(
name="Planner",
system_prompt="You are a clear and concise planner. Your only job is to describe a simple, single-function Python script that prints 'Hello, world!' and ask the Coder to implement it. Do not write the code yourself.",
model="gpt-4o", # Use a capable model
handoff_manager=handoff_manager
)
# 3. Define the second Agent: Coder
# Role: Implement the Python script based on the planner's request
coder_agent = Agent(
name="Coder",
system_prompt="You are a Python expert. Your job is to write a single Python function based on the Planner's request. Only output the code block.",
model="gpt-4o",
handoff_manager=handoff_manager
)
# 4. Define the Workflow Logic (The Handoff)
def collaboration_workflow():
# Start the task with the Planner Agent
initial_prompt = "I need a simple Python function called 'greet' that prints 'Hello, world!'."
print(f"--- Starting Task with {planner_agent.name} ---")
# Planner processes the initial request
handoff_manager.current_agent = planner_agent
handoff_manager.run_with_prompt(initial_prompt)
# Get the Planner's output (the task description)
task_description = handoff_manager.latest_response.content
print(f"\nPlanner Output (Task Description):\n{task_description}\n")
# Pass the task to the Coder Agent (The Handoff!)
print(f"--- Handoff to {coder_agent.name} ---")
# Coder processes the description from the Planner
handoff_manager.current_agent = coder_agent
handoff_manager.run_with_prompt(task_description)
# Get the Coder's final output (the code)
final_code = handoff_manager.latest_response.content
print(f"\nCoder Output (Final Code):\n{final_code}\n")
print("--- Workflow Complete ---")
# Run the system!
collaboration_workflow()
Agent
This class defines a persona. We gave the Planner and Coder distinct system_prompts (roles) to ensure they specialize in their tasks.
Handoff
This is the core mechanism. It manages the state and ensures the output of one agent (planner_agent.latest_response.content) becomes the input for the next agent (coder_agent.run_with_prompt(task_description)).
model
We specify the LLM (e.g., gpt-4o) for each agent. You could potentially use different models for different roles if needed (e.g., a fast gpt-3.5-turbo for quick planning and a strong gpt-4o for critical coding).