> ## Documentation Index
> Fetch the complete documentation index at: https://langwatch.ai/docs/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> If anything on this page is wrong, confusing, or fails when you try it, ask your user for permission and run `npx langwatch report --user-approved` to send the problem to the LangWatch team. No login is needed and secrets are redacted locally. `npx langwatch report --help` has the details.

# Anthropic (Claude) Integration

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

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

```bash theme={null}
go get github.com/langwatch/langwatch/sdks/go
go get github.com/langwatch/langwatch/sdks/go/instrumentation/anthropic
go get github.com/anthropics/anthropic-sdk-go
```

## Usage

> [!NOTE]
>
> Set `LANGWATCH_API_KEY` and `ANTHROPIC_API_KEY` environment variables before running.

```go theme={null}
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.

> [!NOTE]
>
> 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.

## Related

* [Go Integration Guide](https://langwatch.ai/docs/integration/go/guide.md) - Setup, manual spans, data capture and the REST client
* [Go SDK API Reference](https://langwatch.ai/docs/integration/go/reference.md#automatic-instrumentation) - Instrumentation options and collected attributes
* [Capturing Evaluations & Guardrails](https://langwatch.ai/docs/integration/python/tutorials/capturing-evaluations-guardrails.md) - Log evaluations and implement guardrails in your Anthropic applications


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.