77
88logger = logging .getLogger ("aide" )
99
10- #cost per input/output token for each model
10+ # cost per input/output token for each model
1111MODEL_COST = {
12- "gpt-4o-2024-08-06" : {"input" : 2.5 / 1000000 , "output" : 10 / 1000000 },
13- "o3-mini" : {"input" : 1.1 / 1000000 , "output" : 4.4 / 1000000 },
14- "o3" : {"input" : 10 / 1000000 , "output" : 40 / 1000000 },
12+ "gpt-4o-2024-08-06" : {"input" : 2.5 / 1000000 , "output" : 10 / 1000000 },
13+ "o3-mini" : {"input" : 1.1 / 1000000 , "output" : 4.4 / 1000000 },
14+ "o3" : {"input" : 10 / 1000000 , "output" : 40 / 1000000 },
1515}
1616
17+
1718def determine_provider (model : str ) -> str :
18- if model .startswith ("gpt-" ) or model .startswith ("o1-" ) or model .startswith ("o3-" ) or model .startswith ("o4-" ):
19+ if (
20+ model .startswith ("gpt-" )
21+ or model .startswith ("o1-" )
22+ or model .startswith ("o3-" )
23+ or model .startswith ("o4-" )
24+ ):
1925 return "openai"
2026 elif model .startswith ("claude-" ):
2127 return "anthropic"
@@ -33,67 +39,69 @@ def determine_provider(model: str) -> str:
3339 "openrouter" : backend_openrouter .query ,
3440}
3541
42+
3643class TokenCounter :
37- def __init__ (self , cost_limit :int ):
44+ def __init__ (self , cost_limit : int ):
3845 self .cost_limit = cost_limit
3946 self .total_input_tokens = defaultdict (int )
4047 self .total_output_tokens = defaultdict (int )
41-
48+
4249 def cost (self ) -> float :
43- '''
50+ """
4451 compute to total cost of the tokens used
45- '''
52+ """
4653 total_cost = 0
4754
48- #compute cost for input tokens
55+ # compute cost for input tokens
4956 for model_name , input_tokens in self .total_input_tokens .items ():
5057 if model_name not in MODEL_COST :
5158 raise ValueError (f"Model { model_name } not supported for token counting" )
5259 total_cost += input_tokens * MODEL_COST [model_name ]["input" ]
53-
54- #compute cost for output tokens
60+
61+ # compute cost for output tokens
5562 for model_name , output_tokens in self .total_output_tokens .items ():
5663 if model_name not in MODEL_COST :
5764 raise ValueError (f"Model { model_name } not supported for token counting" )
5865 total_cost += output_tokens * MODEL_COST [model_name ]["output" ]
5966 return total_cost
60-
61- def add_tokens (self , model_name :str , input_tokens = None , output_tokens = None ):
62- '''
67+
68+ def add_tokens (self , model_name : str , input_tokens = None , output_tokens = None ):
69+ """
6370 update the token counts
64- '''
71+ """
6572 if model_name not in MODEL_COST :
6673 raise ValueError (f"Model { model_name } not supported for token counting" )
67-
74+
6875 if input_tokens is not None :
6976 self .total_input_tokens [model_name ] += input_tokens
7077 if output_tokens is not None :
7178 self .total_output_tokens [model_name ] += output_tokens
7279
73- def remaining_output_tokens (self , model_name :str , max_budget :int ) -> int :
74- '''
80+ def remaining_output_tokens (self , model_name : str , max_budget : int ) -> int :
81+ """
7582 max_budget: the maximum dollar budget for the model
7683 compute the remaining tokens for a model
77- '''
84+ """
7885 if model_name not in MODEL_COST :
7986 raise ValueError (f"Model { model_name } not supported for token counting" )
80-
87+
8188 current_cost = self .cost
8289 remaining_budget = max_budget - current_cost
8390 if remaining_budget <= 0 :
8491 return 0
8592 else :
8693 output_tokens_cost = MODEL_COST [model_name ]["output" ]
8794 return int (remaining_budget / output_tokens_cost )
88-
95+
8996 def exceed_budget_limit (self ) -> bool :
90- '''
97+ """
9198 check if the budget limit is exceeded
92- '''
93-
99+ """
100+
94101 current_cost = self .cost
95102 return current_cost > self .cost_limit
96-
103+
104+
97105def query (
98106 system_message : PromptType | None ,
99107 user_message : PromptType | None ,
@@ -150,6 +158,8 @@ def query(
150158 logger .info (f"response: { output } " , extra = {"verbose" : True })
151159 logger .info ("---Query complete---" , extra = {"verbose" : True })
152160 if token_counter is not None :
153- token_counter .add_tokens (model , input_tokens = in_tok_count , output_tokens = out_tok_count )
161+ token_counter .add_tokens (
162+ model , input_tokens = in_tok_count , output_tokens = out_tok_count
163+ )
154164
155165 return output
0 commit comments