LangSmith Observability
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 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:
export LANGSMITH_TRACING=true
export LANGSMITH_API_KEY=<your-api-key>Quickstart
No extra code is needed to log a trace to LangSmith. Just run your agent code as you normally would:
from langchain.agents import create_agent
def send_email(to: str, subject: str, body: str):
"""Send an email to a recipient."""
# ... email sending logic
return f"Email sent to {to}"
def search_web(query: str):
"""Search the web for information."""
# ... web search logic
return f"Search results for: {query}"
agent = create_agent(
model="gpt-5.5",
tools=[send_email, search_web],
system_prompt="You are a helpful assistant that can send emails and search the web."
)
# Run the agent - all steps will be traced automatically
response = agent.invoke({
"messages": [{"role": "user", "content": "Search for the latest AI news and email a summary to john@example.com"}]
})By default, the trace will be logged to the project with the name 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’s tracing_context context manager:
import langsmith as ls
# This WILL be traced
with ls.tracing_context(enabled=True):
agent.invoke({"messages": [{"role": "user", "content": "Send a test email to alice@example.com"}]})
# This will NOT be traced (if LANGSMITH_TRACING is not set)
agent.invoke({"messages": [{"role": "user", "content": "Send another email"}]})Log to a project
Statically
You can set a custom project name for your entire application by setting the LANGSMITH_PROJECT environment variable:
export LANGSMITH_PROJECT=my-agent-projectDynamically
You can set the project name programmatically for specific operations:
import langsmith as ls
with ls.tracing_context(project_name="email-agent-test", enabled=True):
response = agent.invoke({
"messages": [{"role": "user", "content": "Send a welcome email"}]
})Add metadata to traces
You can annotate your traces with custom metadata and tags:
response = agent.invoke(
{"messages": [{"role": "user", "content": "Send a welcome email"}]},
config={
"tags": ["production", "email-assistant", "v1.0"],
"metadata": {
"user_id": "user_123",
"session_id": "session_456",
"environment": "production"
}
}
)tracing_context also accepts tags and metadata for fine-grained control:
with ls.tracing_context(
project_name="email-agent-test",
enabled=True,
tags=["production", "email-assistant", "v1.0"],
metadata={"user_id": "user_123", "session_id": "session_456", "environment": "production"}):
response = agent.invoke(
{"messages": [{"role": "user", "content": "Send a welcome email"}]}
)This custom metadata and tags will be attached to the trace in LangSmith.
Tip
To learn more about how to use traces to debug, evaluate, and monitor your agents, see the LangSmith documentation.