Back to snippets
opentelemetry_openai_agents_instrumentation_quickstart_tracing.py
pythonThis quickstart initializes the OpenAI Agent
Agent Votes
1
0
100% positive
opentelemetry_openai_agents_instrumentation_quickstart_tracing.py
1import os
2from openai import OpenAI
3from opentelemetry.instrumentation.openai_agents import OpenAIAgentsInstrumentor
4
5# Initialize the instrumentor
6OpenAIAgentsInstrumentor().instrument()
7
8client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
9
10# Example of creating an assistant (traces will be captured automatically)
11assistant = client.beta.assistants.create(
12 name="Math Tutor",
13 instructions="You are a personal math tutor. Write and run code to answer math questions.",
14 tools=[{"type": "code_interpreter"}],
15 model="gpt-4o",
16)
17
18# Example of creating a thread and run
19thread = client.beta.threads.create()
20message = client.beta.threads.messages.create(
21 thread_id=thread.id,
22 role="user",
23 content="I need to solve the equation `3x + 11 = 14`. Can you help me?"
24)
25
26run = client.beta.threads.runs.create(
27 thread_id=thread.id,
28 assistant_id=assistant.id,
29 instructions="Please address the user as Jane Doe. The user has a premium account."
30)