Using Opik with OpenAI Agents

Opik integrates with OpenAI Agents to provide a simple way to log traces and analyse for all OpenAI LLM calls. This works for all OpenAI models, including if you are using the streaming API.

Creating an account on Comet.com

Comet provides a hosted version of the Opik platform, simply create an account and grab your API Key.

You can also run the Opik platform locally, see the installation guide for more information.

1%pip install --upgrade opik openai-agents
1import opik
2
3opik.configure(use_local=False)

Preparing our environment

First, we will set up our OpenAI API keys.

1import os
2import getpass
3
4if "OPENAI_API_KEY" not in os.environ:
5 os.environ["OPENAI_API_KEY"] = getpass.getpass("Enter your OpenAI API key: ")

Logging traces

In order to log traces to Opik, we need to wrap our OpenAI calls with the track_openai function:

1from agents import Agent, Runner
2from agents import set_trace_processors
3from opik.integrations.openai.agents import OpikTracingProcessor
4
5os.environ["OPIK_PROJECT_NAME"] = "openai-agents-demo"
6
7set_trace_processors(processors=[OpikTracingProcessor()])
1# Create and run your agent
2agent = Agent(
3 name="Creative Assistant",
4 instructions="You are a creative writing assistant that helps users with poetry and creative content.",
5 model="gpt-4o-mini"
6)
7
8# Use async Runner.run() instead of run_sync() in Jupyter notebooks
9result = await Runner.run(agent, "Write a haiku about recursion in programming.")
10print(result.final_output)

Using it with the track decorator

If you have multiple steps in your LLM pipeline, you can use the track decorator to log the traces for each step. If OpenAI is called within one of these steps, the LLM call with be associated with that corresponding step:

1from agents import Agent, Runner, function_tool
2from opik import track
3
4@function_tool
5def calculate_average(numbers: list[float]) -> float:
6 return sum(numbers) / len(numbers)
7
8@function_tool
9def get_recommendation(topic: str, user_level: str) -> str:
10 recommendations = {
11 "python": {
12 "beginner": "Start with Python.org's tutorial, then try Python Crash Course book. Practice with simple scripts and built-in functions.",
13 "intermediate": "Explore frameworks like Flask/Django, learn about decorators, context managers, and dive into Python's data structures.",
14 "advanced": "Study Python internals, contribute to open source, learn about metaclasses, and explore performance optimization."
15 },
16 "machine learning": {
17 "beginner": "Start with Andrew Ng's Coursera course, learn basic statistics, and try scikit-learn with simple datasets.",
18 "intermediate": "Dive into deep learning with TensorFlow/PyTorch, study different algorithms, and work on real projects.",
19 "advanced": "Research latest papers, implement algorithms from scratch, and contribute to ML frameworks."
20 }
21 }
22
23 topic_lower = topic.lower()
24 level_lower = user_level.lower()
25
26 if topic_lower in recommendations and level_lower in recommendations[topic_lower]:
27 return recommendations[topic_lower][level_lower]
28 else:
29 return f"For {topic} at {user_level} level: Focus on fundamentals, practice regularly, and build projects to apply your knowledge."
30
31def create_advanced_agent():
32 """Create an advanced agent with tools and comprehensive instructions."""
33 instructions = """
34 You are an expert programming tutor and learning advisor. You have access to tools that help you:
35 1. Calculate averages for performance metrics, grades, or other numerical data
36 2. Provide personalized learning recommendations based on topics and user experience levels
37
38 Your role:
39 - Help users learn programming concepts effectively
40 - Provide clear, beginner-friendly explanations when needed
41 - Use your tools when appropriate to give concrete help
42 - Offer structured learning paths and resources
43 - Be encouraging and supportive
44
45 When users ask about:
46 - Programming languages: Use get_recommendation to provide tailored advice
47 - Performance or scores: Use calculate_average if numbers are involved
48 - Learning paths: Combine your knowledge with tool-based recommendations
49
50 Always explain your reasoning and make your responses educational.
51 """
52
53 return Agent(
54 name="AdvancedProgrammingTutor",
55 instructions=instructions,
56 model="gpt-4o-mini",
57 tools=[calculate_average, get_recommendation]
58 )
59
60advanced_agent = create_advanced_agent()
61
62advanced_queries = [
63 "I'm new to Python programming. Can you tell me about it?",
64 "I got these test scores: 85, 92, 78, 96, 88. What's my average and how am I doing?",
65 "I know some Python basics but want to learn machine learning. What should I do next?",
66 "Can you help me calculate the average of these response times: 1.2, 0.8, 1.5, 0.9, 1.1 seconds? And tell me if that's good performance?"
67]
68
69for i, query in enumerate(advanced_queries, 1):
70 print(f"\n📝 Query {i}: {query}")
71 result = await Runner.run(advanced_agent, query)
72 print(f"🤖 Response: {result.final_output}")
73 print("=" * 80)

The trace can now be viewed in the UI:

OpenAI Integration