Langchain Instrumentation
Learn how to instrument Langchain applications with the LangWatch Python SDK.
Langchain is a powerful framework for building LLM applications. LangWatch integrates with Langchain to provide detailed observability into your chains, agents, LLM calls, and tool usage.
This guide covers the primary approaches to instrumenting Langchain with LangWatch:
- Using LangWatch’s Langchain Callback Handler (Recommended): The most direct method, using a specific callback provided by LangWatch to capture rich Langchain-specific trace data.
- Using Community OpenTelemetry Instrumentors: Leveraging dedicated Langchain instrumentors like those from OpenInference or OpenLLMetry.
- Utilizing Langchain’s Native OpenTelemetry Export (Advanced): Configuring Langchain to send its own OpenTelemetry traces to an endpoint where LangWatch can collect them.
1. Using LangWatch’s Langchain Callback Handler (Recommended)
This is the preferred and most comprehensive method for instrumenting Langchain with LangWatch. The LangWatch SDK provides a LangchainCallbackHandler
that deeply integrates with Langchain’s event system.
How it Works:
@langwatch.trace()
: Creates a parent LangWatch trace.current_trace.get_langchain_callback()
: Retrieves a LangWatch-specific callback handler linked to the current trace.RunnableConfig(callbacks=[langwatch_callback])
: Injects the handler into Langchain’s execution. Langchain emits events (on_llm_start, on_chain_end, etc.), which the handler converts into detailed LangWatch spans, correctly parented under the main trace.
Key points:
- Provides the most detailed Langchain-specific structural information (chains, agents, tools, LLMs as distinct steps).
- Works for all Langchain execution methods (
astream
,stream
,invoke
,ainvoke
).
2. Using Community OpenTelemetry Instrumentors
Dedicated Langchain instrumentors from libraries like OpenInference and OpenLLMetry can also be used to capture Langchain operations as OpenTelemetry traces, which LangWatch can then ingest.
Instrumenting Langchain with Dedicated Instrumentors
i. Via langwatch.setup()
ii. Direct Instrumentation
Key points for dedicated Langchain instrumentors:
- Directly instrument Langchain operations, providing traces from Langchain’s perspective.
- Requires installing the respective instrumentation package (e.g.,
openinference-instrumentation-langchain
oropentelemetry-instrumentation-langchain
for OpenLLMetry).
3. Utilizing Langchain’s Native OpenTelemetry Export (Advanced)
Langchain itself can be configured to export OpenTelemetry traces. If you set this up and configure Langchain to send traces to an OpenTelemetry collector endpoint that LangWatch is also configured to receive from (or if LangWatch is your OTLP endpoint), then LangWatch can ingest these natively generated Langchain traces.
Setup (Conceptual):
- Configure Langchain for OpenTelemetry export. This usually involves setting environment variables:
- Initialize LangWatch:
langwatch.setup()
.
Key points for Langchain’s native OTel export:
- LangWatch acts as a backend/collector for OpenTelemetry traces generated directly by Langchain.
- Requires careful configuration of Langchain’s environment variables.
- The level of detail depends on Langchain’s native OpenTelemetry instrumentation quality.
Which Approach to Choose?
- LangWatch’s Langchain Callback Handler (Recommended): Provides the richest, most Langchain-aware traces directly integrated with LangWatch’s tracing context. Ideal for most users.
- Dedicated Langchain Instrumentors (OpenInference, OpenLLMetry): Good alternatives if you prefer an explicit instrumentor pattern for Langchain itself or are standardizing on these specific OpenTelemetry ecosystems.
- Langchain’s Native OTel Export (Advanced): Suitable if you have an existing OpenTelemetry collection infrastructure and want Langchain to be another OTel-compliant source.
For the best Langchain-specific observability within LangWatch, the Langchain Callback Handler is the generally recommended approach, with dedicated Langchain Instrumentors as strong alternatives for instrumentor-based setups.