Skip to main content
This cookbook walks you through adding full observability to a Retrieval-Augmented Generation (RAG) pipeline—tracing every stage from document ingestion to answer generation, tracking costs, and monitoring performance.

Open in Google Colab

Run the complete observability notebook in your browser

What You’ll Learn

1. Build the RAG Pipeline

Create a complete RAG chatbot that loads PDFs, chunks documents, generates embeddings, and retrieves relevant context for answering questions.

2. Add Auto-Instrumentation

Instrument every stage—chunking, embedding, retrieval, and generation—with Netra auto-tracing to capture the full execution flow.

3. Track Costs & Performance

Monitor token usage, API costs, and latency at each step to identify bottlenecks and optimize your pipeline.

4. Add User & Session Tracking

Track usage per user and session to understand conversation flows and user behavior.
Prerequisites:

High-Level Concepts

RAG Architecture

A RAG chatbot works in two phases: Ingestion (one-time):
  1. Load and chunk the PDF into smaller text segments
  2. Generate embeddings for each chunk
  3. Store embeddings in a vector database
Query (per question):
  1. Convert the user’s question to an embedding
  2. Find the most similar chunks (retrieval)
  3. Pass retrieved chunks + question to an LLM
  4. Return the generated answer
RAG Pipeline Architecture

Why Observability Matters for RAG

RAG systems can fail silently in multiple ways:

Creating the Chat Agent

Let’s build the RAG chatbot first, then add tracing.

Installation

Start by installing the required packages. We’ll use OpenAI for embeddings and generation, ChromaDB as our vector store, and a PDF parsing library.

Environment Setup

Configure your API keys. You’ll need both an OpenAI key for the LLM operations and a Netra key for observability.

Loading and Chunking Documents

The first step in any RAG pipeline is extracting text from your documents and splitting it into manageable chunks. We use overlapping chunks to ensure context isn’t lost at chunk boundaries—this helps when relevant information spans multiple segments.

Generating Embeddings and Indexing

Next, we convert each chunk into a vector embedding and store it in ChromaDB. These embeddings capture the semantic meaning of each chunk, allowing us to find relevant content based on meaning rather than just keywords.

Building the Query Pipeline

Now we implement the core RAG logic: given a user question, retrieve the most relevant chunks from our vector store, then pass them as context to the LLM to generate an answer. The top_k parameter controls how many chunks we retrieve—more chunks provide more context but also increase cost and latency.

Adding Session Support

For production use, we wrap everything in a class that maintains conversation history and session state. This enables multi-turn conversations where the chatbot remembers previous exchanges, and allows us to track usage per user and session.

Tracing the Agent

Now let’s add Netra observability to see what’s happening inside the RAG pipeline. The good news: with auto-instrumentation, you get full visibility with minimal code changes.

Initializing Netra

Add these imports and initialization at the very top of your script, before any other code. Auto-instrumentation captures all OpenAI and ChromaDB operations automatically—no decorators or manual spans required.
What gets auto-traced with zero code changes:
  • OpenAI chat completions with model, tokens, cost, and latency
  • OpenAI embeddings with token counts
  • ChromaDB queries and inserts with timing
  • Full prompts and responses (when trace_content=True)

What Gets Auto-Traced

With the initialization above, your existing code from the Creating the Chat agent section is automatically traced. Here’s what appears in your Netra dashboard:

Document Ingestion

The generate_embeddings() call to OpenAI and collection.add() to ChromaDB are captured automatically.
Ingestion trace showing OpenAI embeddings and ChromaDB operations

Auto-traced document ingestion showing embedding generation and vector storage

Retrieval Operations

Query embedding generation and vector search operations appear as child spans with timing and metadata.
Retrieval trace showing embedding and search spans

Auto-traced retrieval showing query embedding and ChromaDB vector search

LLM Generation

OpenAI chat completions are fully traced with model, tokens, cost, latency, and full prompt/response content.
Generation trace showing OpenAI chat completion details

Auto-traced LLM generation showing tokens, cost, and full prompt/response

Adding User and Session Tracking

To analyze usage per user and track conversation flows, add user and session context to your existing PDFChatbot class. This is the one piece that requires explicit code—everything else is auto-traced. Simply add these two lines in your chat method:

What You’ll See in the Dashboard

After running the chatbot, you’ll see traces in the Netra dashboard with:
  • OpenAI spans showing model, tokens, cost, and full prompt/response
  • ChromaDB spans showing query timing and results
  • User and session IDs attached to all spans for filtering

Using Decorators

Auto-instrumentation handles most cases of tracing but if you want to bring in more structure, you can use decorators. Use decorators to create parent spans that group related operations. This is useful when you want to see a single trace for an entire pipeline rather than individual OpenAI/ChromaDB calls.
Decorator traces

Traces with decorators showing hierarchical span structure


Summary

You’ve built a fully observable RAG pipeline with Netra. Your chatbot now has:
  • End-to-end tracing across document ingestion, retrieval, and generation
  • Cost and performance tracking at each pipeline stage
  • User and session tracking for usage analytics
  • Debugging capabilities to trace issues back to specific chunks and prompts
With this foundation, you can identify bottlenecks, optimize costs, and debug issues in your RAG system with confidence.

See Also

Evaluate Your RAG Pipeline

Add quality metrics and test suites to measure retrieval and generation quality

Simulation Testing

Run automated simulation tests to stress-test your pipeline
Last modified on March 17, 2026