Skip to main content
LangWatch provides native instrumentation for the official Anthropic Go SDK (github.com/anthropics/anthropic-sdk-go). It wires a middleware that records request and response attributes for the Messages API (/v1/messages), both buffered and streamed, including the full token-usage breakdown (input, output, cache-read and cache-creation) and thinking content.

Installation

Usage

Set LANGWATCH_API_KEY and ANTHROPIC_API_KEY environment variables before running.
Streaming works identically: drive client.Messages.NewStreaming(...) and drain it; the span is recorded once the stream concludes.
The middleware captures input and output content by default (langwatch.DataCaptureAll). Pass otelanthropic.WithDataCapture(langwatch.DataCaptureNone) to opt out. If your client points at an Anthropic-compatible gateway (e.g. AWS Bedrock or Google Vertex), set the provider with otelanthropic.WithGenAIProvider(...).
The defer tp.Shutdown(ctx) call is essential. Without it, traces buffered in memory will be lost when your application exits.

Check it worked

Run the program with go run .. The deferred tp.Shutdown(ctx) flushes the spans when main returns. Then open your project in LangWatch: a new trace appears in the traces list within a few seconds, with the Anthropic call as an LLM span carrying its input, output and token counts. If no trace appears, check that LANGWATCH_API_KEY is set in the environment the program runs in.
Last modified on October 29, 2026