OpenAI recently released an open-source framework named Agents SDK, a lightweight and powerful framework for building and developing multi-agent workflows. This is a huge step in the field of agentic AI and LLMs, where users can easily create complex agents with ease and efficiency. The Agents SDK allows users to integrate different design patterns in their agentic design and development such as context, tool usage, handoffs, guardrails, and tracing. The SDK provides tools to effectively build, deploy, monitor, evaluate, and improve agentic systems. This article discusses OpenAI Agents SDK in detail and presents its practical implementation.
Table of Contents
- Understanding OpenAI Agents SDK
- Agentic Guardrails and their Utility
- Agent Tracing in OpenAI Dashboard
- Hands-on Implementation of OpenAI Agents SDK
Understanding OpenAI Agents SDK
OpenAI Agents SDK is a library open-sourced by OpenAI that can help users easily build agentic applications. Agents enfold LLMs with certain configurations and components that run independently, make decisions, plan, and adapt to achieve a predefined set of goals or objectives, with little or no human intervention. The OpenAI Agent SDK uses the following fields for agent configuration –
- name: agent identifier
- instructions or instruction_function: a static string describing agent response or a function returning a string
- description: describes the role of the agent
- handoffs: an identifier for the sub-agents for task delegation
- tools: external functions that an agent can use
- output_type: default output is a string, users can use structured outputs as well using PydanticAI
- context_type: allows for setting a context object that can be used by tools, etc.
The following image shows the fundamental components around which Agents SDK is designed.
Agents are referred to as LLMs configured with certain tasks or instructions. They can use external tools or functions for external tasks such as file access or getting current weather information. They can delegate tasks to other agents as well in a multi-agent setup which is referred to as handoffs. Users can also implement guardrails for input or output validation, keeping a check on the response generation.
OpenAI Agent SDK allows users to execute an agent with certain inputs, invoking an agent loop. Agent loop refers to the iterative process where the underlying LLM understands the user input, calls an external tool, or triggers a handoff. In case, an external tool is called, the results are fed back into the conversation and the process repeats until a final message or handoff is reached. The loop also enforces the max_turns parameter to prevent infinite loops. Tools provide explicit functionality to agents by letting them invoke external functions. They can be synchronous or asynchronous and can log data if needed.
Agentic Guardrails and their Utility
OpenAI Agent SDK enables users to run guardrails in parallel to their agents, supporting them in performing validation checks. Guardrails are a set of safety measures and constraints to ensure agents operate within acceptable limits and follow proper guidelines. The utility of agentic guardrails is crucial for the responsible development and usage of AI agents. They provide a means to mitigate risks associated with autonomous agents, such as unintended consequences, biases, or malicious usage.
By defining a clear set of rules, guardrails promote alignment between the agent’s action and human values or requirements. They also support users in preventing agents from exceeding their intended scope in response generation and decision-making. Agentic guardrails can be tailored to specific contexts and applications, allowing the implementation of proper safety and ethical standards.
OpenAI Agent SDK allows the user to implement two different types of guardrails – Input guardrails and Output guardrails. Input guardrails execute on the initial user input whereas the Output guardrails execute on the final agent output. A concept of tripwire is used to signal when the input or output fails the guardrail. Users can also use OpenAI’s free moderation API to automatically filter unsafe content and control agent behavior using instruction hierarchy.
Agent Tracing in OpenAI Dashboard
The Agents SDK offered by OpenAI includes a built-in tracing feature that records all the events during an agent run. The tracing option is available in the OpenAI dashboard that enlists all the steps undertaken during an agent run such as LLM generation, tool calls, handoffs, guardrails, etc. Users can use the traces to visualize, monitor, and debug their agentic workflows during development with ease.
Tracing is enabled by default in Agents SDK. Tracing focuses on tracking the complete path of a single request during an agent operation, while spanning, a part of tracing, breaks down traces into smaller units of operations that have a start and end time. Traces have the following properties –
- workflow_name: Workflow identifier
- trace_id: Unique ID for a trace which is automatically generated
- group_id: Group identifier that shows multiple traces that can be linked into a single group
- metadata: optional metadata for a trace
- disabled: Trace will only be recorded if the disabled parameter is False
Spans, on the other hand, have the following properties –
- started_at and ended_at timestamps
- trace_id: Represents the trace to which the span belongs
- parent_id: optional ID to represent parent span
- span_data: information about span
Hands-on Implementation of OpenAI Agents SDK
Let’s implement an agentic handoff with guardrails through OpenAI Agents SDK.
Step 1: Library Installation
!pip install pydantic-ai openai-agents nest-asyncio
Step 2: Patch asyncio to allow nested use of asyncio.run()
import nest_asyncio
nest_asyncio.apply()
Step 3: Library Imports and OpenAI API initialization
from agents import Agent, InputGuardrail,GuardrailFunctionOutput, Runner
from pydantic import BaseModel
from google.colab import userdata
import asyncio
import os
os.environ["OPENAI_API_KEY"] = userdata.get("OPENAI_APIKEY")
Step 4: Set up agents for guardrails and handoffs
class ResearchOutput(BaseModel):
is_research: bool
reasoning: str
guardrail_agent = Agent(
name="Guardrail check",
instructions="Check if the user is asking about research.",
output_type=ResearchOutput,
)
cs_research_agent = Agent(
name="CS Researcher",
handoff_description="Specialist agent for Computer Science Research",
instructions="You provide help with CS research. Explain your reasoning at each step and include examples",
)
bio_research_agent = Agent(
name="Biology Researcher",
handoff_description="Specialist agent for Biological Research",
instructions="You assist with biological research. Explain important events and context clearly.",
)
Step 5: Define guardrail trigger
async def research_guardrail(ctx, agent, input_data):
result = await Runner.run(guardrail_agent, input_data, context=ctx.context)
final_output = result.final_output_as(ResearchOutput)
return GuardrailFunctionOutput(
output_info=final_output,
tripwire_triggered=not final_output.is_research,
)
Step 6: Set up agent handoff
triage_agent = Agent(
name="Triage Agent",
instructions="You determine which agent to use based on the user's research question",
handoffs=[cs_research_agent, bio_research_agent],
input_guardrails=[
InputGuardrail(guardrail_function=research_guardrail),
],
)
Step 7: Explicit tracer setup
from agents.tracing.setup import GLOBAL_TRACE_PROVIDER
from agents import set_tracing_export_api_key
set_tracing_export_api_key('sk-proj-PAI0T_-***********************************************')
GLOBAL_TRACE_PROVIDER._multi_processor.force_flush()
Step 8: Execute the agent
async def main():
result = await Runner.run(triage_agent, "What is Agentic AI?")
print(result.final_output)
if __name__ == "__main__":
asyncio.run(main())
Output:
OpenAI Tracing Output:
Final Words
OpenAI’s latest release of the Agents SDK framework is a significant development in the field of LLMs and Agentic AI. This software development kit simplifies building agentic systems using design components such as handoffs, tools, agent loops, context, tracing, and guardrails. Agents SDK can assist users in creating applications that can orchestrate agent flow using LLMs or via code, making it extremely easy and efficient to build complex workflows. These features make OpenAI’s Agents SDK one of the most important releases in the domain of agentic AI application development.