fix(budget): avoid remote image fetch in projected cost

This commit is contained in:
soroush5 2026-09-04 19:56:26 +03:30
parent 81f19176fc
commit 11addf2de0

View file

@ -100,7 +100,10 @@ class BudgetManager:
return self.user_dict[user]
def projected_cost(self, model: str, messages: list, user: str):
prompt_tokens: Final = litellm.token_counter(model=model, messages=messages)
# Fixed image estimate: a budget check must never fetch remote
# image URLs (unbounded/chunked bodies exhaust memory, and a check
# should not do network I/O at all).
prompt_tokens: Final = litellm.token_counter(model=model, messages=messages, use_default_image_token_count=True)
prompt_cost, _ = litellm.cost_per_token(model=model, prompt_tokens=prompt_tokens, completion_tokens=0)
current_cost: Final = self.user_dict[user].get("current_cost", 0)
projected_cost: Final = prompt_cost + current_cost