Files
owi-litellm-tool/litellm_dashboard.py
T

238 lines
11 KiB
Python

"""
title: LiteLLM Dashboard
author: Kagamine Rin & VAI
author_url: https://rin.captcouch.xyz
description: Consolidated Hardcore LiteLLM observability — status, usage, and history.
required_open_webui_version: 0.4.0
requirements: requests
version: 1.5.0
licence: MIT
"""
import requests
import json
from datetime import datetime, timedelta
from zoneinfo import ZoneInfo
from pydantic import BaseModel, Field
from typing import Optional, List, Dict, Any, Literal
class Tools:
def __init__(self):
self.valves = self.Valves()
self._price_cache = {}
class Valves(BaseModel):
"""LiteLLM proxy connection settings."""
litellm_url: str = Field(
default="http://localhost:4000",
description="LiteLLM proxy base URL",
)
litellm_master_key: str = Field(
default="",
description="LiteLLM master API key for authentication",
json_schema_extra={"input": {"type": "password"}},
)
default_hide_zero_spend: bool = Field(
default=True,
description="Default behavior for zero-spend model filtering.",
)
timezone: str = Field(
default="UTC",
description="IANA Timezone name (e.g. America/New_York).",
)
enable_dynamic_pricing: bool = Field(
default=True,
description="When True, attempts to fetch live pricing from /v1/model/info for the Audit Engine.",
)
def _headers(self) -> dict:
if not self.valves.litellm_master_key:
return {}
return {"Authorization": f"Bearer {self.valves.litellm_master_key}"}
def _base_url(self) -> str:
return self.valves.litellm_url.rstrip("/")
def _fetch_live_prices(self):
"""Sync internal price cache with LiteLLM's live model registry."""
if not self.valves.enable_dynamic_pricing or self._price_cache:
return
try:
url = f"{self._base_url()}/v1/model/info"
resp = requests.get(url, headers=self._headers(), timeout=10)
if resp.status_code == 200:
data = resp.json().get("data", [])
for m in data:
name = m.get("model_name")
info = m.get("model_info", {})
if name and info:
self._price_cache[name] = {
"input": info.get("input_cost_per_token", 0),
"input_cached": info.get("cache_read_input_token_cost", 0),
"output": info.get("output_cost_per_token", 0),
"reasoning": info.get("output_cost_per_reasoning_token", info.get("output_cost_per_token", 0))
}
except Exception: pass
def _resolve_date(self, date_str: str) -> str:
date_str = date_str.strip().lower()
try:
today = datetime.now(ZoneInfo(self.valves.timezone)).date()
except Exception:
today = datetime.utcnow().date()
if date_str == "today": return today.isoformat()
if date_str == "yesterday": return (today - timedelta(days=1)).isoformat()
if "days ago" in date_str:
try:
n = int(date_str.split()[0])
return (today - timedelta(days=n)).isoformat()
except Exception: pass
try:
datetime.strptime(date_str, "%Y-%m-%d")
return date_str
except ValueError: pass
return date_str
def _audit_cost(self, model: str, prompt: int, cached: int, completion: int, reported_cost: float, reasoning: int = 0) -> str:
"""Audit reported cost with dynamic pricing."""
self._fetch_live_prices()
pricing = self._price_cache.get(model)
if not pricing: return ""
uncached = max(0, prompt - cached)
actual_output = max(0, completion - reasoning)
calc = (cached * pricing.get("input_cached", 0)) + \
(uncached * pricing.get("input", 0)) + \
(actual_output * pricing.get("output", 0)) + \
(reasoning * pricing.get("reasoning", pricing.get("output", 0)))
delta = reported_cost - calc
symbol = "⚠️" if abs(delta) > 0.005 else "✓"
return f" [calc: ${calc:.4f} {symbol}]"
def litellm_status(self) -> str:
"""
Comprehensive system check: Proxy readiness, DB connectivity, and Model health.
Use this to verify the infrastructure is up and which models are available.
"""
lines = [f"LiteLLM Infrastructure Status — {self._base_url()}", "=" * 50]
# 1. Readiness
try:
r = requests.get(f"{self._base_url()}/health/readiness", timeout=5)
if r.status_code == 200:
d = r.json()
lines.append(f"Proxy: ✅ {d.get('status')} | DB: {d.get('db')} | v{d.get('litellm_version')}")
else: lines.append(f"Proxy Readiness: ⚠️ Status {r.status_code}")
except Exception: lines.append("Proxy Readiness: ❌ Connection Error")
# 2. Model Health
try:
h = requests.get(f"{self._base_url()}/health", headers=self._headers(), timeout=10)
if h.status_code == 200:
hd = h.json()
healthy = hd.get("healthy_endpoints", [])
unhealthy = hd.get("unhealthy_endpoints", [])
lines.append(f"Models: {len(healthy)} healthy, {len(unhealthy)} unhealthy")
for ep in healthy[:15]: # Cap display to save tokens
m = ep.get("model", "unknown")
lines.append(f" ✅ {m:25s} | rpm: {ep.get('rpm','?'):>3}")
if len(healthy) > 15: lines.append(f" ... and {len(healthy)-15} more healthy models.")
for ep in unhealthy:
lines.append(f" ❌ {ep.get('model', 'unknown')}")
except Exception: lines.append("Model Health: ❌ Failed to fetch")
return "\n".join(lines)
def litellm_usage(self, start_date: str = "today", end_date: str = "today", detail_level: Literal["summary", "full"] = "summary") -> str:
"""
Get fiscal usage and spend data.
:param start_date: YYYY-MM-DD or shortcut (today, yesterday, 7 days ago)
:param end_date: YYYY-MM-DD or shortcut
:param detail_level: 'summary' for quick spend snapshot, 'full' for daily breakdown and sparklines.
"""
if not self.valves.litellm_master_key: return "❌ Master key required."
r_start = self._resolve_date(start_date)
r_end = self._resolve_date(end_date)
if detail_level == "summary":
try:
s = requests.get(f"{self._base_url()}/global/spend/models", headers=self._headers(), timeout=10)
if s.status_code == 200:
models = sorted(s.json(), key=lambda x: x.get("total_spend", 0), reverse=True)
total = sum(m.get("total_spend", 0) for m in models)
lines = [f"LiteLLM Spend Summary (Lifetime)", "=" * 40, f"Total Lifetime Spend: ${total:.2f}"]
for m in models[:5]:
lines.append(f" - {m['model']}: ${m['total_spend']:.2f}")
return "\n".join(lines)
except Exception: pass
# Full Report
try:
url = f"{self._base_url()}/user/daily/activity"
resp = requests.get(url, headers=self._headers(), params={"start_date": r_start, "end_date": r_end}, timeout=30)
if resp.status_code != 200: return f"❌ Error {resp.status_code}"
data = resp.json()
except Exception as e: return f"❌ Failed: {str(e)}"
results = data.get("results", [])
if not results: return f"No usage found for {r_start} to {r_end}."
spends = [d.get("metrics", {}).get("spend", 0) for d in results]
def get_spark(vals):
if len(vals) < 2: return ""
chars = " ▂▃▄▅▆▇█"
low, high = min(vals), max(vals)
rng = (high - low) or 1
return " [" + "".join(chars[int((v - low) / rng * 7)] for v in vals) + "]"
meta = data.get("metadata", {})
lines = [f"LiteLLM Usage Report: {r_start} to {r_end}", "=" * 60]
lines.append(f"Spend: ${meta.get('total_spend', 0):.2f}{get_spark(spends)}")
lines.append(f"Requests: {meta.get('total_successful_requests', 0)} ok, {meta.get('total_failed_requests', 0)} fail")
lines.append("\nBreakdown by Endpoint:")
ends = results[0].get("breakdown", {}).get("endpoints", {}) if results else {}
for path, edata in ends.items():
m = edata.get("metrics", {})
lines.append(f" {path:20s} | ${m.get('spend',0):.4f} | {m.get('successful_requests',0)} reqs")
return "\n".join(lines)
def litellm_history(self, limit: int = 20, filter_model: Optional[str] = None) -> str:
"""
Get recent request logs with audit data and thought detection.
:param limit: Number of requests (1-50)
:param filter_model: Optional string to filter by model name (e.g. 'gpt-4')
"""
if not self.valves.litellm_master_key: return "❌ Master key required."
try:
resp = requests.get(f"{self._base_url()}/spend/logs", headers=self._headers(), timeout=30)
if resp.status_code != 200: return f"❌ Error {resp.status_code}"
logs = sorted(resp.json(), key=lambda x: x.get("startTime", ""), reverse=True)
except Exception as e: return f"❌ Failed: {str(e)}"
if filter_model:
logs = [l for l in logs if filter_model.lower() in l.get("model", "").lower()]
recent = logs[:max(1, min(50, limit))]
lines = [f"LiteLLM History (Most Recent {len(recent)})", "=" * 95]
for i, entry in enumerate(recent, 1):
ts = entry.get("startTime", "?")[5:16]
model = entry.get("model", "unknown")[:25]
spend = entry.get("spend", 0)
prompt = entry.get("prompt_tokens", 0) or 0
completion = entry.get("completion_tokens", 0) or 0
usage = entry.get("usage", {}) or {}
thought = usage.get("completion_tokens_details", {}).get("reasoning_tokens", 0)
thought_str = f" [💭{thought}]" if thought else ""
cached = entry.get("cache_read_input_tokens", 0) or 0
cache_str = f" 💾{int(cached/prompt*100)}%" if prompt and cached else ""
audit_display = self._audit_cost(entry.get("model"), prompt, cached, completion, spend, reasoning=thought)
status = "❌" if entry.get("metadata", {}).get("exception") else "✅"
lines.append(f"{i:2d}. {ts} {status} | {model:25s} | ${spend:.4f}{audit_display} | {prompt}→{completion}{thought_str}{cache_str}")
return "\n".join(lines)