AnyLearn
All lessons
Programmingintermediate

LangSmith: Tracing & Evaluating Your LLM Applications

Dive into LangSmith, the developer platform for building and evaluating robust Large Language Model (LLM) applications. Learn how to trace execution paths, debug complex chains, and rigorously evaluate your LLM's performance to ensure reliability and quality.

Not signed in — your progress and quiz score won't be saved.
Lesson progress1 / 9

Introduction to LangSmith: The LLM Developer's Workbench

Developing applications powered by Large Language Models (LLMs) often feels like working in a black box. Unlike traditional software, where every line of code behaves deterministically, LLMs introduce inherent non-determinism, making debugging and performance optimization incredibly challenging. This is where LangSmith steps in.

LangSmith is a unified platform from LangChain designed to help developers build, evaluate, and monitor LLM applications at every stage of their lifecycle. It provides critical visibility into the complex interactions within your LLM chains, from prompt engineering to tool usage, allowing you to debug issues, compare different approaches, and systematically improve your application's quality and reliability. Think of it as an observability and MLOps platform specifically tailored for LLMs.

Its core functionalities revolve around tracing individual requests, enabling detailed inspection of inputs, outputs, intermediate steps, and performance metrics. Beyond debugging, LangSmith empowers you to define datasets, run automated evaluations, and collaborate with human annotators to ensure your LLM application meets the desired criteria for accuracy, safety, and helpfulness before deployment and throughout its operation.

Full lesson text

All 9 steps on one page — for reading, reference, and search.

Show

1. Introduction to LangSmith: The LLM Developer's Workbench

Developing applications powered by Large Language Models (LLMs) often feels like working in a black box. Unlike traditional software, where every line of code behaves deterministically, LLMs introduce inherent non-determinism, making debugging and performance optimization incredibly challenging. This is where LangSmith steps in.

LangSmith is a unified platform from LangChain designed to help developers build, evaluate, and monitor LLM applications at every stage of their lifecycle. It provides critical visibility into the complex interactions within your LLM chains, from prompt engineering to tool usage, allowing you to debug issues, compare different approaches, and systematically improve your application's quality and reliability. Think of it as an observability and MLOps platform specifically tailored for LLMs.

Its core functionalities revolve around tracing individual requests, enabling detailed inspection of inputs, outputs, intermediate steps, and performance metrics. Beyond debugging, LangSmith empowers you to define datasets, run automated evaluations, and collaborate with human annotators to ensure your LLM application meets the desired criteria for accuracy, safety, and helpfulness before deployment and throughout its operation.

2. Why Tracing is Indispensable for LLM Apps

The opacity of LLM-powered applications is their greatest challenge. A single user query might trigger a cascade of events: a prompt sent to an LLM, the LLM generating a tool call, the tool executing a database query, the result fed back to the LLM, and finally, a synthesized response. If the final output is incorrect or unexpected, pinpointing the exact failure point in this multi-step process can be like finding a needle in a haystack without proper tracing.

Tracing provides a complete, granular record of every interaction and operation within your LLM application's execution path. For each 'run' (an invocation of your chain or agent), LangSmith captures:

  • Inputs and Outputs: What went into each component and what came out.
  • Intermediate Steps: Every LLM call, tool invocation, and retriever query.
  • Latency: How long each step took, identifying bottlenecks.
  • Token Usage: The number of input and output tokens, crucial for cost management.
  • Errors: Where and why failures occurred.

This level of detail is critical for debugging, understanding performance characteristics, and ensuring that your application behaves as intended under various conditions. It transforms the black box into a transparent, inspectable system.

3. Anatomy of a LangSmith Trace

A simplified view of how LangSmith traces an LLM agent's execution.

flowchart TD
  UserRequest["User Request"]
  Agent["Agent Invocation"]
  LLM_Call["LLM Call (Thought)"]
  Tool_Use["Tool Use (Action)"]
  LLM_Reply["LLM Call (Observation & Final Answer)"]
  UserResponse["User Response"]

  UserRequest --> Agent
  Agent --> LLM_Call
  LLM_Call --> Tool_Use
  Tool_Use --> Agent
  Agent --> LLM_Reply
  LLM_Reply --> UserResponse

  subgraph "LangSmith Trace"
    Agent
    LLM_Call
    Tool_Use
    LLM_Reply
  end

4. Integrating LangSmith with LangChain

LangSmith is deeply integrated with the LangChain framework, making it straightforward to enable tracing for your existing LangChain applications. The setup primarily involves configuring a few environment variables. When these variables are set, LangChain automatically instruments your runs and sends the data to your LangSmith project.

Here's how to get started:

  1. Install LangChain and LangSmith Client: Ensure you have langchain and langsmith installed: pip install langchain langsmith.

  2. Set Environment Variables: You'll need your LangSmith API key and project name. If you don't specify a project, runs will default to a project named 'default'.

    export LANGCHAIN_TRACING_V2='true'
    export LANGCHAIN_API_KEY='<your_langsmith_api_key>'
    export LANGCHAIN_PROJECT='my-llm-app-project'
    
  3. Run Your LangChain Application: Any Runnable or Chain invocation will now automatically generate traces visible in the LangSmith UI. LangSmith uses a hierarchical structure, where a top-level invocation (e.g., an AgentExecutor call) becomes a parent run, and its internal LLM calls, tool uses, and retriever invocations become child runs. This gives you a complete, nested view of your application's execution flow, making debugging a breeze.

5. The Imperative of LLM Application Evaluation

Once you've built an LLM application, how do you know if it's actually good? This isn't a trivial question. Traditional software testing relies on predictable, deterministic outcomes. For LLMs, subjectivity, creativity, and non-determinism make evaluation a far more complex undertaking. You need to verify not just that the code runs, but that the LLM generates helpful, accurate, and safe responses consistently.

Evaluation is critical for:

  • Quality Assurance: Ensuring the LLM behaves as expected across various inputs and scenarios.
  • Regression Prevention: Catching unintended performance drops when changing prompts, models, or data.
  • Model Selection: Comparing different LLMs or fine-tuned versions to determine the best performer for your task.
  • Iterative Improvement: Providing objective metrics to guide development and prompt engineering efforts.
  • Cost Optimization: Understanding the trade-offs between model performance and token usage.

LangSmith facilitates both human-in-the-loop evaluation (collecting feedback) and automated evaluation (using reference answers or critique models) to provide a comprehensive view of your application's performance.

6. Crafting Datasets for Robust Evaluation

The foundation of any meaningful evaluation in LangSmith is a well-constructed dataset. A dataset in LangSmith is a collection of input-output examples, often including ground truth answers, that represent the range of scenarios your LLM application is expected to handle. These examples serve as a benchmark against which your application's performance is measured.

Consider these types of examples for your dataset:

  • Question-Answer Pairs: For retrieval-augmented generation (RAG) systems or chatbots.
  • Summarization Tasks: Original text input and ideal summary output.
  • Classification Tasks: Input text and the correct category label.
  • Edge Cases & Failure Modes: Specific inputs that have historically caused problems, to ensure regressions are caught.

LangSmith allows you to create datasets in several ways:

  • Manual Creation: Uploading CSVs or JSON files directly through the UI or API.
  • From Traces: Exporting successful or failed traces from your development runs into a dataset, which is incredibly useful for capturing real-world interactions.

It's crucial that your evaluation datasets are representative of your production traffic and cover diverse scenarios to avoid bias and ensure your evaluation metrics truly reflect real-world performance.

7. Running Evaluations with LangSmith

Once you have a dataset, LangSmith makes it straightforward to run evaluations. You can select an existing dataset, choose the LangChain 'chain' or 'runnable' you want to evaluate, and configure your evaluators. LangSmith offers a variety of built-in evaluators, but also provides the flexibility to create custom ones.

Key evaluator types:

  • Reference-based Evaluators: Compare your application's output directly against a 'ground truth' reference answer in your dataset. Examples include ExactMatchEvaluator or EmbeddingDistanceEvaluator.
  • Model-based Evaluators: Utilize another LLM (often a powerful one like GPT-4) to act as a 'critique model'. This model assesses your application's output based on predefined criteria (e.g., helpfulness, correctness, safety) against the input or context. This is incredibly powerful for tasks where a perfect reference answer is hard to define.
  • Custom Evaluators: For highly specific needs, you can write your own Python evaluators to implement custom logic and metrics. This allows integration with external tools or proprietary scoring systems.

When you kick off an evaluation, LangSmith systematically runs your chosen application against every example in the dataset, applies the configured evaluators to each output, and aggregates the results, providing a comprehensive report of your application's performance.

8. Analyzing Evaluation Results and Iterating

Running evaluations is only half the battle; the real value comes from interpreting the results and using them to drive improvements. LangSmith provides a rich interface for analyzing evaluation outcomes.

Upon completion, you'll see a summary dashboard displaying aggregate metrics (e.g., average correctness score, pass rates for different criteria) across your dataset. You can then drill down into individual examples to understand specific failures. Key analysis features include:

  • Metric Aggregations: View overall scores and distributions for each evaluation metric.
  • Filtering and Sorting: Easily identify poor-performing runs, specific types of errors, or edge cases.
  • Side-by-Side Comparison: Compare different versions of your application (e.g., different prompt templates, different models) against the same dataset to see which performs better on which metrics.
  • Trace Inspection: For any failed or poorly rated example, you can immediately jump into its detailed trace to diagnose the root cause—was it a bad LLM call, an incorrect tool output, or a faulty prompt?

This iterative process of evaluating, analyzing failures, refining your application (prompts, RAG components, tools), and re-evaluating is the core loop for building high-quality, reliable LLM applications. LangSmith streamlines this entire workflow.

9. Best Practices and Advanced Tips

To maximize your effectiveness with LangSmith, consider these best practices and advanced tips:

  • Consistent Project Naming: Use clear, descriptive project names (e.g., rag-chatbot-v1, qa-agent-experiment-2) to organize your development efforts and easily differentiate between applications or stages.
  • Meaningful Tags: Apply tags to individual runs (prompt-v3, gpt-4-turbo, retriever-faiss) to enable granular filtering and comparison in the LangSmith UI. This is invaluable for A/B testing prompt variations or model changes.
  • Capture All Context: Ensure your LangChain Runnable inputs are well-structured, including all relevant context for debugging. For RAG apps, this means passing the retrieved documents as part of the input to the LLM step, making it visible in the trace.
  • Leverage 'Feedback' for Human Evaluation: Integrate human feedback directly into your application. LangSmith allows you to programmatically log feedback (e.g., 'helpful', 'not helpful') to individual runs, enriching your evaluation data and enabling human-in-the-loop improvements.
  • Automate Dataset Creation: Whenever possible, automate the creation or updating of your evaluation datasets from production traffic or user interactions. This ensures your evaluations remain relevant to real-world usage.
  • Custom Evaluator for Nuance: Don't shy away from writing custom evaluators for highly specific, domain-dependent metrics that built-in or model-based evaluators can't capture accurately. This provides the most precise feedback for your application's unique goals.

Check your understanding

The lesson ends with a 5-question quiz. Take it in the player above to see your score.

  1. What is the primary benefit of using LangSmith for tracing LLM applications?
    • It provides real-time visibility into the execution path and intermediate steps of LLM chains.
    • It automatically optimizes LLM prompts for better performance.
    • It reduces the cost of LLM API calls by caching responses.
    • It compiles LLM code into a more efficient binary format.
  2. Which environment variable is essential to enable LangSmith tracing for LangChain applications (version 2)?
    • LANGCHAIN_DEBUG=true
    • LANGSMITH_TRACING_ON=1
    • LANGCHAIN_TRACING_V2='true'
    • LLM_OBSERVABILITY_MODE=verbose
  3. Why is evaluating LLM applications more challenging than traditional software?
    • LLM applications can only be tested manually, not with automated tools.
    • The non-deterministic and subjective nature of LLM outputs makes objective assessment difficult.
    • LLM applications cannot be deployed without a comprehensive evaluation suite.
    • There are no established metrics for measuring LLM performance.
  4. What is the purpose of a 'dataset' in LangSmith's evaluation workflow?
    • To store all the LLM models used in a project.
    • A collection of input-output examples, often with ground truth, used as a benchmark for evaluation.
    • A log of all past evaluation runs and their results.
    • A list of all users who have interacted with the LLM application.
  5. Which type of LangSmith evaluator uses another LLM to assess an application's output based on predefined criteria?
    • Reference-based Evaluator
    • Rule-based Evaluator
    • Manual Evaluator
    • Model-based Evaluator

Related lessons

AI
intermediate

LLM Observability with OpenTelemetry: GenAI Semantic Conventions

Master the OTel GenAI semantic conventions — gen_ai.* attributes, span structure for prompts/completions/tools, sampling strategies, and cost attribution — and understand why standardizing across LangSmith, Phoenix, Datadog, and Grafana matters for production AI systems.

13 steps·~20 min·audio
AI
intermediate

RAG Evaluation in Production: Metrics, Tools, and Cadence

Learn how to systematically evaluate Retrieval-Augmented Generation systems in production using RAGAS, TruLens, and Phoenix — covering golden sets, retrieval drift, embedding drift, and cost-aware eval scheduling.

12 steps·~18 min·audio
AI
advanced

Loop engineering: verification, orchestration, and anti-patterns

Make loops trustworthy. Adversarial verification panels, sub-agent orchestration, loop-until-dry for unbounded discovery, loop-until-budget for paid depth, multi-modal sweeps with diverse prompts, eval-driven cap selection, and the five anti-patterns — silent caps, infinite plans, drift, premature termination, thrashing — that ship to prod more than they should.

9 steps·~14 min
AI
intermediate

Loop engineering in production: state, errors, and budgets

Once an agent loop runs past a few iterations the failure surface shifts: state bloats, tool calls fail in three distinct ways, retries multiply if you stack them wrong, and unbounded budgets rack up four-figure bills overnight. The state, errors, and budgets you need to make the loop survive contact with reality.

8 steps·~12 min