Skip to content

Commit eeda774

Browse files
authored
feat(src): add price calculation function (#6)
* add pricing_on_token_usage function * modify model names and revise functionality of calc pricing
1 parent d931034 commit eeda774

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

src/pricing.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Pricing data for each model (prices are in dollars per million (1,000,000) tokens)
2+
MODEL_PRICING = {
3+
# OpenAI models
4+
"gpt-3.5-turbo": (0.5, 1.5),
5+
"gpt-4o-mini": (0.15, 0.6),
6+
"gpt-4o": (2.5, 10),
7+
"o1-preview": (15, 60),
8+
"o1-mini": (1.1, 4.4),
9+
"o1": (15, 60),
10+
# OpenRouter models
11+
"llama3.1-405b": (3.5, 3.5),
12+
# Anthropic models
13+
"claude-3-sonnet-v1": (0.8, 4),
14+
"claude-3-sonnet": (3, 15),
15+
"claude-3-5-sonnet-v2": (3, 15),
16+
"claude-3-5-sonnet": (3, 15),
17+
"claude-3-haiku-v1": (0.25, 1.25),
18+
"claude-3-haiku": (0.25, 1.25),
19+
"claude-3-opus-v1": (15, 75),
20+
"claude-3-opus": (0.8, 4),
21+
# DeepSeek models
22+
"deepseek-chat": (0.07, 0.27),
23+
"deepseek-reasoner": (0.14, 0.55),
24+
# Google Gemini models
25+
"gemini-1.5-flash": (0.01875, 0.075),
26+
"gemini-1.5-pro": (0.3125, 1.25),
27+
}
28+
29+
def calculate_pricing(model: str, input_tokens: int, output_tokens: int) -> float:
30+
# Check if the model exists
31+
if model not in MODEL_PRICING:
32+
for m in MODEL_PRICING:
33+
if model.startswith(m):
34+
model = m
35+
else:
36+
raise ValueError(f"Pricing for '{model}' is not found.")
37+
38+
input_price, output_price = MODEL_PRICING[model]
39+
40+
# Check if pricing data is available
41+
if input_price is None or output_price is None:
42+
raise ValueError(f"Pricing for '{model}' is unavailable.")
43+
44+
# The pricing is per million (1,000,000) tokens.
45+
input_cost = (input_tokens / 1000000) * input_price
46+
output_cost = (output_tokens / 1000000) * output_price
47+
48+
total_cost = input_cost + output_cost
49+
return total_cost

0 commit comments

Comments
 (0)