π LangChain Learning Path - Step 7 of 7
- β Step 6: Build Your Agent Project
- Step 7 (this page): Observability with LangSmith
- π Course Complete!
π Hands-On Practice
β¬οΈ Download Jupyter Notebook - Set up LangSmith tracing and learn to debug and monitor your AI applications.
The Problem: AI is a Black Box
When your AI agent doesnβt work as expected, itβs hard to know why:
Agent"] --> R["β Wrong Answer"] Note["What happened inside?
- Which tools were called?
- What did the LLM see?
- Where did it go wrong?"] style BB fill:#fecaca,stroke:#dc2626 style Note fill:#fef3c7,stroke:#d97706
Common problems:
- Agent uses wrong tool
- LLM hallucinates
- Slow performance
- Unexpected errors
- High costs
LangSmith solves this by making everything visible!
What is LangSmith?
LangSmith is like a video replay system for your AI - it records every step so you can see exactly what happened.
Records Everything"] LS --> T["π Traces
See each step"] LS --> D["π Debugging
Find issues"] LS --> P["β‘ Performance
Optimize speed"] LS --> C["π° Costs
Track spending"] style A fill:#dbeafe,stroke:#2563eb style LS fill:#fef3c7,stroke:#d97706 style T fill:#d1fae5,stroke:#059669 style D fill:#fce7f3,stroke:#db2777 style P fill:#e0e7ff,stroke:#6366f1 style C fill:#fef3c7,stroke:#d97706
Real-world analogy: Like a sports replay - you can see every play, slow it down, and understand what went wrong.
Getting Started
Step 1: Sign Up
- Go to smith.langchain.com
- Sign up for free account
- Create a new project
Step 2: Get API Key
- Go to Settings β API Keys
- Create new API key
- Copy it
Step 3: Configure Your Code
# Add to .env file
LANGCHAIN_TRACING_V2=true
LANGCHAIN_API_KEY=your_langsmith_api_key
LANGCHAIN_PROJECT=your_project_name
import os
from dotenv import load_dotenv
load_dotenv()
# That's it! LangSmith is now tracking your agent
Understanding Traces
What is a Trace?
A trace is a complete record of one interaction with your agent.
Decides to use tool"] Step1 --> Step2["π§ Tool Call:
get_weather('NYC')"] Step2 --> Step3["π€ LLM Call 2:
Formats answer"] Step3 --> End["π¬ Response:
It's sunny, 72Β°F"] style Trace fill:#fef3c7,stroke:#d97706 style Step1 fill:#dbeafe,stroke:#2563eb style Step2 fill:#fce7f3,stroke:#db2777 style Step3 fill:#dbeafe,stroke:#2563eb style End fill:#d1fae5,stroke:#059669
Each trace shows:
- Inputs: What went in
- Outputs: What came out
- Steps: Everything in between
- Timing: How long each step took
- Costs: How much each call cost
Viewing Traces in LangSmith
The Trace View
When you open a trace, you see a timeline:
Trace: "What's the weather in New York?"
ββ π€ ChatOpenAI (230ms, $0.002)
β ββ Input: "What's the weather in New York?"
β ββ Output: [calls get_weather tool]
β
ββ π§ get_weather (45ms, $0.000)
β ββ Input: {"city": "New York"}
β ββ Output: "Sunny, 72Β°F"
β
ββ π€ ChatOpenAI (180ms, $0.001)
ββ Input: [tool result + original question]
ββ Output: "It's sunny and 72Β°F in New York!"
Total: 455ms, $0.003
What You Can See
Common Debugging Scenarios
Scenario 1: Agent Uses Wrong Tool
Problem: Agent calls send_email instead of search_email
How to debug:
- Open trace in LangSmith
- Find the LLM call before tool use
- Check the Prompt tab
- See what the LLM saw
What you might find:
Available tools:
- send_email: Send an email β Tool description unclear!
- search_email: Find emails
User: "Find my emails from Bob"
Fix: Improve tool description:
@tool
def search_email(query: str) -> str:
"""
SEARCH for existing emails (does NOT send).
Use this to FIND or LOOKUP emails.
"""
Scenario 2: LLM Hallucination
Problem: Agent makes up information not in the retrieved documents
How to debug:
- Find the generation step
- Check the Context provided
- Compare to the output
Example:
Context provided:
"Employee handbook says: 15 vacation days"
LLM output:
"You get 20 vacation days and unlimited sick leave"
β Made up! β Not in context!
Fix: Add stricter prompt:
prompt = """
CRITICAL: Only use information from the provided context.
If info is not in context, say "I don't have that information."
Do NOT make up or infer information.
Context: {context}
"""
Scenario 3: Slow Performance
Problem: Agent takes 10 seconds to respond
How to debug:
- Look at timing for each step
- Find the bottleneck
Example trace:
ββ Retrieval (50ms) β
Fast
ββ LLM Call 1 (8000ms) β SLOW!
ββ LLM Call 2 (100ms) β
Fast
Total: 8150ms
Fix: Reduce input size for slow LLM call:
# Before: Sending entire document (10,000 tokens)
# After: Send only relevant chunks (500 tokens)
Scenario 4: High Costs
Problem: $50 bill for 100 requests
How to debug:
- Click βAnalyticsβ in LangSmith
- See cost breakdown
- Find expensive calls
What you might find:
GPT-4 calls: $45 (10,000 requests with 4,000 tokens each)
GPT-3.5 calls: $5 (1,000 requests)
Fix: Use cheaper model when possible:
# For simple tasks
cheap_model = ChatOpenAI(model="gpt-3.5-turbo")
# For complex tasks only
expensive_model = ChatOpenAI(model="gpt-4")
Monitoring Patterns
Pattern 1: Testing Changes
Compare before and after:
# Tag your runs
from langsmith import traceable
@traceable(run_type="chain", tags=["version-1"])
def old_chain(input):
# Old implementation
pass
@traceable(run_type="chain", tags=["version-2"])
def new_chain(input):
# New improved implementation
pass
# In LangSmith, filter by tags and compare metrics
Pattern 2: Quality Monitoring
Track quality metrics:
drop?"} Alert -->|Yes| Notify["π Alert team"] style Prod fill:#dbeafe,stroke:#2563eb style Track fill:#fef3c7,stroke:#d97706 style Alert fill:#fce7f3,stroke:#db2777 style Notify fill:#fecaca,stroke:#dc2626
Pattern 3: Error Tracking
Automatically detect issues:
from langsmith import Client
client = Client()
# Get failed runs
failed_runs = client.list_runs(
project_name="my-project",
filter="error eq true",
start_time=yesterday
)
for run in failed_runs:
print(f"Error: {run.error}")
print(f"Input: {run.inputs}")
# Alert or fix automatically
Advanced Features
1. Datasets & Testing
Create test sets to ensure quality:
from langsmith import Client
client = Client()
# Create dataset
dataset = client.create_dataset("customer-service-tests")
# Add examples
client.create_examples(
dataset_id=dataset.id,
inputs=[
{"question": "What's my order status?"},
{"question": "How do I return an item?"},
],
outputs=[
{"should_use_tool": "check_order"},
{"should_use_tool": "return_policy"},
]
)
# Run tests
def test_agent():
for example in dataset.examples:
result = agent.invoke(example.inputs)
# Validate result matches expected output
2. Feedback & Ratings
Collect user feedback:
from langsmith import Client
client = Client()
# User gives feedback
client.create_feedback(
run_id=run_id,
key="helpfulness",
score=5, # 1-5 stars
comment="Very helpful!"
)
# View feedback in LangSmith dashboard
3. Playground Testing
Test prompts interactively:
- Go to LangSmith Playground
- Paste your prompt
- Try different inputs
- See results immediately
- Compare versions side-by-side
Real-World Example
Letβs add full observability to our Research Assistant:
import os
from dotenv import load_dotenv
from langsmith import traceable
from langchain_openai import ChatOpenAI
from langgraph.prebuilt import create_react_agent
# Enable tracing
load_dotenv()
os.environ["LANGCHAIN_TRACING_V2"] = "true"
os.environ["LANGCHAIN_PROJECT"] = "research-assistant"
# Tools with better descriptions for debugging
@traceable(name="web_search_tool")
def web_search(query: str) -> str:
"""
Search the web for information.
WHEN TO USE: User asks about current info, facts, or research topics.
DO NOT USE: For personal info, past conversations, or saved notes.
"""
# Implementation
pass
@traceable(name="save_notes_tool")
def save_notes(content: str, title: str) -> str:
"""
Save research findings to notes.
WHEN TO USE: After finding important information worth remembering.
DO NOT USE: For every small detail.
"""
# Implementation
pass
# Create agent with metadata
agent = create_react_agent(
ChatOpenAI(model="gpt-4"),
tools=[web_search, save_notes],
state_modifier="You are a research assistant..."
)
# Track user sessions
@traceable(
run_type="chain",
name="research_session",
tags=["production", "v2.0"]
)
def handle_user_query(user_id: str, query: str):
"""Handle user query with full tracing."""
config = {
"configurable": {"thread_id": user_id},
"metadata": {
"user_id": user_id,
"environment": "production"
},
"tags": ["user_query"]
}
result = agent.invoke({"messages": [("user", query)]}, config)
return result["messages"][-1].content
# Use it
response = handle_user_query("user_123", "Research quantum computing")
What you get in LangSmith:
Session: research_session
ββ Metadata: user_id=user_123, environment=production
ββ Tags: production, v2.0, user_query
β
ββ π§ web_search_tool (234ms, $0.000)
β ββ Output: "Quantum computing uses..."
β
ββ π€ ChatOpenAI (890ms, $0.005)
β ββ Decides to save notes
β
ββ π§ save_notes_tool (45ms, $0.000)
ββ Saved: "Quantum Computing Basics"
Total: 1169ms, $0.005
β
Success
Best Practices
1. Add Meaningful Tags
tags = [
"environment:production",
"user_type:premium",
"feature:research",
"version:2.1"
]
2. Include Metadata
metadata = {
"user_id": "12345",
"session_id": "abc",
"feature_flags": ["new_ui"],
"model_version": "gpt-4"
}
3. Name Your Traces
@traceable(name="generate_report", run_type="chain")
def generate_report(data):
# Clear name in dashboard
pass
4. Monitor Key Metrics
Set up alerts for:
- Error rate > 5%
- Average latency > 2s
- Cost per request > $0.10
- Success rate < 95%
5. Regular Reviews
Weekly:
- Check failed runs
- Review slow traces
- Analyze cost trends
- Read user feedback
Troubleshooting Tips
Traces Not Showing Up?
Check:
# 1. Environment variables set?
print(os.getenv("LANGCHAIN_TRACING_V2")) # Should be "true"
print(os.getenv("LANGCHAIN_API_KEY")) # Should have value
# 2. Internet connection working?
# 3. Project name correct?
print(os.getenv("LANGCHAIN_PROJECT"))
# 4. Try manual trace
from langsmith import Client
client = Client()
client.create_run(...) # Test connection
Too Many Traces?
Filter traces:
# Only trace in development
if os.getenv("ENVIRONMENT") == "development":
os.environ["LANGCHAIN_TRACING_V2"] = "true"
Need to Hide Sensitive Data?
@traceable(name="process_payment",
hide_inputs=True, # Don't log input
hide_outputs=True) # Don't log output
def process_payment(card_number, cvv):
# Sensitive operation
pass
The Debugging Workflow
in LangSmith"] Find --> Analyze["π Analyze Steps"] Analyze --> Root["π― Find Root Cause"] Root --> Fix["π§ Fix Code"] Fix --> Test["β Test with
Same Input"] Test --> Verify["πΉ Compare
New Trace"] Verify --> Deploy["π Deploy Fix"] style Issue fill:#fecaca,stroke:#dc2626 style Find fill:#fef3c7,stroke:#d97706 style Analyze fill:#dbeafe,stroke:#2563eb style Root fill:#fce7f3,stroke:#db2777 style Fix fill:#d1fae5,stroke:#059669 style Deploy fill:#d1fae5,stroke:#059669
What Youβve Learned
β
Why observability matters for AI
β
Setting up LangSmith
β
Reading and understanding traces
β
Debugging common issues
β
Monitoring production systems
β
Testing and quality assurance
β
Best practices for tracing
You now have the tools to build, debug, and monitor production AI systems!
π Congratulations!
Youβve completed the LangChain Learning Path! You now know:
- Foundations - LLM orchestration concepts
- Essentials - Chat models, prompting, LCEL
- Tool Calling - Extending LLMs with capabilities
- RAG - Teaching AI about your documents
- LangGraph - Building complex agent workflows
- Projects - Building complete applications
- Observability - Debugging and monitoring
Whatβs Next?
- Build your own projects using what youβve learned
- Explore advanced patterns in the LangChain docs
- Join the community on Discord
- Share your creations on GitHub
Resources
Keep Learning
More Topics to Explore:
- Advanced prompting techniques
- Multi-modal AI (images, audio)
- Fine-tuning custom models
- Scaling to production
- Security and privacy
Other Resources:
- β Back to Foundations to review
- Explore other topics in the handbook
- Try building your own agent variations
Thank you for learning with us! π
Now go build amazing AI applications! π―