Orchestrator / core /pricing.py
ZENLLC's picture
Create pricing.py
803da64 verified
from dataclasses import asdict
from typing import Dict, Any
from .config import ModelRate
def cost_from_tokens(rate: ModelRate, tokens: Dict[str, int]) -> Dict[str, Any]:
"""
tokens fields supported:
prompt_tokens, completion_tokens, cached_prompt_tokens, reasoning_tokens
"""
prompt = int(tokens.get("prompt_tokens", 0))
completion = int(tokens.get("completion_tokens", 0))
cached = int(tokens.get("cached_prompt_tokens", 0))
reasoning = int(tokens.get("reasoning_tokens", 0))
# per-token costs
inp = (prompt / 1_000_000.0) * rate.input_per_1m
out = (completion / 1_000_000.0) * rate.output_per_1m
cache = (cached / 1_000_000.0) * rate.cached_input_per_1m
reas = (reasoning / 1_000_000.0) * rate.reasoning_per_1m
total = inp + out + cache + reas
return {
"usd": round(total, 8),
"breakdown": {
"input": round(inp, 8),
"output": round(out, 8),
"cached_input": round(cache, 8),
"reasoning": round(reas, 8),
},
"rate": asdict(rate),
}