Skip to contentSkip to page list

Docs

npx langwatch

Trace any coding agent · Setup guide →

Anthropic (Claude) Integration

Instrument Anthropic Claude API calls in Go using LangWatch to track performance, detect errors, and improve AI agent testing.

On this page

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

example.shBash
go get github.com/langwatch/langwatch/sdks/gogo get github.com/langwatch/langwatch/sdks/go/instrumentation/anthropicgo get github.com/anthropics/anthropic-sdk-go

Usage

Set LANGWATCH_API_KEY and ANTHROPIC_API_KEY environment variables before running.
example.goGo
package main​import (	"context"	"log"	"os"​	"github.com/anthropics/anthropic-sdk-go"	"github.com/anthropics/anthropic-sdk-go/option"	"go.opentelemetry.io/otel"	sdktrace "go.opentelemetry.io/otel/sdk/trace"​	langwatch "github.com/langwatch/langwatch/sdks/go"	otelanthropic "github.com/langwatch/langwatch/sdks/go/instrumentation/anthropic")​func main() {	ctx := context.Background()​	// Set up the LangWatch exporter (reads LANGWATCH_API_KEY from env).	exporter, err := langwatch.NewExporter(ctx)	if err != nil {		log.Fatalf("failed to create exporter: %v", err)	}	tp := sdktrace.NewTracerProvider(sdktrace.WithBatcher(exporter))	otel.SetTracerProvider(tp)	defer tp.Shutdown(ctx) // Critical: ensures traces are flushed.​	// Create an instrumented Anthropic client. By default the middleware	// captures both request (input) and response (output) content.	client := anthropic.NewClient(		option.WithAPIKey(os.Getenv("ANTHROPIC_API_KEY")),		option.WithMiddleware(otelanthropic.Middleware()),	)​	message, err := client.Messages.New(ctx, anthropic.MessageNewParams{		Model:     anthropic.ModelClaudeHaiku4_5,		MaxTokens: 1024,		System:    []anthropic.TextBlockParam{{Text: "You are a helpful assistant."}},		Messages: []anthropic.MessageParam{			anthropic.NewUserMessage(anthropic.NewTextBlock("Hello, Claude!")),		},	})	if err != nil {		log.Fatalf("messages call failed: %v", err)	}​	log.Printf("Response: %v", message)}

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(...).

Warning

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.

Documentation