LangSmith Integration Guide
Complete guide for integrating LangSmith observability into the MCP Server with LangGraph.Table of Contents
- Overview
- Quick Start
- Configuration
- Tracing
- Datasets and Evaluation
- Feedback Collection
- Debugging
- Best Practices
Overview
LangSmith is LangChain’s observability and debugging platform. It provides:- Automatic Tracing: Capture all LLM calls and agent steps
- Prompt Engineering: Iterate on prompts with production data
- Dataset Creation: Build test datasets from traces
- Evaluation: Compare model performance
- Debugging: Root cause analysis for failures
- User Feedback: Collect and analyze user ratings
- Cost Tracking: Monitor LLM API costs
Why LangSmith?
- ✅ For Developers:
- See exactly what your agent is doing
- Debug failures with full context
- Optimize prompts based on real usage
- Track costs per user/session
- ✅ For Teams:
- Share traces for collaboration
- Create regression test suites
- Monitor production performance
- Analyze user feedback
Quick Start
1. Create LangSmith Account
- Visit: https://smith.langchain.com/
- Sign up for free account
- Create a new project (e.g., “mcp-server-langgraph”)
2. Get API Key
- Go to: https://smith.langchain.com/settings
- Click “Create API Key”
- Copy the key (starts with
lsv2_pt_...)
3. Enable LangSmith Tracing
Option A: Environment Variables4. Run Your Agent
5. View Traces
- Go to: https://smith.langchain.com/
- Select your project
- See all traces appear in real-time!
Configuration
Environment Variables
Required:Programmatic Configuration
The agent is pre-configured to use LangSmith. Configuration is insrc/mcp_server_langgraph/core/config.py:
Dual Observability (OpenTelemetry + LangSmith)
This project supports both OpenTelemetry and LangSmith:- OpenTelemetry: Infrastructure metrics, distributed tracing, custom metrics
- LangSmith: LLM-specific tracing, prompt engineering, evaluations
Tracing
Automatic Tracing
When LangSmith is enabled, all agent invocations are automatically traced:- All LLM calls (prompts, completions, tokens)
- Agent routing decisions
- Tool invocations
- Intermediate states
- Timing information
- Error stack traces
Manual Tracing
Add custom metadata to traces:Nested Tracing
LangSmith automatically creates hierarchical traces:Tracing with Context
Add business context to traces:Datasets and Evaluation
Create Dataset from Traces
-
In LangSmith UI:
- Go to your project
- Filter traces (e.g., “success only”, “last 7 days”)
- Click “Add to Dataset”
- Name your dataset (e.g., “prod-examples-2025-01”)
- Programmatically:
Run Evaluations
Compare model performance on datasets:Custom Evaluators
Create custom evaluation metrics:Feedback Collection
Programmatic Feedback
Collect user feedback on responses:Feedback Schema
Built-in feedback types:-
Thumbs up/down: Binary rating
-
Star rating: 1-5 stars
-
Correctness: Factual accuracy
Feedback Analysis
View feedback in LangSmith:- Go to project
- Click “Feedback” tab
- Filter by feedback type
- Analyze trends over time
Debugging
Find Failing Traces
In LangSmith UI:- Go to your project
- Filter:
status:error - Sort by:
timestamp desc - Click on trace to see details
Analyze Slow Traces
Find performance bottlenecks: In LangSmith UI:- Filter:
latency > 5s - Sort by:
latency desc - Expand trace to see timing breakdown
- Identify slow LLM calls → use faster model
- Identify slow tool calls → add caching
- Identify redundant calls → optimize logic
Compare Traces
Compare successful vs failed traces:- Select two traces (shift+click)
- Click “Compare”
- See side-by-side diff of:
- Inputs
- Intermediate steps
- Outputs
- Timing
Root Cause Analysis
For any trace, you can see:- Full input/output: Exact data sent and received
- Intermediate steps: All agent decisions
- LLM calls: Prompts and completions
- Error stack traces: Full Python traceback
- Timing breakdown: Where time was spent
- Token usage: Tokens per LLM call
- Cost: Estimated cost per call
Best Practices
1. Project Organization
Create separate projects for:- Development:
my-agent-dev - Staging:
my-agent-staging - Production:
my-agent-prod
2. Tagging Strategy
Use consistent tags:- Environment:
production,staging,development - User tier:
free,pro,enterprise - Feature:
chat,analysis,search - Priority:
high,medium,low
3. Metadata Best Practices
Include actionable metadata:4. Sampling for High Volume
For very high-traffic applications:5. Cost Monitoring
Track costs in LangSmith:- Go to project
- Click “Analytics”
- View “Cost Over Time”
- Set budget alerts
- Use cheaper models for simple tasks
- Implement caching
- Set max token limits
- Monitor high-cost users
6. Privacy and Compliance
Redact sensitive data:- Delete user traces on request
- Set data retention policies
- Use metadata for user identification
7. Performance Monitoring
Set up monitoring for:- Latency (P95): Alert if >5 seconds
- Error rate: Alert if >5%
- Token usage: Alert on anomalies
- Cost per user: Track trends
Advanced Features
Prompt Hub
Save and version prompts:Online Evaluation
Run evaluations on production traces:A/B Testing
Compare model versions:Troubleshooting
Traces Not Appearing
Check:- API key is set:
echo $LANGSMITH_API_KEY - Tracing is enabled:
echo $LANGSMITH_TRACING - Project name is correct:
echo $LANGSMITH_PROJECT - Network connectivity to api.smith.langchain.com
Slow Tracing Performance
LangSmith tracing is async and shouldn’t slow down requests. If you experience slowness:- Check network latency to api.smith.langchain.com
- Reduce trace size: Avoid logging huge payloads
- Use sampling: Don’t trace every request
Missing LLM Calls
If LLM calls aren’t traced:Resources
- LangSmith Docs: https://docs.langchain.com/langsmith
- LangSmith Cookbook: https://github.com/langchain-ai/langsmith-cookbook
- API Reference: https://docs.smith.langchain.com/api-reference
- Community: https://github.com/langchain-ai/langsmith-sdk/discussions
- LangSmith support: https://support.langchain.com/
- Project issues: https://github.com/vishnu2kmohan/mcp-server-langgraph/issues
Last Updated: 2025-10-10