LangChain LangSmith -- Observability

LangSmith is LangChain's official observability platform that helps you trace Agent execution processes, monitor performance, and debug issues.


What is LangSmith

When an Agent runs in the background, you can't see what's happening inside it — which models were called, which tools were executed, and how many Tokens each step consumed. LangSmith solves this "black box" problem.

FeaturesDescription
Execution TracingRecords the execution trajectory of each Agent step
Performance MonitoringTracks the time consumption and Token usage of each call
Debug ReplayView detailed information of historical executions
Evaluation TestingCreate test sets to evaluate Agent performance

Quick Start

Registration and Installation

$ pip install langsmith

Insmith.langchain.comRegister an account, obtain an API Key, then configure it in .env:

Example

# .env file
LANGCHAIN_TRACING_V2=true
LANGCHAIN_API_KEY=lsv2_pt_your_key_here
LANGCHAIN_PROJECT=my-agent-project

Automatic Tracing

Example

from dotenv import load_dotenv
load_dotenv()  # LangSmith configuration will be loaded automatically

from langchain.agents import create_agent
from langchain.chat_models import init_chat_model
from langchain.messages import HumanMessage
from langchain.tools import tool

# After setting environment variables, all Agent executions will be automatically traced
# No additional code needed!

@tool
def search_course(keyword: str) -> str:
    """Search courses"""
    return f"Search results: courses related to {keyword}"

model = init_chat_model("deepseek:deepseek-v4-flash", temperature=0)
agent = create_agent(
    model=model,
    tools=[search_course],
    system_prompt="You are the assistant of the Example tutorial.",
)

# This execution will be automatically recorded to LangSmith
result = agent.invoke({
    "messages": [HumanMessage(content="Search Python courses")]
})

# Open https://smith.langchain.com to view trace records
print("Completed. Please go to the LangSmith console to view trace details")

Viewing Trace Records

In the LangSmith console, you can see the complete trajectory of each Agent execution:

  • Execution timeline: Complete timeline from model call → tool call → model call again
  • Input/Output: The input messages and model return results for each step
  • Token Usage: Token consumption and cost estimation for each model call
  • Latency Analysis: Time distribution of each step
  • Error Messages: If an error occurs in a step, you can see the complete error stack trace

Manually Creating Traces

Example

from langsmith import traceable

# Use the @traceable decorator to mark functions that need tracing
@traceable
def process_user_query(query: str) -> dict:
    """Process user query (this function will be traced separately)"""
    # Preprocessing
    cleaned = query.strip().lower()
    # Call Agent
    result = agent.invoke({"messages": [HumanMessage(content=cleaned)]})
    return {
        "query": cleaned,
        "answer": result["messages"][-1].content,
    }

# In LangSmith, you will see process_user_query as an independent step
result = process_user_query("Python course recommendations")
print(result["answer"])

Common Configuration

Environment VariablesDescriptionExample
LANGCHAIN_TRACING_V2Enable tracing (must be set to true)true
LANGCHAIN_API_KEYLangSmith API Keylsv2_pt_xxx
LANGCHAIN_PROJECTProject name (used to group traces)my-agent
LANGCHAIN_ENDPOINTAPI endpoint (default is fine)https://api.smith.langchain.com

In production, it is recommended to set LangSmith's trace sampling rate lower (to avoid the high cost of recording all requests), and only enable full tracing when debugging is needed.

Other Extensions