Skip to main content
As you build and run agents with LangChain, you need visibility into how they behave: which tools they call, what prompts they generate, and how they make decisions. LangChain agents built with createAgent automatically support tracing through LangSmith, a platform for capturing, debugging, evaluating, and monitoring agent behavior. Traces record every step of your agent’s execution, from the initial user input to the final response, including all tool calls, model interactions, and decision points. This execution data helps you debug issues, evaluate performance across different inputs, and monitor usage patterns in production. This guide shows you how to enable tracing for your LangChain agents and use LangSmith to analyze their execution.

Prerequisites

Before you begin, ensure you have the following:

Enable tracing

In an agent-based workspace, create an agent once before sending traces. Use agent addressing to select its identifier and environment. If an integration requires project_name, use the selected environment’s tracer_session_name as a compatibility value.
All LangChain agents automatically support LangSmith tracing. To enable it, set the following environment variables:

Quickstart

No extra code is needed to log a trace to LangSmith. Just run your agent code as you normally would:
Traces appear in the destination configured during setup.

Trace selectively

You may opt to trace specific invocations or parts of your agent using LangSmith’s tracing_context context manager:

Select an agent and environment

Use LANGSMITH_AGENT_ID and LANGSMITH_AGENT_ENVIRONMENT to select the registered agent and environment. For per-request routing, pass an agent address in code. See Log traces to an agent.
Set LANGSMITH_PROJECT to the tracing project name instead of setting the agent variables. For integrations that require project_name in an agent-based workspace, use the environment’s tracer_session_name as a compatibility value.

Add metadata to traces

You can annotate your traces with custom metadata and tags:
This custom metadata and tags will be attached to the trace in LangSmith.
To learn more about how to use traces to debug, evaluate, and monitor your agents, see the LangSmith documentation.