OpenAI Agents SDK is a lightweight yet powerful framework for building multi-agent workflows. The SDK provides a comprehensive set of tools for creating, managing, and monitoring agent-based applications. AgentOps seamlessly integrates to provide observability into these workflows.

Core Concepts

  • Agents: LLMs configured with instructions, tools, guardrails, and handoffs
  • Handoffs: Allow agents to transfer control to other agents for specific tasks
  • Guardrails: Configurable safety checks for input and output validation
  • Tracing: Built-in tracking of agent runs, allowing you to view, debug and optimize your workflows

Installation

Install AgentOps, the OpenAI Agents SDK, and python-dotenv for managing API keys:

pip install agentops openai-agents python-dotenv

Setting Up API Keys

Before using the OpenAI Agents SDK with AgentOps, you need to set up your API keys:

You can set these as environment variables or in a .env file.

export OPENAI_API_KEY="your_openai_api_key_here"
export AGENTOPS_API_KEY="your_agentops_api_key_here"

Then load them in your Python code:

from dotenv import load_dotenv
import os

load_dotenv()

AGENTOPS_API_KEY = os.getenv("AGENTOPS_API_KEY")
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")

Usage

AgentOps will automatically instrument the OpenAI Agents SDK after being initialized. You can then create agents, run them, and track their interactions.

import agentops
from agents import Agent, Runner

# Initialize AgentOps
agentops.init()

# Create an agent with instructions
agent = Agent(name="Assistant", instructions="You are a helpful assistant")

result = Runner.run_sync(agent, "Write a haiku about recursion in programming.")
print(result.final_output)

Examples

from agents import Agent, Runner
import asyncio
import agentops
import os

agentops.init()

spanish_agent = Agent(
    name="Spanish agent",
    instructions="You only speak Spanish.",
)

english_agent = Agent(
    name="English agent",
    instructions="You only speak English",
)

triage_agent = Agent(
    name="Triage agent",
    instructions="Handoff to the appropriate agent based on the language of the request.",
    handoffs=[spanish_agent, english_agent],
)


async def main():
    result = await Runner.run(triage_agent, input="Hola, ¿cómo estás?")
    print(result.final_output)
    # Expected Output: ¡Hola! Estoy bien, gracias por preguntar. ¿Y tú, cómo estás?


if __name__ == "__main__":
    asyncio.run(main())

More Examples