ZENLLC commited on
Commit
803da64
·
verified ·
1 Parent(s): e2a55ab

Create pricing.py

Browse files
Files changed (1) hide show
  1. core/pricing.py +32 -0
core/pricing.py ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from dataclasses import asdict
2
+ from typing import Dict, Any
3
+ from .config import ModelRate
4
+
5
+ def cost_from_tokens(rate: ModelRate, tokens: Dict[str, int]) -> Dict[str, Any]:
6
+ """
7
+ tokens fields supported:
8
+ prompt_tokens, completion_tokens, cached_prompt_tokens, reasoning_tokens
9
+ """
10
+ prompt = int(tokens.get("prompt_tokens", 0))
11
+ completion = int(tokens.get("completion_tokens", 0))
12
+ cached = int(tokens.get("cached_prompt_tokens", 0))
13
+ reasoning = int(tokens.get("reasoning_tokens", 0))
14
+
15
+ # per-token costs
16
+ inp = (prompt / 1_000_000.0) * rate.input_per_1m
17
+ out = (completion / 1_000_000.0) * rate.output_per_1m
18
+ cache = (cached / 1_000_000.0) * rate.cached_input_per_1m
19
+ reas = (reasoning / 1_000_000.0) * rate.reasoning_per_1m
20
+
21
+ total = inp + out + cache + reas
22
+
23
+ return {
24
+ "usd": round(total, 8),
25
+ "breakdown": {
26
+ "input": round(inp, 8),
27
+ "output": round(out, 8),
28
+ "cached_input": round(cache, 8),
29
+ "reasoning": round(reas, 8),
30
+ },
31
+ "rate": asdict(rate),
32
+ }