What is Tracing?
Tracing captures the complete execution flow of your AI agent as a hierarchical tree of operations. Each node in the tree represents a “run”—an LLM call, a tool execution, or a custom function you want to observe. Think of tracing like a detailed flight recorder for your agent. It doesn’t just log what happened; it captures:- Inputs and outputs at every step
- Timing and latency for performance analysis
- Parent-child relationships showing how operations nest
- Metadata and tags for filtering and organization
- Errors and exceptions with full context
Setting Up LangSmith Tracing
1. Get Your API Key
First, create a LangSmith account at smith.langchain.com and generate an API key.2. Configure Environment Variables
Add these to your.env file:
3. Install Required Packages
Instrumenting Your Agent
Let’s see how to add tracing to a real agent. Here’s the evolution fromagent_v0.py (no tracing) to agent_v1.py (with tracing):
Before: No Tracing
After: With LangSmith Tracing
Two Simple Changes
- Wrap your OpenAI client with
wrap_openai()to auto-trace LLM calls - Add
@traceabledecorator to functions you want to trace
chat() creates a trace in LangSmith showing:
- The user’s question
- The messages sent to the model
- The model’s response
- Token usage and latency
- Any errors that occurred
Tracing Tools and Custom Functions
When your agent uses tools, you want to trace those too. Here’s how the OfficeFlow agent traces its database queries:Tracing Tool Calls
Tracing Knowledge Base Search
Run Types Explained
Therun_type parameter categorizes your traces:
llm
Direct calls to language models (usually auto-traced by
wrap_openai)chain
Sequences of operations, orchestration logic, or main entry points
tool
Tool executions like database queries, API calls, or retrieval operations
retriever
Specialized retrieval operations like vector database searches
embedding
Embedding generation (also auto-traced when using wrapped clients)
agent
High-level agent execution (alternative to “chain” for agent entry points)
Adding Metadata and Tags
Enrich your traces with context:Real-World Example: Tracing an Agent with Tools
Here’s a simplified version of the OfficeFlow agent showing complete instrumentation:- The main
chatfunction execution - First LLM call to decide on tool usage
- Tool execution (
query_database) - Second LLM call with tool results
- Final response to the user
Viewing Traces in LangSmith
Once your agent is instrumented:- Run your agent normally
- Visit smith.langchain.com
- Navigate to your project
- Click on any trace to see the detailed execution tree
- Expand/collapse nodes to focus on specific parts
- View inputs and outputs at each level
- See timing information for performance analysis
- Share trace URLs with teammates for debugging
- Filter by metadata, tags, or run type
Common Patterns
Pattern 1: Tracing Third-Party Agents
Even simple agents benefit from tracing:Pattern 2: Tracing Conversational Agents
For agents with conversation history:thread_id metadata lets you filter traces by conversation in the UI.
Best Practices
Trace Entry Points
Always trace your main agent function—this creates the root node that contains all other operations.
Trace Tools
Every tool should be traced so you can see exactly what arguments were passed and what was returned.
Use Descriptive Names
Name traces after their purpose: “query_database”, “search_knowledge”, not “function_1”, “helper_2”.
Add Context with Metadata
Include user IDs, session IDs, version numbers, or any context that helps you filter and analyze traces later.
Troubleshooting
Traces Not Appearing?
Check these common issues:- Environment variables: Verify
LANGSMITH_TRACING=trueandLANGSMITH_API_KEYare set - Project name: Ensure
LANGSMITH_PROJECTis configured - Network access: LangSmith needs to send traces to the API (check firewalls)
- Client wrapping: Make sure you’re using
wrap_openai()or the@traceabledecorator
Traces Too Verbose?
You can control granularity:Next Steps
Now that you can trace your agents:Evaluation Strategies
Learn how to use traces to systematically evaluate agent performance
Analyzing Traces
Discover techniques for debugging and improving agents using trace data