Skip to main content

LangSmith Integration Guide

Complete guide for integrating LangSmith observability into the MCP Server with LangGraph.

Table of Contents


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

  1. Visit: https://smith.langchain.com/
  2. Sign up for free account
  3. Create a new project (e.g., “mcp-server-langgraph”)

2. Get API Key

  1. Go to: https://smith.langchain.com/settings
  2. Click “Create API Key”
  3. Copy the key (starts with lsv2_pt_...)

3. Enable LangSmith Tracing

Option A: Environment Variables
Option B: Programmatic Configuration

4. Run Your Agent

5. View Traces

  1. Go to: https://smith.langchain.com/
  2. Select your project
  3. See all traces appear in real-time!

Configuration

Environment Variables

Required:
Optional:

Programmatic Configuration

The agent is pre-configured to use LangSmith. Configuration is in src/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
Enable both:
Use only LangSmith:

Tracing

Automatic Tracing

When LangSmith is enabled, all agent invocations are automatically traced:
What’s captured:
  • 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

  1. 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”)
  2. 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:
  1. Go to project
  2. Click “Feedback” tab
  3. Filter by feedback type
  4. Analyze trends over time

Debugging

Find Failing Traces

In LangSmith UI:
  1. Go to your project
  2. Filter: status:error
  3. Sort by: timestamp desc
  4. Click on trace to see details
Programmatically:

Analyze Slow Traces

Find performance bottlenecks: In LangSmith UI:
  1. Filter: latency > 5s
  2. Sort by: latency desc
  3. Expand trace to see timing breakdown
Optimize based on findings:
  • 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:
  1. Select two traces (shift+click)
  2. Click “Compare”
  3. 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:
  1. Go to project
  2. Click “Analytics”
  3. View “Cost Over Time”
  4. Set budget alerts
Optimize costs:
  • Use cheaper models for simple tasks
  • Implement caching
  • Set max token limits
  • Monitor high-cost users

6. Privacy and Compliance

Redact sensitive data:
GDPR compliance:
  • 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:
  1. API key is set: echo $LANGSMITH_API_KEY
  2. Tracing is enabled: echo $LANGSMITH_TRACING
  3. Project name is correct: echo $LANGSMITH_PROJECT
  4. Network connectivity to api.smith.langchain.com
Test connection:

Slow Tracing Performance

LangSmith tracing is async and shouldn’t slow down requests. If you experience slowness:
  1. Check network latency to api.smith.langchain.com
  2. Reduce trace size: Avoid logging huge payloads
  3. Use sampling: Don’t trace every request

Missing LLM Calls

If LLM calls aren’t traced:

Resources

Need help?
Last Updated: 2025-10-10