Tracing with LangSmith
Relevant source files
The following files were used as context for generating this wiki page:
LangSmith is a unified developer platform designed to help build, test, and monitor LLM applications. A key feature of LangSmith is its ability to provide detailed tracing and observability for LangChain applications. This allows developers to debug poor-performing LLM application runs, evaluate agent trajectories, and gain visibility into production systems to improve performance over time.
Tracing is integrated into the core of the LangChain framework, primarily through the Runnable interface and the callback system. By configuring LangSmith, developers can automatically capture detailed information about the execution of their chains, agents, and tools, providing a hierarchical view of each run.
Sources: README.md, libs/core/langchain_core/runnables/base.py
Configuration
To enable tracing with LangSmith, you need to configure a few environment variables. This setup directs the LangChain application to send trace data to your LangSmith project.
The following table details the necessary environment variables:
| Variable | Description | Default |
|---|---|---|
LANGSMITH_TRACING | Set to "true" to enable tracing. | Not set |
LANGSMITH_API_KEY | Your LangSmith API key. You can sign up for LangSmith to get one. | Not set |
LANGSMITH_PROJECT | The name of the project in LangSmith where traces will be sent. | "default" |
Here is an example of how to set these variables in your shell:
export LANGSMITH_TRACING=true
export LANGSMITH_API_KEY=<your-api-key>
export LANGSMITH_PROJECT=<your-project> # if not specified, defaults to "default"
Sources: libs/cli/langchain_cli/project_template/README.md:20-24, libs/cli/langchain_cli/package_template/README.md:21-25
Core Components
LangChainTracer
The LangChainTracer class is the central component for handling communication with the LangSmith platform. It acts as a callback handler that listens for events during a Runnable's execution and persists them as a trace.
This diagram shows the basic structure of the LangChainTracer.
Key responsibilities of LangChainTracer:
- Initialization: It can be initialized with a
langsmith.Clientinstance, aproject_name, andtagsto apply to all runs. - Trace Management: It handles the start and end of traces, creating
RunTreeobjects to represent the execution hierarchy. - Persistence: It sends the collected run data to the LangSmith API via the client.
Sources: libs/core/langchain_core/tracers/langchain.py, libs/core/tests/unit_tests/runnables/test_tracing_interops.py:33-40
Tracing Context and Handoff
LangChain provides multiple ways to manage the tracing context, ensuring that traces are correctly grouped and nested.
tracing_context
The langsmith.run_helpers.tracing_context context manager allows you to enable tracing for a specific block of code. This is useful for situations where you don't want to enable tracing globally.
# libs/core/tests/unit_tests/runnables/test_tracing_interops.py:43-54
with tracing_context(project_name=project_name, client=mock_client_, enabled=True):
assert my_function.invoke(1) == 2
@traceable Decorator
The @traceable decorator from the langsmith library can be used on functions to make them part of the current trace. When a Runnable invokes a function decorated with @traceable, the trace context is automatically handed off. This ensures that the decorated function's execution appears as a child run within the parent Runnable's trace.
This mechanism relies on context variables to pass the RunTree object, which maintains the state of the trace, including the trace_id, parent_run_id, and dotted_order for correct nesting.
Sources: libs/core/tests/unit_tests/runnables/test_tracing_interops.py:57-133
Tracing Execution Flow
When a Runnable is invoked with tracing enabled, a series of events are triggered through the callback system. The LangChainTracer listens to these events to construct and persist the trace.
This sequence diagram illustrates the tracing process for a Runnable.invoke() call.
- A
Runnable'sinvokemethod is called. - The
invokemethod internally calls_call_with_config, which gets aCallbackManager. CallbackManager.on_chain_startis triggered, which in turn callsLangChainTracer.on_chain_start.- The
LangChainTracercreates a new run and posts it to the LangSmith API. - The
Runnableexecutes its logic. If it calls otherRunnables or@traceablefunctions, they will appear as nested child runs. - Upon completion,
CallbackManager.on_chain_endis called, and theLangChainTracerupdates the run with the final output and posts the update to LangSmith.
Sources: libs/core/langchain_core/runnables/base.py:1008-1035, libs/core/tests/unit_tests/runnables/test_tracing_interops.py
Trace Structure
The trace data sent to LangSmith is structured to represent the execution hierarchy. Key fields in the posted data include:
| Field | Type | Description |
|---|---|---|
id | string (UUID) | A unique identifier for the run. |
trace_id | string (UUID) | The identifier for the entire trace. All runs in a single trace share this ID. |
parent_run_id | string (UUID) | The ID of the parent run. null for the root run. |
dotted_order | string | A string representing the run's position in the trace tree, e.g., "1.2.1". |
name | string | The name of the Runnable or function being executed. |
session_name | string | The project name in LangSmith. |
tags | list[str] | Tags associated with the run. |
The dotted_order and parent_run_id are crucial for correctly reconstructing the nested structure of the trace in the LangSmith UI.
Sources: libs/core/tests/unit_tests/runnables/test_tracing_interops.py:110-133
Summary
Tracing with LangSmith is a powerful feature integrated into LangChain's core Runnable execution model. By setting a few environment variables, developers gain deep visibility into their application's behavior. The system uses a callback mechanism, with LangChainTracer as the key component, to capture and persist execution traces. The interoperability between Runnable objects and the @traceable decorator allows for seamless and comprehensive tracing of complex chains and agentic workflows, which is invaluable for debugging, evaluation, and monitoring.
Sources: README.md, libs/core/langchain_core/runnables/base.py, libs/core/langchain_core/tracers/langchain.py