Skip to main content

Someone wanted to learn this too, so Grasp built them a personal learning path.

You can keep exploring this one or create your own

Monitoring LLMs with LLMOps Platforms

Welcome to the first lesson of our module on production hardening. In our previous module, we constructed a sophisticated, asynchronous architecture for our AI agents. This system is scalable and resilient, but as with any production system, once it's running, a new set of critical questions emerges: Is it working correctly? Is it fast enough? How much is it costing us? And are users getting good results? Answering these questions is the domain of LLMOps, and today we'll focus on one of its most vital pillars: observability.

This lesson will guide you through integrating an LLMOps platform to gain deep visibility into your application. We'll start by defining what observability means in the context of LLM applications, explore the landscape of available tools, and then perform a hands-on integration with Langfuse, a powerful open-source platform. By the end of this hour, you will have instrumented an application to monitor its token usage, latency, and costs, and you will understand how to collect user feedback.

From Black Box to Glass Box: The Need for Observability

So far, we've focused on building the "engine" of our AI application. Now, we need to build the "dashboard." In traditional software engineering, we monitor things like CPU usage, memory, and database query times. For LLM applications, we need to track all of that, plus a new set of metrics unique to this domain.

The entire process, from a user's request to the final response, involves multiple steps, each with its own potential for failure, latency, or high cost.

This diagram shows a complete LLMOps pipeline. Our focus today is on the "Observability & Logging" stages, which are crucial for understanding and improving every other part of the system.

This is where the concepts of monitoring and observability become essential. An excellent article from Towards Data Science clarifies these terms and their importance for LLM applications.

LLM Monitoring and Observability: Hands-on with Langfuse

This article provides a solid foundation for understanding why we need to add instrumentation to our LLM applications.

First, read the section What is Monitoring and Observability? to understand the distinction between these two concepts. Monitoring tells you that something is wrong (e.g., high latency), while observability helps you understand why. Next, read through the key terminology, from Telemetry to Metrics. Pay close attention to the definitions of Trace, Span, and Generation, as these are the fundamental building blocks of LLM observability. Finally, review the section Why is LLM Monitoring and Observability Necessary? to solidify your understanding of the core business and technical drivers.

As the article highlights, we need observability to debug complex chains, manage API costs, improve user experience, and ensure our application is reliable. We achieve this by collecting telemetry data through a process called instrumentation.

The LLMOps Tooling Landscape

There are two primary architectural approaches for integrating observability tools into your application: AI Gateways and SDK-based Instrumentation.

1. AI Gateways (e.g., Helicone)

A gateway acts as a proxy, sitting between your application and the various LLM providers. All your API requests are routed through the gateway, which logs the request and response data before forwarding it to the target LLM (like OpenAI or Mistral).

This diagram illustrates the gateway pattern. Your app, using a standard OpenAI SDK, simply changes the API endpoint to point to the Helicone gateway. The gateway handles routing, caching, and logging to its observability platform.
  • Pros: Very easy to set up (often just changing a single URL). Can provide features like unified API keys, request retries, caching, and fallbacks across different providers.
  • Cons: Introduces an external dependency and a potential single point of failure. It might add a small amount of latency. You have less granular control over what gets logged compared to an SDK.

2. SDK-based Instrumentation (e.g., Langfuse, LangSmith)

This approach involves integrating a library (an SDK) directly into your application code. You use decorators, callbacks, or context managers to "wrap" the parts of your code you want to trace, such as LLM calls, function calls, or entire agentic chains.

  • Pros: Offers maximum flexibility and granular control. You can trace not just LLM calls, but any arbitrary function or block of code, providing a much richer view of your application's internal state. Since it's integrated into your code, it doesn't introduce an external network hop (though it does send data asynchronously in the background).
  • Cons: Requires code modifications. The setup is slightly more involved than using a gateway.

For our purposes, we will focus on the SDK-based approach with Langfuse. As an engineer, you'll appreciate the control and depth it provides, and its open-source nature means you can self-host it for complete data privacy—a critical consideration for many production systems.

A Deep Dive into Langfuse

Langfuse is a comprehensive, open-source LLM engineering platform. While it has features for prompt management and evaluation, its core strength lies in its observability and tracing capabilities. Cole Medin's video gives an excellent high-level pitch for why a tool like this is a "secret weapon" for building serious AI agents.

If You’re Serious About Building AI Agents, This is Your Secret Weapon

This video frames Langfuse not just as a tool, but as a critical capability for moving from prototypes to production-ready agents.

Watch the first few minutes, from the introduction to observability to understand the "why". Then, review the overview of the platform, which highlights its key features and open-source nature. Finally, watch the live demo walkthrough. This will give you a concrete feel for the Langfuse UI and the rich, detailed information you can get from a single trace, including costs, latency, token counts, and the full sequence of tool calls and generations.

Setting Up Your Langfuse Instance

To get started, you need a Langfuse server to send your data to. Langfuse can be self-hosted using Docker, which is perfect for our needs. The Towards Data Science article we looked at earlier provides a step-by-step guide.

LLM Monitoring and Observability: Hands-on with Langfuse

This part of the article will guide you through setting up a local Langfuse environment.

First, read the brief introduction to Langfuse to understand its components. Then, follow the instructions in the section "Setting Up a Langfuse Server". The docker-compose method is the most straightforward. You'll simply clone a repository and run docker compose up. Once the server is running, follow the steps in Configuring Langfuse Dashboard to create your first project and get your API keys. Make sure to save your Public Key and Secret Key.

Integrating the Langfuse SDK

With the server running and your keys in hand, it's time to instrument an application. The easiest way to start is by using Langfuse's drop-in replacement for the OpenAI SDK. This requires minimal code changes.

Let's take a simple Python script that makes a call to OpenAI.

Original Code:

import os
import openai
from dotenv import load_dotenv

load_dotenv()




# Note: The 'openai' library now uses the 'OPENAI_API_KEY' environment variable by default.
# The client can be initialized without explicitly passing the api_key.
client = openai.OpenAI()

response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "What is the key benefit of LLM observability?"}
    ]
)

print(response.choices[0].message.content)

Now, let's integrate Langfuse.

Instrumented Code:
First, install the Langfuse library:
pip install langfuse openai --upgrade

Then, modify the code:

import os



# The original 'import openai' is replaced with the line below
from langfuse.openai import openai
from dotenv import load_dotenv

load_dotenv()




# Set Langfuse credentials from environment variables
# LANGFUSE_SECRET_KEY="sk-lf-..."
# LANGFUSE_PUBLIC_KEY="pk-lf-..."
# LANGFUSE_HOST="http://localhost:3000"




# The Langfuse wrapper automatically reads these environment variables
client = openai.OpenAI()

response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[
        {"role": "system", "content": "What is the key benefit of LLM observability?"}
    ],



    # You can add metadata to track users or sessions
    user="user-12345",
    session="session-abcde"
)

print(response.choices[0].message.content)




# In short-lived scripts, it's important to flush events before exiting
client.flush()

The changes are minimal:

  1. You change import openai to from langfuse.openai import openai.
  2. You set your Langfuse credentials as environment variables (LANGFUSE_SECRET_KEY, LANGFUSE_PUBLIC_KEY, LANGFUSE_HOST).
  3. You can pass user and session IDs directly into the create call to automatically group your traces.
  4. You call client.flush() at the end to ensure all telemetry data is sent before the script exits.

When you run this code, the request will execute as normal, but in the background, a detailed trace will be sent to your local Langfuse dashboard. You will see the prompt, the response, the token counts, the latency, and the calculated cost, all associated with the user and session you specified.

Given your background in JavaScript, it's worth noting that the process is conceptually identical. You wrap your OpenAI client and can then track requests automatically.

Langfuse: Free Open Source LLM Engineering Platform

This video shows the same integration pattern but for the JavaScript/TypeScript SDK.

Watch the segment from this point. Notice how the developer wraps the OpenAI client with observeOpenAI and then calls flushAsync. The core pattern of wrapping the client and flushing events is universal across languages.

Capturing User Feedback and Sessions

Simple traces are powerful, but the real magic happens when you start organizing them and collecting feedback. Langfuse allows you to group traces into sessions (representing a single conversation) and to score traces based on quality or user feedback.

Langfuse: Free Open Source LLM Engineering Platform

This segment demonstrates how to use session IDs and add scores for evaluation.

Please watch from this section. You'll see how passing a session_id groups multiple traces together. More importantly, you'll see how you can configure custom scores (e.g., a boolean for 'error' or a 1-10 'quality' rating) and apply them to traces. This is how you fulfill the "monitor user feedback" part of our learning outcome—either by manual annotation in the UI or by programmatically sending scores via the API.

By scoring traces, you can build dashboards that track quality over time, identify underperforming parts of your application, and create datasets for fine-tuning or regression testing.

Conclusion

You have now taken a critical step towards building production-grade AI systems. An application without observability is a black box; by integrating a platform like Langfuse, you have turned it into a glass box, allowing you to see exactly what's happening inside. This visibility is not just a debugging tool—it's the foundation for optimization, cost management, and continuous improvement.

Key Takeaways:

  • Observability is Essential: For any serious LLM application, you must be able to track performance, cost, and quality.
  • Traces are the Core Unit: An end-to-end trace, composed of spans and generations, provides a detailed execution path for every user request.
  • Gateways vs. SDKs: Gateways offer simplicity, while SDKs provide granular control. Langfuse is a powerful, open-source SDK-based platform.
  • Integration is Straightforward: With tools like Langfuse, you can start monitoring your application with minimal code changes, often just by swapping an import and setting environment variables.
  • Monitoring Goes Beyond Cost and Latency: Grouping traces by session and scoring them for quality (user feedback) unlocks deeper insights into your application's performance.

In our next lesson, we will address another critical aspect of production hardening: security. Now that we can monitor our application's inputs and outputs, we'll learn how to defend it against common attacks like prompt injection and jailbreaking.

Can't find a good explanation? Sign up and we'll make it for you

Sign up