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.
| Features | Description |
|---|---|
| Execution Tracing | Records the execution trajectory of each Agent step |
| Performance Monitoring | Tracks the time consumption and Token usage of each call |
| Debug Replay | View detailed information of historical executions |
| Evaluation Testing | Create 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
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")
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"])
# 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 Variables | Description | Example |
|---|---|---|
| LANGCHAIN_TRACING_V2 | Enable tracing (must be set to true) | true |
| LANGCHAIN_API_KEY | LangSmith API Key | lsv2_pt_xxx |
| LANGCHAIN_PROJECT | Project name (used to group traces) | my-agent |
| LANGCHAIN_ENDPOINT | API endpoint (default is fine) | https://api.smith.langchain.com |
Other ExtensionsIn 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.