Skip to main content
This reference provides detailed documentation for all public APIs in the LangWatch Go SDK and its associated instrumentation packages.

Installation

For a quick start guide with step-by-step instructions, see the Go Integration Guide. For practical examples of creating traces and spans, see the Core Concepts section in the guide.

Core SDK (langwatch)

Setup

Create a LangWatch exporter and configure it as your tracer provider:
The exporter reads LANGWATCH_API_KEY from your environment automatically.
Always call shutdown! Traces are buffered in memory before being sent. Without defer shutdown(ctx), your traces will be lost when the application exits. This is critical for CLI tools, serverless functions, and any short-lived process.
For custom configuration, use NewExporter with options:

Tracer

Tracer() retrieves a LangWatchTracer instance, which is a thin wrapper around an OpenTelemetry Tracer.
Example:

LangWatchTracer

The LangWatchTracer interface provides a Start method that mirrors OpenTelemetry’s but returns a LangWatchSpan.
Example:

LangWatchSpan

The LangWatchSpan interface embeds the standard trace.Span and adds several helper methods for LangWatch-specific data.
function
Sets the span type for categorization in LangWatch. This enables specialized UI treatment and analytics.Example:
Using span types is optional but highly recommended as it enables LangWatch to provide more tailored insights and visualizations.
function
Assigns a thread ID to group this trace with a conversation. Useful for multi-turn conversations.Example:
All spans within the same trace will share the same thread ID, allowing you to group related interactions together.
function
Assigns a user ID to the trace for user-centric analytics and filtering.Example:
function
Records a simple string as the span’s input. Ideal for user queries or simple text inputs.Example:
function
Records a structured object (e.g., struct, map) as the span’s input, serialized to JSON. Use for complex request objects.Example:
function
Records a simple string as the span’s output. Ideal for AI responses or simple text outputs.Example:
function
Records a structured object as the span’s output, serialized to JSON. Use for complex response objects.Example:
function
Sets the model identifier used for a request (e.g., an LLM call). This is the model you requested to use.Example:
function
Sets the model identifier reported in a response. This is the actual model that processed your request.Example:
The response model may differ from the request model, especially with OpenAI’s model updates.
function
Attaches a slice of retrieved context chunks for RAG analysis. This enables LangWatch to analyze the relevance and quality of retrieved documents.Example:

OpenAI Instrumentation

The github.com/langwatch/langwatch/sdk-go/instrumentation/openai package provides middleware for the official openai-go client.
For step-by-step instructions on setting up OpenAI instrumentation, see the OpenAI integration guide.

Middleware

Middleware() creates an openai.Middleware that automatically traces OpenAI API calls.
Parameters:
  • instrumentationName - Name of your application or service
  • opts - Optional configuration options
Configuration Options (...Option):
function
Records the full input payload as a span attribute. This captures the complete request sent to the LLM.Example:
Enabling input capture may include sensitive data in your traces. Ensure this aligns with your data privacy requirements.
function
Records the full response payload as a span attribute. For streams, this is the final accumulated response.Example:
This is particularly useful for debugging and understanding what the LLM actually returned.
function
Sets the gen_ai.system attribute. Useful for identifying providers like "anthropic" or "azure". Defaults to "openai".Example:
function
Specifies the trace.TracerProvider to use. Defaults to the global provider.Example:

Filtering

Control which spans are exported to reduce noise and focus on what matters.

Preset Filters

Custom Filters

Use Include() to keep matching spans or Exclude() to remove them:

Matchers

Multiple filters use AND semantics (span must pass all). Within a Criteria, matchers use OR semantics (span matches if any matcher matches).

LangWatch Span Types

SpanType is a string constant used with span.SetType() to categorize spans in LangWatch for specialized UI treatment and analytics.
Using these span types is optional but highly recommended, as it enables LangWatch to provide more tailored insights and visualizations for your traces.

Collected Attributes

The OpenAI instrumentation automatically adds these attributes to spans:

Request Attributes

  • gen_ai.system - AI system name (e.g., “openai”)
  • gen_ai.request.model - Model used for the request
  • gen_ai.request.temperature - Temperature parameter
  • gen_ai.request.top_p - Top-p parameter
  • gen_ai.request.top_k - Top-k parameter
  • gen_ai.request.frequency_penalty - Frequency penalty
  • gen_ai.request.presence_penalty - Presence penalty
  • gen_ai.request.max_tokens - Maximum tokens
  • langwatch.gen_ai.streaming - Boolean indicating streaming
  • gen_ai.operation.name - Operation name (e.g., “completions”)
  • langwatch.input.value - Input content (if WithCaptureInput enabled)

Response Attributes

  • gen_ai.response.id - Response ID from the API
  • gen_ai.response.model - Model that generated the response
  • gen_ai.response.finish_reasons - Completion finish reasons
  • gen_ai.usage.input_tokens - Number of input tokens used
  • gen_ai.usage.output_tokens - Number of output tokens generated
  • gen_ai.openai.response.system_fingerprint - OpenAI system fingerprint
  • langwatch.output.value - Output content (if WithCaptureOutput enabled)

HTTP Attributes

Standard HTTP client attributes are also included:
  • http.request.method - HTTP method
  • url.path - Request path
  • server.address - Server address
  • http.response.status_code - HTTP status code

Request/Response Examples

Basic Chat Completion

RAG Pipeline

Error Handling

All SDK methods handle errors gracefully. In case of failures:
  1. Serialization errors - Fallback to string representation
  2. Network errors - Logged but don’t interrupt application flow
  3. Invalid data - Sanitized or excluded from traces
Example error handling:

Environment Variables

Complete Example

Here’s a comprehensive example showing a complete RAG application with proper error handling and best practices:

Version Compatibility

  • Go Version: 1.19 or later
  • OpenTelemetry: v1.24.0 or later
  • OpenAI Go SDK: Latest version

Support

For additional help:
For common setup issues and troubleshooting tips, see the Troubleshooting section in the integration guide.