Skip to main content
OpenLIT uses OpenTelemetry instrumentation to help you monitor Go applications built with Anthropic models. This includes tracking performance, token usage (including prompt cache tokens), costs, and how users interact with the application. The Go SDK wraps your Anthropic client with an InstrumentedClient that automatically emits traces and metrics for every API call — with zero changes to your application logic. The integration supports:
  • Messages (standard and streaming)
  • Prompt caching token tracking (cache_creation_input_tokens, cache_read_input_tokens)
  • Tool use

Get started

1

Install the Go SDK

Open your terminal and run:
go get github.com/openlit/openlit/sdk/go
2

Initialize OpenLIT

Add this once at the start of your application (e.g. in main()):
import (
    "context"
    openlit "github.com/openlit/openlit/sdk/go"
)

if err := openlit.Init(openlit.Config{
    OtlpEndpoint:    "YOUR_OTEL_ENDPOINT",
    ApplicationName: "my-ai-app",
    Environment:     "production",
}); err != nil {
    log.Fatal(err)
}
defer openlit.Shutdown(context.Background())
Replace YOUR_OTEL_ENDPOINT with the URL of your OpenTelemetry backend, such as http://127.0.0.1:4318 for a local OpenLIT deployment.
3

Create an instrumented client

Replace your existing Anthropic client creation with the OpenLIT instrumented client:
import "github.com/openlit/openlit/sdk/go/instrumentation/anthropic"

client := anthropic.NewClient("your-anthropic-api-key")
Optional configuration:
// Custom API version or base URL
client := anthropic.NewClient("your-api-key",
    anthropic.WithAPIVersion("2023-06-01"),
    anthropic.WithBaseURL("https://api.anthropic.com/v1"),
)
4

Use the client

Use the instrumented client exactly as you would a normal Anthropic client:Message:
resp, err := client.CreateMessage(ctx, anthropic.MessageRequest{
    Model:     "claude-opus-4-5",
    MaxTokens: 256,
    System:    "You are a helpful assistant.",
    Messages: []anthropic.Message{
        {Role: "user", Content: "What is OpenTelemetry?"},
    },
})
if err != nil {
    return err
}

for _, block := range resp.Content {
    if block.Type == "text" {
        fmt.Println(block.Text)
    }
}
Streaming:
stream, err := client.CreateMessageStream(ctx, anthropic.MessageRequest{
    Model:     "claude-opus-4-5",
    MaxTokens: 1024,
    Messages: []anthropic.Message{
        {Role: "user", Content: "Tell me a story."},
    },
})
if err != nil {
    return err
}
defer stream.Close()

for {
    event, err := stream.Recv()
    if err == io.EOF {
        break
    }
    if err != nil {
        return err
    }
    if event.Type == "content_block_delta" && event.Delta != nil {
        fmt.Print(event.Delta.Text)
    }
}
Tool use:
resp, err := client.CreateMessage(ctx, anthropic.MessageRequest{
    Model:     "claude-opus-4-5",
    MaxTokens: 512,
    Messages: []anthropic.Message{
        {Role: "user", Content: "What is the weather in San Francisco?"},
    },
    Tools: []anthropic.Tool{
        {
            Name:        "get_weather",
            Description: "Get the current weather in a location",
            InputSchema: map[string]interface{}{
                "type": "object",
                "properties": map[string]interface{}{
                    "location": map[string]interface{}{
                        "type":        "string",
                        "description": "The city and state",
                    },
                },
                "required": []string{"location"},
            },
        },
    },
})

What gets collected

Every call to the instrumented client automatically records:
DataAttribute
Operation namegen_ai.operation.name
Model requestedgen_ai.request.model
Model usedgen_ai.response.model
Response IDgen_ai.response.id
Input tokensgen_ai.usage.input_tokens
Output tokensgen_ai.usage.output_tokens
Cache creation tokensgen_ai.usage.prompt_tokens_details.cache_write
Cache read tokensgen_ai.usage.prompt_tokens_details.cache_read
Estimated costgen_ai.usage.cost
Finish reasongen_ai.response.finish_reasons
Tool callsgen_ai.tool.name, gen_ai.tool.call.id
Time to first tokengen_ai.server.time_to_first_token (streaming)
Time per output tokengen_ai.server.time_per_output_token (streaming)
Metrics emitted:
  • gen_ai.client.token.usage — token usage histogram (input/output)
  • gen_ai.client.operation.duration — total operation duration
  • gen_ai.server.time_to_first_token — TTFT for streaming
  • gen_ai.client.operation.time_to_first_chunk — client-side TTFT
  • gen_ai.client.operation.time_per_output_chunk — per-chunk latency
  • gen_ai.server.request.duration — estimated server processing time