OpenAI Agents

OpenAI released an agentic framework aptly named Agents. What sets this framework apart from others is that it provides a rich set of core building blocks:

  1. Models: Support for all OpenAI Models
  2. Tools: Similar function calling functionality than the one available when using the OpenAI models directly
  3. Knowledge and Memory: Seamless integration with OpenAI’s vector store and Embeddings Anthropic
  4. Guardrails: Run Guardrails checks in parallel to your agent execution which allows for secure execution without slowing down the total agent execution.

Opik’s integration with Agents is just one line of code and allows you to analyse and debug the agent execution flow in our Open-Source platform.

Getting started

First, ensure you have both opik and openai-agents packages installed:

$pip install opik openai-agents

In addition, you can configure Opik using the opik configure command which will prompt you for the correct local server address or if you are using the Cloud platform your API key:

$opik configure

Enabling logging to Opik

To enable logging to Opik, simply add the following two lines of code to your existing OpenAI Agents code:

1from agents import Agent, Runner
2from agents import set_trace_processors
3from opik.integrations.openai.agents import OpikTracingProcessor
4
5set_trace_processors(processors=[OpikTracingProcessor()])
6
7agent = Agent(name="Assistant", instructions="You are a helpful assistant")
8
9result = Runner.run_sync(agent, "Write a haiku about recursion in programming.")
10print(result.final_output)

The Opik integration will automatically track both the token usage and overall cost of each LLM call that is being made. You can also view this information aggregated for the entire agent execution.

Logging threads

When you are running multi-turn conversations with OpenAI Agents using OpenAI Agents trace API, Opik integration automatically use the trace group_id as the Thread ID so you can easily review conversation inside Opik. Here is an example below:

1async def main():
2 agent = Agent(name="Assistant", instructions="Reply very concisely.")
3
4 thread_id = str(uuid.uuid4())
5
6 with trace(workflow_name="Conversation", group_id=thread_id):
7 # First turn
8 result = await Runner.run(agent, "What city is the Golden Gate Bridge in?")
9 print(result.final_output)
10 # San Francisco
11
12 # Second turn
13 new_input = result.to_input_list() + [{"role": "user", "content": "What state is it in?"}]
14 result = await Runner.run(agent, new_input)
15 print(result.final_output)
16 # California

Further improvements

OpenAI Agents is still a relatively new framework and we are working on a couple of improvements:

  1. Improved rendering of the inputs and outputs for the LLM calls as part of our Pretty Mode functionality
  2. Improving the naming conventions for spans
  3. Adding the agent execution input and output at a trace level

If there are any additional improvements you would like us to make, feel free to open an issue on our GitHub repository.