create_agent automatically support tracing through LangSmith, a platform for capturing, debugging, evaluating, and monitoring LLM application 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:- A LangSmith account: Sign up (for free) or log in at smith.langchain.com.
- A LangSmith API key: Follow the Create an API key guide.
Enable tracing
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:default. To configure a custom project name, see Log to a project.
Trace selectively
You may opt to trace specific invocations or parts of your application using LangSmith’stracing_context context manager:
Log to a project
Statically
Statically
You can set a custom project name for your entire application by setting the
LANGSMITH_PROJECT environment variable:Dynamically
Dynamically
You can set the project name programmatically for specific operations:
Add metadata to traces
You can annotate your traces with custom metadata and tags:tracing_context also accepts tags and metadata for fine-grained control: