diff --git a/backend/python/app/api/chat_service.py b/backend/python/app/api/chat_service.py index 7a83622..b94522d 100644 --- a/backend/python/app/api/chat_service.py +++ b/backend/python/app/api/chat_service.py @@ -1612,6 +1612,20 @@ async def _send_message_core( except Exception: pass + # Phase 1: Bill tokens on stream completion + try: + _pt = prompt_tokens if "prompt_tokens" in dir() else 0 + _ct = completion_tokens if "completion_tokens" in dir() else 0 + _uid = user_id if "user_id" in dir() else "unknown" + if _pt or _ct: + import asyncio + asyncio.create_task(_bill_tokens( + _uid, _pt, _ct, + ai_model if "ai_model" in dir() else "unknown", + conv_id)) + except Exception: + pass + return {