LangFuse Online Evaluation

Open in Colab Open in GitHub

Overview

This tutorial covers the observation and tracing of LangGraph applications using LangFuse.

LangFuse provides a comprehensive logging, debugging, and evaluation framework for LangChain applications.

In this tutorial, we will explore how to integrate LangFuse into a LangGraph application and monitor its execution.

Table of Contents

References


Environment Setup

Set up the environment. You may refer to Environment Setup for more details.

[Note]

  • langchain-opentutorial is a package that provides a set of easy-to-use environment setup, useful functions and utilities for tutorials.

  • You can checkout the langchain-opentutorial for more details.

You can alternatively set API keys such as OPENAI_API_KEY in a .env file and load them.

[Note] This is not necessary if you've already set the required API keys in previous steps.

Introduction to LangGraph

LangGraph is an advanced framework designed for building dynamic, multi-step AI workflows. It enables developers to create complex, structured execution flows for AI applications.

  • A structured way to build complex workflows

  • State management capabilities

  • Integration with various LLM tools and services

  • Clear visualization of application flow

Basic LangGraph Concepts

  1. Nodes: Individual processing units

  2. Edges: Connections between nodes

  3. State: Data maintained throughout the workflow

  4. Conditional Logic: Decision making within the graph

Introduction to LangFuse

LangFuse is an observability platform for LLM-based applications. It provides structured logs, debugging insights, and evaluation capabilities to improve the performance of AI models.

Key Features

  • Tracing: Tracks execution paths in LangGraph.

  • Logging: Stores and analyzes LLM interactions.

  • Evaluation: Benchmarks AI-generated responses.

Why LangFuse?

  • Provides detailed insights into LLM application behavior

  • Helps identify bottlenecks and optimization opportunities

  • Enables data-driven iteration on prompts and workflows

  • Supports production monitoring and debugging

Online LangFuse Guide

To enable online tracking with LangFuse, follow these steps:

  1. Create an API Key on LangFuse Cloud.

  2. Set Up Environment Variables in your .env file.

  3. Enable Logging and Tracing in your LangGraph application.

The following sections will provide two practical examples of how LangFuse can be used in an AI application.

LangFuse Cloud Pricing

LangFuse offers flexible pricing tiers to accommodate different needs, starting with a free Hobby plan that requires no credit card.

The pricing structure includes:

LangFuse-Cloud-Pricing

Setup and Configuration

  1. LangFuse Cloud Site Access

    • Navigate to the LangFuse Cloud platform to begin the setup process

  2. Create LangFuse Account

    • Sign up for a new account using your email or OAuth providersCreate LangFuse Account

  3. Create New Organization

    • Set up a new organization to manage your projects and team membersCreate New Organization

  4. Member Settings

    • Configure member roles and permissions for your organizationMember Settings

  5. Project Creation

    • Create a new project to start monitoring your LLM applicationsProject Creation

  6. Obtain API Keys

    • Generate and securely store your public and secret API keys for authenticationObtain API Keys

  7. Dashboard Overview

    • Explore the dashboard interface to monitor your application's performance and usageDashboard Overview

Basic Implementation

This basic implementation shows:

  1. Initialize Langfuse

  2. Creating a simple trace

  3. Basic logging and generation recording

View traces in Langfuse

Example trace in Langfuse: https://cloud.langfuse.com/project/cm71ka0zx07yxad079p1kn1bz/traces/c99361dc-fc41-4152-8ef0-eb7507d01b65

Trace view of simple code in Langfuse

Implementation and Example

In this section, we'll look at two examples of using LangFuse.

  1. Basic LangGraph monitoring: Shows simple trace creation and logging of LLM interactions

  2. Tool-using agent: Demonstrates how to track an AI agent's interactions with a search tool

Example 1. Simple chat app with LangGraph

  • Build a support chatbot in LangGraph that can answer common questions

  • Tracing the chatbot's input and output using Langfuse

Create Agent

Start by creating a StateGraph. A StateGraph object defines our chatbot's structure as a state machine.

We will add nodes to represent the LLM and functions the chatbot can call, and edges to specify how the bot transitions between these functions.

Add Langfuse as callback to the invocation

Now, we will add then Langfuse callback handler for LangChain to trace the steps of our application: config={"callbacks": [langfuse_handler]}

View traces in Langfuse

Example trace in Langfuse: https://cloud.langfuse.com/project/cm71ka0zx07yxad079p1kn1bz/traces/4dd6a2f4-353c-457c-afcd-1fc7837cf3ad

Trace view of chat app in Langfuse

Visualize the chat app

You can visualize the graph using the get_graph method along with a "draw" method

Example 2. Tool-using agent with LangGraph

  • Build an agent that can search and reason about information using ReAct framework and Tavily search tool

  • Track the agent's reasoning process and tool usage with Langfuse monitoring

Import and Create the Search Tool

The Tavily Search API tool is designed to facilitate powerful search capabilities within the chatbot. It retrieves comprehensive and reliable search results, making it ideal for answering questions about current events or topics that require external information.

Add the Tool to the Tool List

  • The search tool is added to a list ( tools ). In LangChain, multiple tools can be combined to build more advanced workflows.

Execute the Tool

  • The invoke method is called to execute the search query "U.S. Presidential Inauguration". The search results are returned in JSON format and displayed using the print statement.

  • The results are page summaries that can be used by the chatbot to answer user questions.

Create ReAct Agent

After setting up our search tool, we'll create a ReAct agent using LangGraph's prebuilt functionality.

Execute the Agent

Now we'll run our agent with LangFuse monitoring enabled.

View traces in Langfuse

Example trace in Langfuse: https://cloud.langfuse.com/project/cm71ka0zx07yxad079p1kn1bz/traces/025531e4-137e-4962-839b-3352ec2563c9

Trace view of chat app in Langfuse

Visualize the chat app

You can visualize the graph using the get_graph method along with a "draw" method

Last updated