Skip to main content

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 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

Note

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:

export LANGSMITH_AGENT_ID="<your-agent-id>"
export LANGSMITH_AGENT_ENVIRONMENT="production"
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"}]
})

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:

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"}]})

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.

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(
    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.