Version 1.2.2. Added usage overview. Shows token used and cost in $.

This commit is contained in:
2026-04-15 10:00:39 +02:00
parent 752691fe54
commit d4c6420481
18 changed files with 1657 additions and 86 deletions

View File

@@ -372,6 +372,50 @@ async def get_models_info(
return results
def get_model_pricing(model_id: str) -> tuple[float | None, float | None]:
"""
Return (prompt_per_1m, completion_per_1m) in USD for the given model ID.
Uses only in-memory data (hardcoded Anthropic/OpenAI + cached OpenRouter raw).
Returns (None, None) if pricing is unknown for this model.
model_id format: "anthropic:claude-sonnet-4-6", "openrouter:openai/gpt-4o", "openai:gpt-4o"
"""
for m in _ANTHROPIC_MODEL_INFO:
if m["id"] == model_id:
p = m["pricing"]
return p["prompt_per_1m"], p["completion_per_1m"]
for m in _OPENAI_MODEL_INFO:
if m["id"] == model_id:
p = m["pricing"]
return p["prompt_per_1m"], p["completion_per_1m"]
# OpenRouter: strip "openrouter:" prefix to get the bare OR model id
if model_id.startswith("openrouter:"):
bare = model_id[len("openrouter:"):]
for m in _or_raw:
if m.get("id", "") == bare and not _is_free_openrouter(m):
pricing = m.get("pricing", {})
try:
prompt = float(pricing.get("prompt", 0)) * 1_000_000
completion = float(pricing.get("completion", 0)) * 1_000_000
return prompt, completion
except (TypeError, ValueError):
return None, None
return None, None
def compute_cost_usd(
model_id: str,
input_tokens: int,
output_tokens: int,
) -> float | None:
"""Compute estimated cost in USD for a completed run. Returns None if pricing unknown."""
prompt_per_1m, completion_per_1m = get_model_pricing(model_id)
if prompt_per_1m is None or completion_per_1m is None:
return None
return (input_tokens / 1_000_000) * prompt_per_1m + (output_tokens / 1_000_000) * completion_per_1m
async def get_access_tier(
user_id: str | None = None,
is_admin: bool = True,