1111)
1212
1313from nilai_api .config import CONFIG
14+ from nilai_api .pricing_service import get_pricing_service
1415
1516from nuc .envelope import NucTokenEnvelope
1617
@@ -51,6 +52,22 @@ def default() -> "LLMCost":
5152 prompt_tokens_price = 2.0 , completion_tokens_price = 2.0 , web_search_cost = 0.05
5253 )
5354
55+ @staticmethod
56+ async def from_redis (model_name : str ) -> "LLMCost" :
57+ """Fetch pricing from Redis for a specific model."""
58+ try :
59+ pricing_service = get_pricing_service ()
60+ price_config = await pricing_service .get_price (model_name )
61+ return LLMCost (
62+ prompt_tokens_price = price_config .prompt_tokens_price ,
63+ completion_tokens_price = price_config .completion_tokens_price ,
64+ web_search_cost = price_config .web_search_cost ,
65+ )
66+ except RuntimeError :
67+ # Pricing service not initialized, use default
68+ logger .warning ("Pricing service not initialized, using default pricing" )
69+ return LLMCost .default ()
70+
5471 def total_cost (
5572 self , prompt_tokens : int , completion_tokens : int , web_searches : int
5673 ) -> float :
@@ -87,14 +104,6 @@ class LLMResponse(BaseModel):
87104
88105LLMCostDict : TypeAlias = dict [str , LLMCost ]
89106
90-
91- MyCostDictionary : LLMCostDict = {
92- "meta-llama/Llama-3.2-1B-Instruct" : LLMCost (
93- prompt_tokens_price = 3.0 , completion_tokens_price = 3.0 , web_search_cost = 0.05
94- ),
95- "default" : LLMCost .default (),
96- }
97-
98107# Configure the singleton credit client
99108CreditClientSingleton .configure (
100109 base_url = CONFIG .auth .credit_service_url ,
@@ -138,10 +147,10 @@ async def extractor(request: Request) -> str:
138147 return extractor
139148
140149
141- def llm_cost_calculator (llm_cost_dict : LLMCostDict ):
150+ def llm_cost_calculator ():
142151 async def calculator (request : Request , response_data : dict ) -> float :
143152 model_name = getattr (request , "model" , "default" )
144- llm_cost = llm_cost_dict . get ( model_name , LLMCost .default () )
153+ llm_cost = await LLMCost .from_redis ( model_name )
145154 total_cost = 0.0
146155 usage : Optional [LLMUsage ] = response_data .get ("usage" , None )
147156 if usage is None :
@@ -157,8 +166,8 @@ async def calculator(request: Request, response_data: dict) -> float:
157166
158167_base_llm_meter = create_metering_dependency (
159168 credential_extractor = credential_extractor (),
160- estimated_cost = 2.0 ,
161- cost_calculator = llm_cost_calculator (MyCostDictionary ),
169+ estimated_cost = 0.5 ,
170+ cost_calculator = llm_cost_calculator (),
162171 public_identifiers = CONFIG .auth .auth_strategy == "nuc" ,
163172)
164173
0 commit comments