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.
โจ 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
- Sessions Tab: View all RAG sessions, sorted by recent activity
- Timeline Tab: Click on any session to see detailed event timeline
- Click Events: Click timeline events to inspect full details
- Apply Filters: Use dropdowns to filter by event type or performance metrics
- 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 debuggerwith_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.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - 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 runand visithttp://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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a2b2bf8534104c2b7d2eac6161b4a6fecf1d62ae66d91d4d13ff7547359afd9b
|
|
| MD5 |
edf4a9b691732a85d882c787f8e509f2
|
|
| BLAKE2b-256 |
040adb4aa67e862932910ade691e4ae1a08f1a03743bbd374b0bb8b08dc37433
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
05e2f2d7d25337d5f70c6aa04a23a7427a59b0c40b99963e1cb1ba18c90f8ad4
|
|
| MD5 |
8605b7d8666311566b2be523c5fa1034
|
|
| BLAKE2b-256 |
bdd6ea4822c6fee50d04c0a944b825c4309263cc3cd135ac170688c4a7b07a91
|