Using LangWatch’s Langchain Callback Handler
This is the preferred and most comprehensive method for instrumenting Langchain with LangWatch. The LangWatch SDK provides aLangWatchCallbackHandler
that deeply integrates with Langchain’s event system.
setupObservability()
: Initializes LangWatch with default configuration.getLangWatchTracer()
: Creates a tracer instance for your application.tracer.withActiveSpan()
: Creates a parent LangWatch trace with automatic error handling and span management.LangWatchCallbackHandler
: A LangWatch-specific callback handler that captures Langchain events and converts them into detailed LangWatch spans.- The callback handler is passed to Langchain components via the
callbacks
option.
- Provides the most detailed Langchain-specific structural information (chains, agents, tools, LLMs as distinct steps).
- Works for all Langchain execution methods (
invoke
,stream
,batch
, etc.). - Automatically handles span lifecycle management with
withActiveSpan()
.
Why Use the LangWatch Langchain Callback Handler?
The LangWatch Langchain Callback Handler provides the richest, most Langchain-aware traces directly integrated with LangWatch’s tracing context. It’s the recommended approach for optimal Langchain-specific observability within LangWatch.Common Mistakes and Caveats
1. Setup and Initialization Issues
Multiple setup calls:
setupObservability()
can only be called once per process. Subsequent calls will throw an error.2. Callback Handler Usage
Reusing callback handlers: Each trace should use a fresh
LangWatchCallbackHandler
instance to avoid span conflicts.3. Span Management
Manual span management: Avoid manually managing spans when using
withActiveSpan()
. The function handles span lifecycle automatically.4. Environment Configuration
Missing environment variables: Ensure all required environment variables are set before running your application.
5. Error Handling
Unhandled promise rejections: Always handle errors in async operations to prevent unhandled promise rejections.
Best Practices Summary
- Call
setupObservability()
only once per process - Use fresh callback handlers for each trace to avoid conflicts
- Let
withActiveSpan()
handle span lifecycle - don’t manually end spans - Validate environment variables before starting your application
- Handle errors properly to avoid unhandled promise rejections
Example Project
You can find a complete example project demonstrating LangChain integration with LangWatch on our GitHub. This example includes:- Basic Chatbot: A simple chatbot that handles conversation flow using LangChain
- Conversation Management: User input handling and conversation history management
- Error Handling: Comprehensive error handling and exit commands
- Full LangWatch Integration: Complete observability and tracing setup
Key Features
- Automatic Tracing: All LangChain operations are automatically traced and sent to LangWatch
- Conversation Flow: Demonstrates proper conversation loop management
- Input/Output Tracking: Tracks user inputs and AI responses
- Error Recovery: Handles errors gracefully with proper cleanup
Related Documentation
For more advanced LangChain integration patterns and best practices:- Integration Guide - Basic setup and core concepts
- Manual Instrumentation - Advanced span management for LangChain operations
- Capturing RAG - RAG-specific patterns with LangChain
- Semantic Conventions - LangChain-specific attributes and conventions
- Debugging and Troubleshooting - Debug LangChain integration issues
LangChain’s automatic instrumentation works well with Manual Instrumentation for custom operations and Semantic Conventions for consistent attribute naming.