0ef5fcb1c5
Security / Dependency audit (pip-audit) (push) Has been cancelled
Security / CodeQL (javascript-typescript) (push) Has been cancelled
Security / CodeQL (python) (push) Has been cancelled
Security / Secret scan (gitleaks) (push) Has been cancelled
rust / test (ubuntu) (push) Has been cancelled
rust / simulator e2e (macos-latest) (push) Has been cancelled
rust / simulator e2e (ubuntu-latest) (push) Has been cancelled
rust / simulator e2e (windows-latest) (push) Has been cancelled
rust / wheels (aarch64-apple-darwin) (push) Has been cancelled
rust / wheels (x86_64-unknown-linux-gnu) (push) Has been cancelled
rust / wheels (x86_64-apple-darwin) (push) Has been cancelled
rust / audit (push) Has been cancelled
rust / parity (nightly, allowed to fail during Phase 0) (push) Has been cancelled
CI / commitlint (push) Has been skipped
Dev Containers / validate (.devcontainer/devcontainer.json, default) (push) Failing after 0s
Dev Containers / validate (.devcontainer/memory-stack/devcontainer.json, memory-stack) (push) Failing after 0s
Dev Containers / validate-worktree (push) Failing after 0s
CI / changes (push) Failing after 4s
Deploy Documentation / validate (push) Has been skipped
Deploy Documentation / deploy (push) Failing after 1s
Init Native E2E / init-native (ubuntu-latest, claude) (push) Failing after 1s
Init Native E2E / init-native (ubuntu-latest, codex) (push) Failing after 1s
Install Native E2E / install-native (ubuntu-latest) (push) Failing after 1s
OpenCode Plugin / typecheck + build + test (push) Failing after 1s
Init Native E2E / init-native (ubuntu-latest, copilot) (push) Failing after 1s
Release Please / release-please (push) Failing after 1s
Wrap E2E / docker-wrap-e2e (push) Failing after 1s
Wrap Native E2E / wrap-native (ubuntu-latest) (push) Failing after 1s
Init E2E / docker-init-e2e (push) Failing after 4s
Merge Conflicts / merge-conflicts (push) Failing after 4s
CI / lint (push) Has been cancelled
CI / build-wheel (push) Has been cancelled
CI / build-wheel-windows (push) Has been cancelled
CI / prefetch-model (push) Has been cancelled
CI / test-dashboard-ui (push) Has been cancelled
CI / test (1) (push) Has been cancelled
CI / test (2) (push) Has been cancelled
CI / test (3) (push) Has been cancelled
CI / test (4) (push) Has been cancelled
CI / test-extras (push) Has been cancelled
CI / test-agno (push) Has been cancelled
CI / build (push) Has been cancelled
CI / workflow-validation (push) Has been cancelled
CI / docker-native-e2e (push) Has been cancelled
CI / windows-native-wrapper (push) Has been cancelled
CI / macos-native-wrapper (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-code-nonroot name:code-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-code-slim name:code-slim]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-code-slim-nonroot name:code-slim-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-nonroot name:nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-slim name:slim]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-slim-nonroot name:slim-nonroot]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime name:]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-code name:code]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-code-nonroot name:code-nonroot]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-code-slim name:code-slim]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-code-slim-nonroot name:code-slim-nonroot]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-nonroot name:nonroot]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-slim name:slim]) (push) Has been cancelled
Docker / docker-manifest (map[bake_target:runtime-slim-nonroot name:slim-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime name:]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-code name:code]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-code-nonroot name:code-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-code-slim name:code-slim]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-code-slim-nonroot name:code-slim-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-nonroot name:nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-slim name:slim]) (push) Has been cancelled
Docker / docker-build (map[name:amd64 platform:linux/amd64 runs_on:ubuntu-24.04], map[bake_target:runtime-slim-nonroot name:slim-nonroot]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime name:]) (push) Has been cancelled
Docker / docker-build (map[name:arm64 platform:linux/arm64 runs_on:ubuntu-24.04-arm], map[bake_target:runtime-code name:code]) (push) Has been cancelled
Docker / promote-latest (push) Has been cancelled
Init Native E2E / init-native (macos-latest, claude) (push) Has been cancelled
Init Native E2E / init-native (macos-latest, codex) (push) Has been cancelled
Init Native E2E / init-native (macos-latest, copilot) (push) Has been cancelled
Install Native E2E / install-native (macos-latest) (push) Has been cancelled
Wrap Native E2E / wrap-native (macos-latest) (push) Has been cancelled
277 lines
6.9 KiB
Plaintext
277 lines
6.9 KiB
Plaintext
---
|
|
title: Metrics & Monitoring
|
|
description: Monitor compression performance, cost savings, and system health with Headroom's built-in metrics, Prometheus endpoint, and SDK APIs.
|
|
---
|
|
|
|
Headroom provides comprehensive metrics for monitoring compression performance, cost savings, and system health through both the proxy server and the SDK.
|
|
|
|
## Proxy Endpoints
|
|
|
|
### Stats Endpoint
|
|
|
|
```bash
|
|
curl http://localhost:8787/stats
|
|
```
|
|
|
|
```json
|
|
{
|
|
"persistent_savings": {
|
|
"lifetime": {
|
|
"tokens_saved": 12500,
|
|
"compression_savings_usd": 0.04
|
|
}
|
|
},
|
|
"requests": {
|
|
"total": 42,
|
|
"cached": 5,
|
|
"rate_limited": 0,
|
|
"failed": 0
|
|
},
|
|
"tokens": {
|
|
"input": 50000,
|
|
"output": 8000,
|
|
"saved": 12500,
|
|
"savings_percent": 25.0
|
|
},
|
|
"cost": {
|
|
"total_cost_usd": 0.15,
|
|
"total_savings_usd": 0.04
|
|
},
|
|
"cache": {
|
|
"entries": 10,
|
|
"total_hits": 5
|
|
}
|
|
}
|
|
```
|
|
|
|
Persistent savings are stored at `~/.headroom/proxy_savings.json` and survive proxy restarts. Override the path with `HEADROOM_SAVINGS_PATH`.
|
|
|
|
### Historical Savings
|
|
|
|
```bash
|
|
curl http://localhost:8787/stats-history
|
|
```
|
|
|
|
Returns durable compression history with hourly, daily, weekly, and monthly rollups. Supports CSV export:
|
|
|
|
```bash
|
|
curl "http://localhost:8787/stats-history?format=csv&series=daily"
|
|
curl "http://localhost:8787/stats-history?format=csv&series=monthly"
|
|
```
|
|
|
|
### Prometheus Metrics
|
|
|
|
```bash
|
|
curl http://localhost:8787/metrics
|
|
```
|
|
|
|
```
|
|
# HELP headroom_requests_total Total requests processed
|
|
headroom_requests_total{mode="optimize"} 1234
|
|
|
|
# HELP headroom_tokens_saved_total Total tokens saved
|
|
headroom_tokens_saved_total 5678900
|
|
|
|
# HELP headroom_persistent_savings_tokens_saved_total Durable lifetime input tokens saved by proxy compression
|
|
headroom_persistent_savings_tokens_saved_total 5678900
|
|
|
|
# HELP headroom_compression_ratio Compression ratio histogram
|
|
headroom_compression_ratio_bucket{le="0.5"} 890
|
|
headroom_compression_ratio_bucket{le="0.7"} 1100
|
|
headroom_compression_ratio_bucket{le="0.9"} 1200
|
|
|
|
# HELP headroom_latency_seconds Request latency histogram
|
|
headroom_latency_seconds_bucket{le="0.01"} 800
|
|
headroom_latency_seconds_bucket{le="0.1"} 1150
|
|
|
|
# HELP headroom_cache_hits_total Cache hit counter
|
|
headroom_cache_hits_total 456
|
|
```
|
|
|
|
### Health Check
|
|
|
|
```bash
|
|
curl http://localhost:8787/health
|
|
```
|
|
|
|
```json
|
|
{
|
|
"status": "healthy",
|
|
"version": "0.1.0",
|
|
"uptime_seconds": 3600
|
|
}
|
|
```
|
|
|
|
## SDK Metrics
|
|
|
|
<Tabs groupId="lang" items={['TypeScript', 'Python']}>
|
|
<Tab value="TypeScript">
|
|
|
|
### Proxy Stats
|
|
|
|
The TypeScript SDK queries the proxy for stats:
|
|
|
|
```ts twoslash
|
|
import { HeadroomClient } from 'headroom-ai';
|
|
|
|
const client = new HeadroomClient();
|
|
|
|
// Get proxy stats
|
|
const stats = await client.proxyStats();
|
|
console.log(`Tokens saved: ${stats.tokens.saved}`);
|
|
console.log(`Savings: ${stats.tokens.savings_percent}%`);
|
|
```
|
|
|
|
### Compression Result Metrics
|
|
|
|
Every `compress()` call returns metrics:
|
|
|
|
```ts twoslash
|
|
import { compress } from 'headroom-ai';
|
|
|
|
const result = await compress(messages, { model: 'gpt-4o' });
|
|
console.log(`Tokens: ${result.tokensBefore} -> ${result.tokensAfter}`);
|
|
console.log(`Saved: ${result.tokensSaved} (${(result.compressionRatio * 100).toFixed(1)}%)`);
|
|
console.log(`Transforms: ${result.transformsApplied.join(', ')}`);
|
|
```
|
|
|
|
</Tab>
|
|
<Tab value="Python">
|
|
|
|
### Session Stats
|
|
|
|
Quick stats for the current session (no database query):
|
|
|
|
```python
|
|
stats = client.get_stats()
|
|
print(f"Mode: {stats['config']['mode']}")
|
|
print(f"Tokens saved: {stats['session']['tokens_saved_total']}")
|
|
print(f"Avg compression: {stats['session']['compression_ratio_avg']:.1%}")
|
|
```
|
|
|
|
Returns:
|
|
|
|
```python
|
|
{
|
|
"session": {
|
|
"requests_total": 10,
|
|
"tokens_input_before": 50000,
|
|
"tokens_input_after": 35000,
|
|
"tokens_saved_total": 15000,
|
|
"tokens_output_total": 8000,
|
|
"cache_hits": 3,
|
|
"compression_ratio_avg": 0.70,
|
|
},
|
|
"config": {
|
|
"mode": "optimize",
|
|
"provider": "openai",
|
|
"cache_optimizer_enabled": True,
|
|
"semantic_cache_enabled": False,
|
|
},
|
|
"transforms": {
|
|
"smart_crusher_enabled": True,
|
|
"cache_aligner_enabled": True,
|
|
"rolling_window_enabled": True,
|
|
},
|
|
}
|
|
```
|
|
|
|
### Historical Metrics
|
|
|
|
Query stored metrics from the database:
|
|
|
|
```python
|
|
from datetime import datetime, timedelta
|
|
|
|
metrics = client.get_metrics(
|
|
start_time=datetime.utcnow() - timedelta(hours=1),
|
|
limit=100,
|
|
)
|
|
|
|
for m in metrics:
|
|
print(f"{m.timestamp}: {m.tokens_input_before} -> {m.tokens_input_after}")
|
|
```
|
|
|
|
### Summary Statistics
|
|
|
|
Aggregate statistics across all stored metrics:
|
|
|
|
```python
|
|
summary = client.get_summary()
|
|
print(f"Total requests: {summary['total_requests']}")
|
|
print(f"Total tokens saved: {summary['total_tokens_saved']}")
|
|
print(f"Average compression: {summary['avg_compression_ratio']:.1%}")
|
|
print(f"Total cost savings: ${summary['total_cost_saved_usd']:.2f}")
|
|
```
|
|
|
|
</Tab>
|
|
</Tabs>
|
|
|
|
## Logging
|
|
|
|
<Tabs groupId="lang" items={['Python', 'Proxy']}>
|
|
<Tab value="Python">
|
|
```python
|
|
import logging
|
|
|
|
# INFO level shows compression summaries
|
|
logging.basicConfig(level=logging.INFO)
|
|
|
|
# DEBUG level shows detailed transform decisions
|
|
logging.basicConfig(level=logging.DEBUG)
|
|
```
|
|
|
|
Example output:
|
|
|
|
```
|
|
INFO:headroom.transforms.pipeline:Pipeline complete: 45000 -> 4500 tokens (saved 40500, 90.0% reduction)
|
|
INFO:headroom.transforms.smart_crusher:SmartCrusher applied top_n strategy: kept 15 of 1000 items
|
|
DEBUG:headroom.transforms.smart_crusher:Kept items: [0,1,2,42,77,97,98,99] (errors at 42, warnings at 77)
|
|
```
|
|
</Tab>
|
|
<Tab value="Proxy">
|
|
```bash
|
|
# Log to file
|
|
headroom proxy --log-file headroom.jsonl
|
|
|
|
# Increase verbosity
|
|
headroom proxy --log-level debug
|
|
```
|
|
</Tab>
|
|
</Tabs>
|
|
|
|
## Cost Tracking
|
|
|
|
### Budget Alerts
|
|
|
|
Set a budget limit in the proxy:
|
|
|
|
```bash
|
|
headroom proxy --budget 10.00
|
|
```
|
|
|
|
When the budget is exceeded, requests return a budget exceeded error, the `/stats` endpoint shows budget status, and logs indicate the budget state.
|
|
|
|
## Key Metrics to Monitor
|
|
|
|
| Metric | What It Tells You | Target |
|
|
|--------|-------------------|--------|
|
|
| `headroom_tokens_saved_total` | Runtime tokens saved since this proxy process started | Higher is better |
|
|
| `headroom_persistent_savings_tokens_saved_total` | Durable lifetime tokens saved from `/stats.persistent_savings` | Higher is better |
|
|
| `compression_ratio_avg` | Efficiency | 0.7--0.9 typical |
|
|
| `cache_hit_rate` | Cache effectiveness | >20% is good |
|
|
| `latency_p99` | Performance impact | <10ms |
|
|
| `failed_requests` | Reliability | 0 |
|
|
|
|
## Grafana Dashboard
|
|
|
|
Example Prometheus queries for a Grafana dashboard:
|
|
|
|
| Panel | PromQL |
|
|
|-------|--------|
|
|
| Runtime Tokens Saved | `headroom_tokens_saved_total` |
|
|
| Lifetime Tokens Saved | `headroom_persistent_savings_tokens_saved_total` |
|
|
| Compression Ratio (median) | `histogram_quantile(0.5, headroom_compression_ratio_bucket)` |
|
|
| Request Latency (p99) | `histogram_quantile(0.99, headroom_latency_seconds_bucket)` |
|
|
| Cache Hit Rate | `headroom_cache_hits_total / (headroom_cache_hits_total + headroom_cache_misses_total)` |
|