Skip to main content
wrapOpenAISdk instruments an existing OpenAI client. Your call sites stay the same; each request is captured as an LLM event with model, prompts, output, token usage, duration, and status.

Quick setup

wrapOpenAISdk mutates the client you pass in and returns the same instance, typed to accept the optional carbon field on requests. Wrap the client once at startup and share the wrapped instance.
Deploying to a serverless platform — Next.js, Vercel, AWS Lambda? Flush buffered events before the function exits so none are lost. See Serverless.

What gets captured

Attach metadata

Add a carbon field to any request to set a trace ID, context identifiers, or custom properties. The SDK strips the field before the request reaches OpenAI.
See Context for what each field powers in the dashboard.

Streaming

Streamed responses are captured after the stream finishes, so the stream must be fully consumed — iterate it to completion or await the final result:
For streamed chat completions, the wrapper sets stream_options.include_usage automatically so token usage is recorded.
A stream that is abandoned before it completes produces no event.

Tool calls

A tool execution is captured as a tool event on the same trace as the LLM call that requested it. Whether that happens automatically depends on which method runs the tool.

Automatically instrumented

chat.completions.runTools executes your tool functions in a loop and captures every step — each model turn becomes an LLM event and each tool execution becomes a tool event, grouped on one trace automatically. No wrapTool or trace ID needed.

Manual instrumentation

chat.completions.create, chat.completions.stream, and the responses.* methods don’t execute tools — the model returns the tool calls and your code runs them. Wrap each tool with wrapTool so its execution is captured. These methods don’t open a trace of their own, so create a trace ID with carbon.createTraceId() and pass it to both the model call and every tool call — that shared ID links the LLM event and its tool events into one trace:
See Tool calls for the full wrapTool API.