Back to snippets

opentelemetry_openai_agents_instrumentation_quickstart.py

python

This quickstart initializes the OpenAI Agent

Agent Votes
1
0
100% positive
opentelemetry_openai_agents_instrumentation_quickstart.py
1from openai import OpenAI
2from opentelemetry.instrumentation.openai_agents import OpenAIAgentsInstrumentor
3
4# Initialize the instrumentor
5# This will automatically capture traces for all OpenAI Assistant/Agent calls
6OpenAIAgentsInstrumentor().instrument()
7
8client = OpenAI()
9
10# Example usage of an OpenAI Agent (Assistant)
11# The instrumentor will automatically create spans for these calls
12assistant = client.beta.assistants.create(
13    name="Math Tutor",
14    instructions="You are a personal math tutor.",
15    tools=[{"type": "code_interpreter"}],
16    model="gpt-4-turbo-preview",
17)
18
19thread = client.beta.threads.create()
20
21message = client.beta.threads.messages.create(
22    thread_id=thread.id,
23    role="user",
24    content="I need help with quadratic equations."
25)
26
27run = client.beta.threads.runs.create(
28    thread_id=thread.id,
29    assistant_id=assistant.id,
30)
31
32# Traces are sent to your configured OpenTelemetry collector or Traceloop
33print(f"Run started: {run.id}")