How to Use LangSmith: A Completer Tutorial

This article teaches you how to use Langsmith, the LLM development platform from the LangChain team!

1000+ Pre-built AI Apps for Any Use Case

How to Use LangSmith: A Completer Tutorial

Start for free
Contents

LangSmith is a powerful platform designed to help developers build, test, and optimize large language model (LLM) applications. It provides a suite of tools for debugging, evaluating, and monitoring LLM-powered systems. In this comprehensive guide, we'll explore how to use LangSmith effectively, covering everything from setup to advanced features.

💡
Want to try out Claude 3.5 Sonnet without Restrictions?

Searching for an AI Platform that gives you access to any AI Model with an All-in-One price tag?

Then, You cannot miss out Anakin AI!

Anakin AI is an all-in-one platform for all your workflow automation, create powerful AI App with an easy-to-use No Code App Builder, with Llama 3, Claude, GPT-4, Uncensored LLMs, Stable Diffusion...

Build Your Dream AI App within minutes, not weeks with Anakin AI!

What is LangSmith?

LangSmith is a comprehensive platform designed to streamline the development, testing, and deployment of Large Language Model (LLM) applications. It offers a suite of tools and features to help developers at every stage of the LLM application lifecycle.

Key aspects of LangSmith include:

Unified DevOps Platform: LangSmith provides a centralized environment for developing, collaborating, testing, deploying, and monitoring LLM applications.

Framework Agnostic: While it integrates seamlessly with LangChain, LangSmith can be used independently with any LLM framework.

Tracing and Debugging:

  • Allows developers to log and visualize the execution of LLM applications
  • Provides detailed insights into model inputs, outputs, and intermediate steps

Testing and Evaluation:

  • Supports creation and management of datasets for testing
  • Offers built-in and custom evaluators to assess model performance
  • Enables systematic prompt optimization

Monitoring:

  • Tracks system-level performance metrics like latency and cost
  • Allows for real-time monitoring of production applications

Collaboration Tools:

  • Facilitates team collaboration on LLM projects
  • Provides version control for prompts and models

Integration Capabilities:

  • Easy to integrate with existing LLM applications
  • Supports various programming languages, including Python and TypeScript

By offering these features, LangSmith aims to bridge the gap between prototype and production, enabling developers to build, test, and deploy high-quality LLM applications with confidence.

Setting Up LangSmith

Before diving into LangSmith's features, you need to set up your environment and connect to the platform.

Creating a LangSmith Account

To get started with LangSmith:

  1. Visit the LangSmith website (https://smith.langchain.com/)
  2. Sign up using your GitHub account, Discord account, or email address
  3. If using email, verify your account by clicking the link sent to your inbox

Obtaining API Keys for LangSmith

Once your account is set up:

  1. Log in to your LangSmith account
  2. Navigate to the Settings page
  3. Click on "Create API Key"
  4. Copy and securely store the generated API key

Configuring Your Environment for LangSmith

To use LangSmith in your projects, set up your environment variables:

export LANGCHAIN_TRACING_V2=true
export LANGCHAIN_ENDPOINT="https://api.smith.langchain.com"
export LANGCHAIN_API_KEY="your-api-key-here"
export LANGCHAIN_PROJECT="your-project-name"  # Optional, defaults to "default"

Replace "your-api-key-here" with your actual API key and set your desired project name.

Integrating LangSmith with Your Code

Now that your environment is set up, let's explore how to integrate LangSmith into your LLM applications.

Basic LangSmith Tracing

LangSmith's tracing feature allows you to log and visualize the execution of your LLM applications. This is crucial for understanding how your application behaves and identifying potential issues. Here's a simple example:

from langchain.llms import OpenAI
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain

# Initialize the language model
llm = OpenAI(temperature=0.7)

# Create a prompt template
prompt = PromptTemplate(
    input_variables=["topic"],
    template="Write a short paragraph about {topic}."
)

# Create an LLMChain
chain = LLMChain(llm=llm, prompt=prompt)

# Run the chain
result = chain.run("artificial intelligence")

print(result)

With the environment variables set, this code will automatically log the execution to LangSmith. You can then view the trace in the LangSmith UI, which will show you the exact inputs, outputs, and intermediate steps of your LLM chain.

Using LangSmith's Traceable Decorator

For more granular control over tracing, you can use the @traceable decorator. This allows you to trace specific functions and customize the information logged to LangSmith:

from langsmith import traceable

@traceable
def generate_text(topic: str) -> str:
    llm = OpenAI(temperature=0.7)
    prompt = f"Write a short paragraph about {topic}."
    return llm(prompt)

result = generate_text("machine learning")
print(result)

This approach is particularly useful when you want to trace only certain parts of your application or when you're working with custom functions that aren't part of a standard LangChain component.

Creating and Managing Datasets in LangSmith

Datasets are crucial for testing and evaluating your LLM applications. LangSmith provides tools to create and manage datasets effectively.

Creating a Dataset in LangSmith

Here's how to create a dataset programmatically:

from langsmith import Client

client = Client()

dataset = client.create_dataset(
    "AI Topics",
    description="A collection of AI-related topics for testing"
)

examples = [
    {"input": {"topic": "neural networks"}, "output": "A brief explanation of neural networks."},
    {"input": {"topic": "natural language processing"}, "output": "An overview of NLP techniques."},
    {"input": {"topic": "computer vision"}, "output": "Key concepts in computer vision."}
]

client.create_examples(examples, dataset_id=dataset.id)

This code creates a dataset named "AI Topics" and populates it with example inputs and outputs. These examples can be used to test your LLM applications and evaluate their performance.

Using LangSmith Datasets for Evaluation

Once you have a dataset, you can use it to evaluate your LLM applications:

from langsmith import RunEvalConfig, run_on_dataset

eval_config = RunEvalConfig(
    evaluators=[
        "criteria",
        "embedding_distance",
    ]
)

results = run_on_dataset(
    dataset_name="AI Topics",
    llm_or_chain_factory=generate_text,
    evaluation=eval_config,
)

print(results.metrics)

This code runs your generate_text function on the "AI Topics" dataset and evaluates the results using predefined criteria and embedding distance metrics. The results.metrics will provide you with quantitative measures of your application's performance.

Debugging with LangSmith

LangSmith provides powerful debugging tools to help you understand and improve your LLM applications.

Visualizing Traces in LangSmith

After running your LLM application with tracing enabled, you can visualize the execution in the LangSmith UI:

  1. Log in to your LangSmith account
  2. Navigate to the "Traces" section
  3. Select your project and the specific run you want to analyze
  4. Explore the detailed execution graph, inputs, outputs, and metadata

This visualization allows you to see exactly how your LLM application processes information, which can be invaluable for identifying bottlenecks or unexpected behaviors.

Using LangSmith's Playground for Debugging

LangSmith's Playground feature allows you to interactively debug and optimize your prompts:

  1. In the LangSmith UI, go to the "Playground" section
  2. Select or create a new prompt template
  3. Enter sample inputs and run the prompt
  4. Analyze the outputs and adjust your prompt as needed
  5. Save and version your optimized prompts

The Playground is particularly useful for iterating on prompt designs and testing how small changes affect your LLM's outputs.

Advanced LangSmith Features

Let's explore some of LangSmith's more advanced capabilities for improving your LLM applications.

Implementing Custom Evaluators in LangSmith

While LangSmith provides built-in evaluators, you can also create custom ones to suit your specific needs:

from langsmith import RunEvalConfig

def custom_evaluator(run, example):
    # Implement your custom evaluation logic here
    score = # ... calculate score based on run and example
    return {"custom_metric": score}

eval_config = RunEvalConfig(
    evaluators=[
        "criteria",
        custom_evaluator,
    ]
)

results = run_on_dataset(
    dataset_name="AI Topics",
    llm_or_chain_factory=generate_text,
    evaluation=eval_config,
)

This allows you to incorporate domain-specific evaluation criteria into your LangSmith workflow. For example, you might create a custom evaluator that checks for specific keywords, assesses the sentiment of the output, or verifies that certain facts are present in the generated text.

Continuous Monitoring with LangSmith

LangSmith enables continuous monitoring of your LLM applications in production:

  1. Implement tracing in your production code using the methods described earlier
  2. Set up alerts in the LangSmith UI:
  • Go to the "Monitoring" section
  • Create new alert rules based on metrics like error rates, latency, or custom criteria
  1. Configure notification channels (e.g., email, Slack) for alerts

This continuous monitoring allows you to quickly identify and respond to issues in your production LLM applications, ensuring high availability and performance.

Optimizing Prompts with LangSmith

LangSmith's prompt optimization features help you iteratively improve your prompts:

  1. Create a dataset of inputs and desired outputs
  2. Implement multiple prompt versions using the Playground
  3. Run evaluations on each prompt version using the dataset
  4. Analyze the results to identify the best-performing prompt
  5. Iterate and refine based on the insights gained

This process allows you to systematically improve your prompts, leading to better performance and more consistent outputs from your LLM applications.

Best Practices for Using LangSmith

To get the most out of LangSmith, consider these best practices:

Organizing Projects in LangSmith

  • Create separate projects for different applications or components
  • Use consistent naming conventions for traces, datasets, and evaluations
  • Leverage tags and metadata to categorize and filter your data effectively

By organizing your work in LangSmith, you can more easily manage complex LLM applications and collaborate with team members.

Effective Dataset Management in LangSmith

  • Regularly update and expand your datasets to cover new use cases
  • Use a mix of real-world examples and edge cases in your datasets
  • Version your datasets to track changes over time

Well-managed datasets are crucial for effective evaluation and continuous improvement of your LLM applications.

Optimizing LangSmith Performance

  • Use batch processing for large-scale evaluations
  • Implement caching mechanisms to reduce redundant API calls
  • Leverage LangSmith's asynchronous APIs for improved performance in high-throughput scenarios

These optimizations can significantly improve the efficiency of your LangSmith workflows, especially when working with large datasets or complex LLM applications.

Troubleshooting Common LangSmith Issues

When using LangSmith, you might encounter some common issues. Here's how to address them:

Resolving Authentication Problems in LangSmith

If you're having trouble authenticating:

  1. Double-check your API key in the environment variables
  2. Ensure your account has the necessary permissions
  3. Verify that your API key hasn't expired or been revoked

Handling Rate Limits in LangSmith

To avoid hitting rate limits:

  1. Implement exponential backoff in your API requests
  2. Use batch processing for large-scale operations
  3. Contact LangSmith support to discuss increasing your rate limits if needed

Debugging Tracing Issues in LangSmith

If traces aren't appearing in the LangSmith UI:

  1. Confirm that LANGCHAIN_TRACING_V2 is set to true
  2. Check that your code is properly decorated with @traceable or using LangChain's tracing-enabled classes
  3. Verify your network connection and firewall settings

Conclusion

LangSmith is a powerful platform that can significantly enhance your LLM application development process. By leveraging its tracing, evaluation, and monitoring capabilities, you can build more robust, efficient, and effective AI-powered systems. As you continue to use LangSmith, experiment with its various features and integrate it deeply into your development workflow to unlock its full potential.

Remember that LangSmith is continuously evolving, so stay updated with the latest features and best practices. Engage with the LangSmith community, participate in forums, and don't hesitate to reach out to LangSmith support for assistance. With practice and exploration, you'll be able to harness the full power of LangSmith to create cutting-edge LLM applications that meet and exceed your users' expectations.

💡
Want to try out Claude 3.5 Sonnet without Restrictions?

Searching for an AI Platform that gives you access to any AI Model with an All-in-One price tag?

Then, You cannot miss out Anakin AI!

Anakin AI is an all-in-one platform for all your workflow automation, create powerful AI App with an easy-to-use No Code App Builder, with Llama 3, Claude, GPT-4, Uncensored LLMs, Stable Diffusion...

Build Your Dream AI App within minutes, not weeks with Anakin AI!