Last Updated: 3/7/2026
Python
Get started
Capabilities
Production
LangGraph APIs
LangSmith Observability
Prerequisites
Enable tracing
export LANGSMITH_TRACING=true export LANGSMITH_API_KEY=<your-api-key>
default
Trace selectively
tracing_context
`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
LANGSMITH_PROJECT
export LANGSMITH_PROJECT=my-agent-project
Dynamically
`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
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
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"}]} )
Use anonymizers to prevent logging of sensitive data in traces
`from langchain_core.tracers.langchain import LangChainTracer from langgraph.graph import StateGraph, MessagesState from langsmith import Client from langsmith.anonymizer import create_anonymizer
anonymizer = create_anonymizer([
Matches SSNs
{ “pattern”: r”\b\d{3}-?\d{2}-?\d{4}\b”, “replace”: "
tracer_client = Client(anonymizer=anonymizer) tracer = LangChainTracer(client=tracer_client)
Define the graph
graph = ( StateGraph(MessagesState) … .compile() .with_config({‘callbacks’: [tracer]}) )`
Was this page helpful?
Resources
Company