Skip to main content
wrapAnthropicSdk instruments an existing Anthropic 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

wrapAnthropicSdk 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 Anthropic.
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:
A stream that is abandoned before it completes produces no event.

Tool calls

The Anthropic Messages API does not execute tools. messages.create and messages.stream capture the LLM turn, including the tool_use blocks the model requests, but running those tools is your code’s job — so tool executions are always instrumented manually. 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.