Prerequisites
Install the SDK and make sure you have an OpenAI API key ready.tp_live_xxxxxxxxxxxxxxxx.
Wrap your first call
1
Import and initialize
Import
TracePilot alongside the OpenAI client and create instances of both.2
Start a trace
Call
tp.startTrace once at the beginning of each agent run. Pass a name that identifies this agent or workflow — it appears as the trace label in the dashboard.3
Wrap the OpenAI call
Replace a bare
openai.chat.completions.create call with tp.wrapOpenAI. Pass the call as a function and include the messages array as the second argument.wrapOpenAI returns the original OpenAI response object unchanged. Any code that reads result.choices[0].message.content or any other field on the completion response continues to work without modification.Full working example
Optional parameters
wrapOpenAI accepts two additional parameters that become useful once your agent has multiple steps.
parentSpanId
Pass the spanId returned by a previous wrapOpenAI or wrapToolCall call to link the current span as a child of that span. This builds a parent-child execution tree in the dashboard.
stepOrder
Pass a number to control the order in which spans are displayed within the same parent. Without stepOrder, the dashboard sorts spans by arrival time, which can be inconsistent under concurrency.
Using a custom ingest endpoint
Using a custom ingest endpoint
If you are self-hosting the TracePilot backend, pass your endpoint as the second argument to the constructor.All other methods work identically.
Error handling
Error handling
If the OpenAI call throws,
wrapOpenAI captures the error as a failed span in the dashboard and re-throws the original error. Your existing try/catch blocks remain effective.