Skip to main content

Observability and tracing for Retrieval-Augmented Generation pipelines

Project description

RAGTrace ๐Ÿ“Š

Observability for RAG pipelines - Trace, inspect, and optimize Retrieval-Augmented Generation systems with ease.

Python 3.11+ License: MIT Code style: black

โœจ What is RAGTrace?

RAGTrace is a lightweight observability layer for RAG (Retrieval-Augmented Generation) systems that captures and visualizes every step of your pipeline:

  • ๐Ÿ” Event Capture - Automatically intercepts retrieval, prompt, and generation events
  • ๐Ÿ’ฐ Cost Tracking - Accurate token counting and cost estimation per query
  • ๐Ÿ“Š Interactive Web UI - Modern timeline view with charts, filters, and event inspection
  • ๐Ÿ”ง CLI Tool - Developer-friendly command-line interface
  • ๐ŸŒ REST API - Query and analyze sessions programmatically
  • ๐Ÿงช Regression Testing - Snapshot and compare RAG outputs

Think of it as OpenTelemetry, but specifically for RAG pipelines.

๐Ÿš€ Quick Start

Installation

# Clone the repository
git clone https://github.com/yourusername/ragtrace.git
cd ragtrace

# Install dependencies (using pip)
pip install -e .

# Or using Poetry
poetry install
poetry shell

# Initialize database
ragtrace init

Basic Usage

from langchain.vectorstores import FAISS
from langchain.embeddings import OpenAIEmbeddings
from langchain.chat_models import ChatOpenAI
from langchain.chains import RetrievalQA
from ragtrace import RagTracer

# Your existing RAG setup
embeddings = OpenAIEmbeddings()
vectorstore = FAISS.from_texts(["Your documents here..."], embeddings)
llm = ChatOpenAI(model="gpt-3.5-turbo")

# Add RAGTrace - just one line!
tracer = RagTracer(auto_save=True)

# Create chain with tracer
chain = RetrievalQA.from_chain_type(
    llm=llm,
    retriever=vectorstore.as_retriever(),
    callbacks=[tracer]  # โ† Automatic capture!
)

# Run your query (automatically captured)
result = chain.run("What is RAG?")

View Results

# View latest session in CLI
ragtrace show last

# List all sessions
ragtrace list

# Export to JSON
ragtrace export <session-id> > session.json

# Start API server (for Web UI)
ragtrace run

# Or start both servers for full Web UI experience
uvicorn api.main:app --port 8000 &  # API server
python ui/serve.py                   # UI server โ†’ http://localhost:3000

๐ŸŒ Web UI

RAGTrace includes a modern web interface for visualizing and analyzing your RAG pipelines:

Start the Servers

# Terminal 1: Start API server
cd ragtrace
source venv/bin/activate
uvicorn api.main:app --host 0.0.0.0 --port 8000 --reload

# Terminal 2: Start UI server
cd ragtrace
python ui/serve.py

Then open http://localhost:3000 in your browser.

Web UI Features

  • ๐Ÿ“‹ Sessions View - Browse all captured RAG sessions with search and filtering
  • ๐Ÿ“Š Timeline View - Interactive timeline showing retrieval โ†’ prompt โ†’ generation flow
  • ๐Ÿ“ˆ Performance Charts - Waterfall chart for event durations, cost breakdown by component
  • ๐Ÿ” Event Inspector - Click any event to see full details including tokens, costs, and data
  • ๐ŸŽฏ Smart Filters - Filter events by type, duration, and cost
  • ๐Ÿ“ค Export Tools - Export session data as JSON or CSV, copy to clipboard

Quick UI Guide

  1. Sessions Tab: View all RAG sessions, sorted by recent activity
  2. Timeline Tab: Click on any session to see detailed event timeline
  3. Click Events: Click timeline events to inspect full details
  4. Apply Filters: Use dropdowns to filter by event type or performance metrics
  5. Export Data: Use export buttons to download session data

๐Ÿ“Š Example Output

โ•ญโ”€ Session: d4f3a8b2-... โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
โ”‚ Query: What is RAG?                                      โ”‚
โ”‚ Model: gpt-3.5-turbo                                     โ”‚
โ”‚ Cost: $0.00360                                           โ”‚
โ”‚ Duration: 1,850ms                                        โ”‚
โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ

โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”ณโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”ณโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”“
โ”ƒ Event       โ”ƒ Duration   โ”ƒ Cost       โ”ƒ
โ”กโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ•‡โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ•‡โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”ฉ
โ”‚ Retrieval   โ”‚ 150ms      โ”‚ $0.00001   โ”‚
โ”‚ Prompt      โ”‚ 0ms        โ”‚ $0.00000   โ”‚
โ”‚ Generation  โ”‚ 1,700ms    โ”‚ $0.00359   โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐ŸŽฏ Features

โœ… Core Features

  • Automatic Event Capture - Works with LangChain callbacks
  • Cost Tracking - Uses tiktoken for accurate token counting
  • Timeline Visualization - See your RAG pipeline in action
  • Session Management - Store and retrieve debugging sessions
  • CLI Tool - Rich formatted terminal output
  • REST API - FastAPI server with OpenAPI docs
  • Web UI - Interactive timeline with charts and event inspection
  • JSON/CSV Export - Export sessions for analysis
  • Snapshot Testing - Save and compare pipeline outputs

๐ŸŽจ Web UI Features

  • Sessions Dashboard - View all sessions with search and sort
  • Interactive Timeline - Click events to inspect details
  • Performance Waterfall - Visualize event durations side-by-side
  • Cost Breakdown Chart - See spending by component (embeddings, prompts, generation)
  • Smart Filters - Filter by event type, duration, or cost
  • Real-time Updates - WebSocket support for live session monitoring
  • Export Tools - Download as JSON, CSV, or copy to clipboard

๐ŸŽจ CLI Commands

ragtrace init              # Initialize database
ragtrace list              # List recent sessions
ragtrace show [id]         # View session details
ragtrace show last         # View latest  session
ragtrace export <id>       # Export to JSON
ragtrace clear             # Clear all data
ragtrace snapshot save     # Save snapshot
ragtrace snapshot list     # List snapshots
ragtrace run               # Start API server

๐ŸŒ API Endpoints

POST   /api/sessions                      # Create session
GET    /api/sessions                      # List sessions
GET    /api/sessions/{id}                 # Get session
PATCH  /api/sessions/{id}                 # Update session
DELETE /api/sessions/{id}                 # Delete session
POST   /api/sessions/{id}/events          # Add event
GET    /api/sessions/{id}/costs           # Get costs
POST   /api/snapshots                     # Create snapshot
GET    /api/snapshots                     # List snapshots
GET    /api/snapshots/{id1}/compare/{id2} # Compare snapshots

Visit http://localhost:8000/docs after running ragtrace run for interactive API documentation.

๐Ÿ—๏ธ Architecture

ragtrace/
โ”œโ”€โ”€ core/              # Core business logic
โ”‚   โ”œโ”€โ”€ models.py      # Pydantic data models
โ”‚   โ”œโ”€โ”€ storage.py     # SQLite database layer
โ”‚   โ”œโ”€โ”€ cost.py        # Token counting & cost calculation
โ”‚   โ””โ”€โ”€ capture.py     # Event aggregation
โ”œโ”€โ”€ langchain/         # LangChain integration
โ”‚   โ””โ”€โ”€ middleware.py  # Callback handler
โ”œโ”€โ”€ api/               # REST API (FastAPI)
โ”‚   โ”œโ”€โ”€ main.py        # FastAPI application
โ”‚   โ””โ”€โ”€ routes.py      # API endpoints
โ”œโ”€โ”€ ui/                # Web UI (HTML/CSS/JS)
โ”‚   โ”œโ”€โ”€ index.html     # Main UI page
โ”‚   โ”œโ”€โ”€ app.js         # Frontend application logic
โ”‚   โ”œโ”€โ”€ styles.css     # UI styling
โ”‚   โ””โ”€โ”€ serve.py       # Development server
โ”œโ”€โ”€ cli/               # Command-line interface
โ”‚   โ””โ”€โ”€ main.py        # Click CLI commands
โ”œโ”€โ”€ examples/          # Usage examples
โ”‚   โ””โ”€โ”€ simple_rag.py  # Complete working example
โ””โ”€โ”€ tests/             # Test suite
    โ”œโ”€โ”€ test_cost.py   # Cost calculation tests
    โ”œโ”€โ”€ test_storage.py # Database tests
    โ””โ”€โ”€ test_capture.py # Capture logic tests

๐Ÿ“‹ Requirements

  • Python: 3.11+ (3.12 recommended)
  • Dependencies: FastAPI, LangChain, tiktoken, Rich, Click
  • OpenAI API Key: Required for examples (not for core functionality)

๐Ÿ› ๏ธ Development

Setup Development Environment

# Clone repository
git clone https://github.com/yourusername/ragtrace.git
cd ragtrace

# Install with development dependencies
poetry install
poetry shell

# Run tests
pytest

# Run with coverage
pytest --cov=core --cov=langchain --cov=api --cov=cli

Running Tests

# All tests
pytest

# Specific test file
pytest tests/test_cost.py -v

# With coverage report
pytest --cov=core tests/

Code Quality

# Format code
black .

# Lint
ruff check .

# Type checking
mypy .

๐Ÿ“– Examples

Check out the examples/ directory for complete working examples:

  • simple_rag.py - Basic RAG pipeline with debugger
  • with_sources.py - RAG with source tracking (coming soon)

๐Ÿ”ฌ Use Cases

1. Debug Failed Queries

# See exactly why your RAG pipeline failed
ragtrace show last

2. Track Costs

# Monitor spending per query
ragtrace list --sort-by cost

3. Identify Retrieval Issues

# Check which documents were retrieved
session = tracer.get_latest_session()
print(session.retrieval_event.chunks)

4. Regression Testing

# Save baseline
ragtrace snapshot save "v1-baseline"

# Compare after changes
ragtrace snapshot compare <snapshot-id-1> <snapshot-id-2>

๐Ÿค Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

๐Ÿ“ License

This project is licensed under the MIT License - see the LICENSE file for details.

๐Ÿ™ Acknowledgments

๐Ÿ“ž Support

  • Issues: GitHub Issues
  • Documentation: See examples/ directory
  • API Docs: Run ragtrace run and visit http://localhost:8000/docs

๐Ÿ—บ๏ธ Roadmap

v0.2.0 (Current)

  • Web UI for timeline visualization
  • Interactive event inspection
  • Performance charts (waterfall, cost breakdown)
  • Export tools (JSON, CSV)
  • Advanced regression testing
  • LlamaIndex integration
  • Prompt versioning

v0.3.0 (Planned)

  • Agent tracing support
  • Cost optimization suggestions
  • Quality scoring
  • Team collaboration features

v1.0.0

  • Cloud mode
  • Advanced analytics
  • Alert system
  • Multi-framework support

โญ Star History

If you find RAGTrace useful, please consider giving it a star! โญ


Built with โค๏ธ for RAG developers

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ragtrace-0.2.0.tar.gz (53.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ragtrace-0.2.0-py3-none-any.whl (59.4 kB view details)

Uploaded Python 3

File details

Details for the file ragtrace-0.2.0.tar.gz.

File metadata

  • Download URL: ragtrace-0.2.0.tar.gz
  • Upload date:
  • Size: 53.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.2

File hashes

Hashes for ragtrace-0.2.0.tar.gz
Algorithm Hash digest
SHA256 a2b2bf8534104c2b7d2eac6161b4a6fecf1d62ae66d91d4d13ff7547359afd9b
MD5 edf4a9b691732a85d882c787f8e509f2
BLAKE2b-256 040adb4aa67e862932910ade691e4ae1a08f1a03743bbd374b0bb8b08dc37433

See more details on using hashes here.

File details

Details for the file ragtrace-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: ragtrace-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 59.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.2

File hashes

Hashes for ragtrace-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 05e2f2d7d25337d5f70c6aa04a23a7427a59b0c40b99963e1cb1ba18c90f8ad4
MD5 8605b7d8666311566b2be523c5fa1034
BLAKE2b-256 bdd6ea4822c6fee50d04c0a944b825c4309263cc3cd135ac170688c4a7b07a91

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page