Skip to main content
Quick setup? Instead of following these steps manually, copy a prompt into your coding agent and it will set this up for you automatically.
LangWatch Go Repo
Go Reference
Integrate LangWatch into your Go application to start observing your LLM interactions. This guide covers the setup and basic usage of the LangWatch Go SDK, which is built on top of OpenTelemetry to keep the tracing vendor-neutral. The SDK ships:
  • A core tracing module (github.com/langwatch/langwatch/sdks/go) with the exporter, the Tracer, the LangWatchSpan helpers and span filtering. It has no provider dependencies at all.
  • A set of provider instrumentations, each its own Go module, so importing one never pulls in the others’ SDKs. OpenAI, Anthropic, Google Gemini / Vertex, Amazon Bedrock, Azure OpenAI, Ollama, the community go-openai client and Firebase Genkit are all supported natively.
  • A typed REST API client (github.com/langwatch/langwatch/sdks/go/client) for reading and writing LangWatch resources: prompts, datasets, traces, annotations, evaluations and more.
Protip: wanna to get started even faster? Copy our llms.txt and ask an AI to do this integration

Prerequisites

Before you begin, ensure you have:
  • Go 1.25 or later installed on your system
  • A LangWatch account at app.langwatch.ai
  • An OpenAI API key (or other LLM provider key)
  • Basic familiarity with Go and OpenTelemetry concepts
If you’re new to OpenTelemetry, the LangWatch SDK handles the setup for you. You only need to understand the basic concepts of traces and spans.

Setup

Get started in just a few minutes by installing the SDK and instrumenting your application.
1

Get your LangWatch API Key

Sign up at app.langwatch.ai and find your API key in your project settings. Set it as an environment variable:
LANGWATCH_PROJECT_ID is required when using a service API key or a Personal Access Token (e.g. for CI/CD or multi-project setups). Project API keys obtained from the project settings page already have the project context built in.
You can verify your API key is set by running echo $LANGWATCH_API_KEY
2

Install SDK Packages

Add the core SDK plus the instrumentation for the provider you use. Each instrumentation is its own module, so you only pull in the SDK you actually need:
Verify installation by running go mod tidy and checking that all dependencies are resolved.
3

Configure the LangWatch Exporter

Set up the LangWatch exporter in your application initialization. NewExporter reads LANGWATCH_API_KEY from the environment, builds an OTLP exporter pointed at LangWatch, and you register it as your global tracer provider:
Use langwatch.NewDefaultExporter(ctx) instead if you want LangWatch’s default span filter applied automatically (it excludes raw HTTP request spans like GET /api). See Filtering Spans below.
Critical: Always call the shutdown function! The defer shutdown(ctx) call is essential. Without it, traces buffered in memory will be lost when your application exits. The shutdown function flushes all pending traces to LangWatch and releases resources. Never skip this step, especially in short-lived applications like CLI tools or serverless functions.
4

Instrument Your OpenAI Client

Add the LangWatch middleware to your OpenAI client. The middleware captures request and response content by default, so you do not need to enable either:
The middleware captures input and output content by default (langwatch.DataCaptureAll). To opt out, pass otelopenai.WithDataCapture(langwatch.DataCaptureNone). See Data Capture.
The middleware automatically captures all OpenAI API calls, including streaming responses, token usage (input / output / cached / reasoning), and model information.
5

Create Your First Trace

Start a root span to capture your LLM interaction. Any instrumented call made with the returned ctx is nested under it:
View your traces at app.langwatch.ai within seconds of making your first API call.
That’s it! 🎉 You’ve successfully integrated LangWatch into your Go application. Traces will now be sent to your LangWatch project, providing you with immediate visibility into your LLM interactions.

Complete Working Example

Here’s a minimal working example that combines all the setup steps. Note the openai-go/v3 import paths:

Core Concepts

The Go SDK is designed to feel familiar to anyone who has used OpenTelemetry. It provides a thin wrapper to simplify LangWatch-specific functionality.
  • Each message triggering your LLM pipeline as a whole is captured with a Trace.
  • A Trace contains multiple Spans, which are the steps inside your pipeline.
  • Traces can be grouped into a conversation by assigning a common thread_id.
  • Use User ID to track individual user interactions.
  • Apply Labels for custom categorization and filtering.
For detailed API documentation of all available methods and options, see the Go SDK API Reference.

Creating a Trace

A trace represents a single, end-to-end task, like handling a user request. You create a trace by starting a “root” span. All other spans created within its context will be nested under it. Before creating traces, ensure you have configured the SDK as shown in the Setup Guide.

Creating Nested Spans

To instrument specific parts of your pipeline (like a RAG query or a tool call), create nested spans within an active trace. Set a span type so LangWatch can visualise it appropriately.
The context ctx is crucial. It carries the active span information, ensuring that tracer.Start() correctly creates a nested span instead of a new trace.
tracer.WithActiveSpan(ctx, name, fn) runs fn with a span that auto-ends and records error status, so you don’t have to remember defer span.End() or wire up error handling by hand.

Automatic Instrumentation

Each provider instrumentation is a separate Go module so importing one never pulls in the others’ SDKs. Add only the one(s) you use. All of the following are supported natively: Every instrumentation captures the maximum available GenAI data: request + response model, every token type (input / output / total / cached / reasoning / cache-creation), cost where the provider returns it, finish reasons, request params, system instructions, tool definitions, and (capture-gated) input/output.

OpenAI-compatible providers

Many providers expose an OpenAI-compatible endpoint. Reuse the openai instrumentation, point the client at the provider’s base URL, and label the spans with the right provider name via WithGenAIProvider:
See the dedicated guides for Groq, Grok (xAI) and OpenRouter.
For detailed configuration options and the full attribute list, see the Automatic Instrumentation section in the API reference.

Recording Data on Spans

LangWatchSpan embeds the standard OpenTelemetry span and adds LangWatch helper methods. All setters return the span, so they can be chained:
SetInput / SetOutput infer a value type (text, json, chat_messages, list, …) from the Go value, or you can force one with a typed variant such as SetInputJSON or SetOutputChatMessages. Token usage is recorded with SetGenAIUsage; cost (only) is recorded with SetMetrics:
For the complete list of span helper methods (RAG contexts, multimodal/binary content, metadata, GenAI semconv setters, evaluations and events), see the Span section in the reference.

Data Capture

Data capture controls whether span input/output content leaves the process. It is enforced at export time, so one setting governs every instrumentation (the provider middlewares and your manual spans alike). Span structure, metrics, metadata, models and identity are always kept; only the content attributes are stripped. The default, unconfigured, captures everything.
The modes are langwatch.DataCaptureAll, langwatch.DataCaptureInput, langwatch.DataCaptureOutput and langwatch.DataCaptureNone. Each instrumentation also accepts WithDataCapture(...) to gate content at the source for a single client; the two compose.
For per-instrumentation data-capture options and how they compose with the exporter, see the Data Capture section in the reference.

The LangWatch API Client

Separate from tracing, github.com/langwatch/langwatch/sdks/go/client is a typed REST client for the LangWatch API: prompts, datasets, traces, annotations, events, evaluations, triggers, monitors, scenarios and projects. It lives in its own module so the generated code stays out of the core graph:
It uses the same credentials as the exporter (sk-lw-* keys or pat-lw-* PATs with a project id).
For the full service list, pagination, retries and typed error handling, see the API Client section in the reference.

Environment Variables

The SDK respects these environment variables for configuration:

Filtering Spans

The LangWatch SDK filters which spans are exported. Use filtering to reduce noise, exclude sensitive data, or focus on specific instrumentation.

Using Preset Filters

The SDK includes convenient preset filters for common use cases:

Custom Filtering

Create custom filters using Include() or Exclude() with matching criteria:

Matcher Types

The SDK provides several matcher types:
Filters use AND semantics when combined - a span must pass all filters to be exported. Within a Criteria, matchers use OR semantics - a span matches if it matches any of the matchers in the list.
For detailed filter API documentation and more examples, see the Filtering section in the API reference.

Features

  • 🔗 OpenTelemetry integration - Works with your existing OTel setup
  • 🚀 Multi-provider instrumentation - OpenAI, Anthropic, Gemini/Vertex, Bedrock, Azure, Ollama, Genkit and more
  • 📊 Rich LLM telemetry - Capture inputs, outputs, every token type, cost, and model information
  • 🔍 Specialized span types - LLM, Chain, Tool, Agent, RAG, and more
  • 🧵 Thread support - Group related LLM interactions together
  • 🎛️ Data capture controls - Decide exactly what content leaves your process
  • 🔄 Streaming support - Real-time reconstruction of streaming responses
  • 🧰 Typed REST client - Read and write prompts, datasets, traces, evaluations and more
Since LangWatch is built on OpenTelemetry, it also supports any library or framework that integrates with OpenTelemetry.

Troubleshooting

Common Issues

No traces appearing in LangWatch dashboard:
  • Verify your LANGWATCH_API_KEY is set correctly
  • Check that you’re calling the shutdown function to flush traces
  • Ensure your application is making LLM API calls through an instrumented client
Import errors:
  • Run go mod tidy to ensure all dependencies are properly resolved
  • Verify you’re using Go 1.25 or later
  • Make sure the OpenAI client import path is github.com/openai/openai-go/v3 (and /v3/option)
OpenTelemetry configuration errors:
  • Check that the LangWatch endpoint URL is correct
  • Verify your API key has the correct permissions

Getting Help

If you’re still having issues:

Next Steps

Last modified on September 8, 2026