mirror of
https://github.com/BerriAI/litellm.git
synced 2026-09-11 22:51:28 +00:00
fix(cookbook): add async callback for acompletion support
Extract shared _record_cost helper and implement both log_success_event and async_log_success_event so the budget cap works with litellm.acompletion as well.
This commit is contained in:
parent
49b119fdf1
commit
4e1cac78ce
1 changed files with 10 additions and 3 deletions
13
cookbook/LiteLLM_Veronica_Run_Budget.ipynb
vendored
13
cookbook/LiteLLM_Veronica_Run_Budget.ipynb
vendored
|
|
@ -49,13 +49,20 @@
|
|||
"budget = BudgetEnforcer(limit_usd=0.05)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def _record_cost(kwargs):\n",
|
||||
" cost = kwargs.get(\"response_cost\", 0.0)\n",
|
||||
" if cost > 0:\n",
|
||||
" budget.spend(cost)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class RunBudgetCallback(CustomLogger):\n",
|
||||
" \"\"\"Record response_cost in BudgetEnforcer after each successful call.\"\"\"\n",
|
||||
"\n",
|
||||
" def log_success_event(self, kwargs, response_obj, start_time, end_time):\n",
|
||||
" cost = kwargs.get(\"response_cost\", 0.0)\n",
|
||||
" if cost > 0:\n",
|
||||
" budget.spend(cost)\n",
|
||||
" _record_cost(kwargs)\n",
|
||||
"\n",
|
||||
" async def async_log_success_event(self, kwargs, response_obj, start_time, end_time):\n",
|
||||
" _record_cost(kwargs)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"litellm.callbacks = [RunBudgetCallback()]\n",
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue