Skip to main content

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:
Use different project names for different environments (e.g., my-agent-dev, my-agent-staging, my-agent-prod) to keep traces organized.

3. Install Required Packages

Instrumenting Your Agent

Let’s see how to add tracing to a real agent. Here’s the evolution from agent_v0.py (no tracing) to agent_v1.py (with tracing):

Before: No Tracing

After: With LangSmith Tracing

Two Simple Changes

  1. Wrap your OpenAI client with wrap_openai() to auto-trace LLM calls
  2. Add @traceable decorator to functions you want to trace
That’s it! Now every call to 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

Now when you look at a trace in LangSmith, you’ll see:

Run Types Explained

The run_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)
Use consistent run types across your codebase. This makes filtering and analysis much easier in the LangSmith UI.

Adding Metadata and Tags

Enrich your traces with context:
You can also add tags programmatically:

Real-World Example: Tracing an Agent with Tools

Here’s a simplified version of the OfficeFlow agent showing complete instrumentation:
This creates a beautiful hierarchical trace showing:
  • The main chat function 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:
  1. Run your agent normally
  2. Visit smith.langchain.com
  3. Navigate to your project
  4. Click on any trace to see the detailed execution tree
You can:
  • 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
Pro tip: When debugging, add the LangSmith trace URL to your issue tracker. This gives you and your team the exact context needed to investigate problems.

Common Patterns

Pattern 1: Tracing Third-Party Agents

Even simple agents benefit from tracing:

Pattern 2: Tracing Conversational Agents

For agents with conversation history:
The 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:
  1. Environment variables: Verify LANGSMITH_TRACING=true and LANGSMITH_API_KEY are set
  2. Project name: Ensure LANGSMITH_PROJECT is configured
  3. Network access: LangSmith needs to send traces to the API (check firewalls)
  4. Client wrapping: Make sure you’re using wrap_openai() or the @traceable decorator

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