Files
Hermes_rocket.chat_Plugin/docs/TOKEN_TRACKING.md
T

136 lines
4.3 KiB
Markdown

# LLM Token Tracking with Prometheus and Grafana
This document explains how to set up and use the LLM token tracking observability system for the Rocket.Chat Hermes plugin.
## Overview
The token tracking system monitors LLM usage in real-time, tracking:
- Input tokens (prompt tokens) per room
- Output tokens (completion tokens) per room
- Estimated cost per room based on model pricing
- Request count per room
Metrics are exposed via a Prometheus-compatible endpoint and can be visualized in Grafana dashboards.
## Architecture
```
Hermes Agent (with Rocket.Chat plugin)
↓ (API calls with usage data)
Token Tracking Plugin (collects metrics)
↓ (Prometheus format)
Metrics Endpoint (:8000/metrics)
↓ (scraped every 15s)
Prometheus (time-series database)
↓ (query from dashboards)
Grafana (visualization)
```
## Setup Instructions
### 1. Enable the Plugin
The token tracking plugin is automatically enabled when the Rocket.Chat plugin is loaded. Ensure the following environment variable is set:
```bash
export HERMES_PROMETHEUS_PORT=8000 # Optional, defaults to 8000
```
### 2. Start Monitoring Stack
Start the Prometheus and Grafana containers:
```bash
cd /home/bernd/projekte/Hermes_rocket.chat_Plugin
docker-compose -f docker-compose.prometheus.yml up -d
```
### 3. Access Grafana Dashboard
1. Open your browser to http://localhost:3000
2. Log in with default credentials (admin/admin)
3. Navigate to the "LLM Token Tracking" dashboard
## Metrics Reference
The plugin exposes the following Prometheus metrics:
- `room_llm_input_tokens_total{room_id, model, provider}` - Counter of input tokens per room
- `room_llm_output_tokens_total{room_id, model, provider}` - Counter of output tokens per room
- `room_llm_cost_total{room_id, model, provider}` - Counter of estimated cost per room
- `room_llm_requests_total{room_id, model, provider}` - Counter of LLM requests per room
## Pricing Model
The system uses a built-in pricing model for cost estimation:
| Model/Provider | Input Price (per 1K tokens) | Output Price (per 1K tokens) |
|----------------|-----------------------------|------------------------------|
| gpt-4/openai | $0.03 | $0.06 |
| claude-3/anthropic | $0.015 | $0.075 |
| llama-3/together | $0.0005 | $0.0008 |
Note: These are example prices. Update the `_calculate_cost` function in `prometheus_plugin.py` with actual pricing.
## Troubleshooting
### No Metrics in Grafana
1. Check that the Hermes agent is running and making LLM calls
2. Verify the metrics endpoint is accessible: `curl http://localhost:8000/metrics`
3. Check Prometheus targets: http://localhost:9090/targets
4. Verify Prometheus is scraping the metrics: http://localhost:9090/graph
### Dashboard Not Loading
1. Check that Grafana is running: `docker ps | grep grafana`
2. Verify dashboard provisioning: Check files in `grafana/provisioning/`
3. Check Grafana logs: `docker logs grafana`
### High Memory Usage
The plugin stores metrics in memory. For high-traffic systems, consider:
1. Adjusting Prometheus retention policy in `prometheus.yml`
2. Adding more RAM to the host system
3. Using an external Prometheus instance instead of the bundled one
## Customization
### Adding New Models
Update the pricing dictionary in `prometheus_plugin.py`:
```python
pricing = {
# Existing models...
("new-model", "new-provider"): {"input": 0.01, "output": 0.02},
}
```
### Modifying Dashboard
Edit `grafana/dashboards/token-tracking.json` to customize the visualization.
### Changing Scraping Interval
Update `prometheus/prometheus.yml`:
```yaml
global:
scrape_interval: 30s # Change from 15s
```
## Security Considerations
- The metrics endpoint (`/metrics`) is not password protected by default
- Do not expose the Prometheus or Grafana ports to the internet without authentication
- Use firewall rules to restrict access to monitoring ports (8000, 9090, 3000)
- For production deployments, add authentication to Grafana and use HTTPS
## Scaling
For large deployments:
1. Use an external Prometheus instance with more resources
2. Implement federation for multi-instance setups
3. Use persistent storage volumes for long-term retention
4. Consider using Thanos or Cortex for horizontal scaling