Setup
setupObservability()
Initializes the LangWatch observability system for Node.js environments, enabling data collection and tracing for your LLM application. This is typically the first function you’ll call when integrating LangWatch.
Configuration options for the LangWatch observability system.
An object containing a
shutdown()
method for graceful cleanup.SetupObservabilityOptions
Configuration options for setting up LangWatch observability.
LangWatch configuration. Set to ‘disabled’ to completely disable LangWatch integration.
Name of the service being instrumented.
Global attributes added to all telemetry data.
Configuration for automatic data capture. Can be “all”, “input”, “output”, “none”, or a configuration object.
Custom span processors for advanced trace processing.
Debug and development options.
Advanced and potentially unsafe configuration options.
LangWatchConfig
Configuration for LangWatch integration.
LangWatch API key for authentication.
LangWatch endpoint URL for sending traces and logs.
Type of span processor to use for LangWatch exporter.
DebugOptions
Debug and development options.
Enable console output for traces (debugging).
Enable console output for logs (debugging).
Log level for LangWatch SDK internal logging.
Custom logger for LangWatch SDK internal logging.
ObservabilityHandle
Handle returned from observability setup.
Gracefully shuts down the observability system.
Tracing
getLangWatchTracer()
Returns a LangWatch tracer instance that provides enhanced tracing capabilities for LLM applications.
The name of the tracer/service.
The version of the tracer/service.
A
LangWatchTracer
instance with enhanced methods for LLM observability.getLangWatchTracerFromProvider()
Get a LangWatch tracer from a specific OpenTelemetry tracer provider.
The OpenTelemetry tracer provider to use.
The name of the tracer/service.
The version of the tracer/service.
LangWatchTracer
The LangWatchTracer
extends the standard OpenTelemetry Tracer
with additional methods for LLM observability.
Methods
Starts a new
LangWatchSpan
without setting it on context. This method does NOT modify the current Context.Starts a new
LangWatchSpan
and calls the given function passing it the created span as first argument. The new span gets set in context and this context is activated for the duration of the function call.Starts a new
LangWatchSpan
, runs the provided async function, and automatically handles error recording, status setting, and span ending. This is a safer and more ergonomic alternative to manually using try/catch/finally blocks.createLangWatchSpan()
Creates a LangWatchSpan, which adds additional methods to an OpenTelemetry Span. You probably don’t need to use this directly, but it’s here for completeness.
The OpenTelemetry Span to add LangWatch methods to.
A LangWatchSpan with additional methods for LLM/GenAI observability.
LangWatchSpan
The LangWatchSpan
extends the standard OpenTelemetry Span
with additional methods for LLM observability.
Span Configuration Methods
Set the type of the span (e.g., ‘llm’, ‘rag’, ‘tool’, etc). This is used for downstream filtering and analytics.
Set the request model name for the span. This is typically the model name sent in the API request (e.g., ‘gpt-5’, ‘claude-3’).
Set the response model name for the span. This is the model name returned in the API response, if different from the request.
Set multiple RAG contexts for the span. Use this to record all retrieved documents/chunks used as context for a generation.
Set a single RAG context for the span. Use this if only one context was retrieved.
Set the metrics for the span.
Set the selected prompt for the span. This will attach this prompt to the trace. If this is set on multiple spans, the last one will be used.
Input/Output Methods
Record the input to the span with automatic type detection.
Record the input to the span with explicit type control. Supports “text”, “raw”, “chat_messages”, “list”, “json”, “guardrail_result”, and “evaluation_result” types.
Record the output from the span with automatic type detection.
Record the output from the span with explicit type control. Supports “text”, “raw”, “chat_messages”, “list”, “json”, “guardrail_result”, and “evaluation_result” types.
Client SDK
LangWatch
The main LangWatch client class that provides access to LangWatch services.
Configuration options for the LangWatch client.
Properties
Access to prompt management functionality.
Access to trace management functionality.
Prompt Management
langwatch.prompts.get()
Retrieves a prompt from the LangWatch platform.
The ID of the prompt to retrieve.
The prompt or compiled prompt object.
Throws an error if the specified prompt version is not found.
langwatch.prompts.create()
Creates a new prompt in the LangWatch platform.
Configuration options for creating the prompt.
The newly created prompt object.
langwatch.prompts.update()
Updates an existing prompt, creating a new version automatically.
The handle (identifier) of the prompt to update.
Configuration options for updating the prompt.
The updated prompt object (new version).
Each update operation creates a new version of the prompt. Previous versions are preserved for version control and rollback purposes.
langwatch.prompts.delete()
Deletes a prompt and all its versions from the LangWatch platform.
The handle (identifier) of the prompt to delete.
Confirmation of the deletion operation.
This action is irreversible and will permanently remove the prompt and all its versions.
Prompt Compilation
prompt.compile()
Compiles a prompt template with provided variables, using lenient compilation that handles missing variables gracefully.
Variables to substitute into the prompt template.
The compiled prompt with resolved variables and messages.
Lenient compilation will not throw errors for missing variables, making it suitable for dynamic content where some variables may be optional.
prompt.compileStrict()
Compiles a prompt template with strict variable validation, throwing an error if any required variables are missing.
Variables to substitute into the prompt template. All template variables must be provided.
The compiled prompt with resolved variables and messages.
Throws an error if any template variables are missing or invalid.
Strict compilation will throw a
PromptCompilationError
if any template variables are missing, ensuring all required data is provided.Processors
FilterableBatchSpanProcessor
A span processor that filters spans before processing them.
The span exporter to use.
Rules to exclude spans from processing.
LangChain Integration
LangWatchCallbackHandler
A LangChain callback handler that automatically traces LangChain operations and integrates them with LangWatch.
LangWatchCallbackHandler
automatically:
- Creates spans for LLM calls, chains, tools, and retrievers
- Captures input/output data
- Sets appropriate span types and metadata
- Handles errors and status codes
- Integrates with the LangWatch tracing system
convertFromLangChainMessages
Utility function to convert LangChain messages to a format compatible with LangWatch GenAI events.
Exporters
LangWatchExporter
A LangWatch exporter for sending traces to the LangWatch platform. Extends the OpenTelemetry OTLP HTTP trace exporter with proper authentication and metadata headers.
Optional API key for LangWatch authentication. If not provided, will use environment variables or fallback configuration.
Optional custom endpoint URL for LangWatch ingestion. If not provided, will use environment variables or fallback configuration.
LangWatchTraceExporter
A LangWatch trace exporter with configuration options.
LangWatchLogsExporter
A LangWatch logs exporter with configuration options.
Data Capture
DataCaptureOptions
Configuration for automatic data capture.
DataCapturePresets
Predefined data capture configurations.
Logging
getLangWatchLogger()
Returns a LangWatch logger instance for structured logging.
getLangWatchLoggerFromProvider()
Get a LangWatch logger from a specific logger provider.
ConsoleLogger
A console-based logger with configurable log levels and prefixes.
Logger configuration options.
NoOpLogger
A no-operation logger that discards all log messages.
CLI
The LangWatch CLI provides command-line tools for managing prompts and interacting with the LangWatch platform.Core Data Types
SpanType
Supported types of spans for LangWatch observability:
InputOutputType
Supported input/output types for span data:
LangWatchSpanRAGContext
Context for a RAG (Retrieval-Augmented Generation) span.
Unique identifier for the source document.
Unique identifier for the chunk within the document.
The actual content of the chunk provided to the model.
LangWatchSpanMetrics
Metrics for a LangWatch span.
The number of prompt tokens used.
The number of completion tokens used.
The cost of the span.
SpanProcessingExcludeRule
Defines a rule to filter out spans before they are exported to LangWatch.
The field of the span to match against. Currently, only
"span_name"
is supported.The value to match for the specified
fieldName
.The operation to use for matching.
PromptResponse
The raw prompt response type extracted from the OpenAPI schema.
Prompt
A prompt object retrieved from the LangWatch platform with compilation capabilities.
Unique identifier for the prompt.
Project identifier the prompt belongs to.
Organization identifier the prompt belongs to.
Optional handle/slug for the prompt.
Scope of the prompt - either organization-wide or project-specific.
Name of the prompt.
Last update timestamp.
Version number.
Version identifier.
Model used for the prompt.
The prompt template.
Array of message objects.
Response format configuration.
ID of the prompt author.
Creation timestamp.
Input definitions for the prompt.
Output definitions for the prompt.
CompiledPrompt
A compiled prompt that extends Prompt with reference to the original template.
The original prompt object before compilation.
TemplateVariables
Template variables for prompt compilation.
PromptCompilationError
Error thrown when prompt compilation fails.
The template that failed to compile.
The original compilation error.
LangWatchConstructorOptions
Configuration options for the LangWatch client.
Your LangWatch API key. Defaults to
process.env.LANGWATCH_API_KEY
.The LangWatch endpoint URL. Defaults to
process.env.LANGWATCH_ENDPOINT
.Additional options including custom logger.
Usage Examples
Basic Tracing
RAG Operations
Using Semantic Conventions
Prompt Management
LangChain Integration
Custom Span Processing
Advanced Setup with Data Capture
Graceful Shutdown
Related Documentation
For practical examples and advanced usage patterns, see:- Integration Guide - Get started with LangWatch TypeScript SDK
- Manual Instrumentation - Advanced span management techniques
- Semantic Conventions - Standardized attribute naming guidelines
- Debugging and Troubleshooting - Troubleshoot integration issues
- Framework Integrations - Framework-specific setup and examples
Start with the Integration Guide for a quick setup, then refer to this API reference for detailed configuration options.