File size: 1,083 Bytes
803da64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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),
    }