Trace Analysis with LangSmith and Langfuse
Reading traces: identifying slow tools, wrong decisions, and error patterns.
Why Trace Your Agent?
Agents make multiple LLM calls and tool invocations per run. Without tracing, debugging is guesswork. Tracing records every step: inputs, outputs, token usage, latency, and errors — giving you a complete picture of each run.
LangSmith Setup
LangSmith is Anthropic's tracing platform for LangChain. Enable it by setting two environment variables. Every LangChain call is automatically traced and visible in the LangSmith UI.
import os
from dotenv import load_dotenv
load_dotenv()
# LangSmith tracing configuration
os.environ['LANGCHAIN_TRACING_V2'] = 'true'
os.environ['LANGCHAIN_API_KEY'] = os.environ.get('LANGSMITH_API_KEY', 'ls__...')
os.environ['LANGCHAIN_PROJECT'] = 'my-agent-project'
# Now any LangChain code is automatically traced
from langchain_openai import ChatOpenAI
from langchain_core.messages import HumanMessage
llm = ChatOpenAI(model='gpt-4o-mini', api_key=os.environ.get('OPENAI_API_KEY', 'sk-...'))
# This call is traced automatically
response = llm.invoke([HumanMessage(content='What is 2+2?')])
print(response.content)
# Check trace at: https://smith.langchain.comAll lessons in this course
- Trace Analysis with LangSmith and Langfuse
- Per-Step Token and Cost Profiling
- Identifying Slow and Expensive Steps
- Root Cause Analysis for Agent Failures