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:
amabito 2026-03-09 19:19:44 +09:00 committed by amabito
parent 49b119fdf1
commit 4e1cac78ce

View file

@ -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",