Skip to content

Usage handler

DatabaseUserCreditsInterface

Bases: UserCreditsInterfaceProtocol

user_id = user_clerk_id instance-attribute

user_uow = user_uow instance-attribute

__init__(user_clerk_id, user_uow)

charge_credits(credits_) async

UsageHandler

Bases: AsyncCallbackHandler

Handles updating the user's usage in the database.

raise_error = True class-attribute instance-attribute

user_credits_interface = user_credits_interface instance-attribute

__init__(user_credits_interface)

Initialize the usage handler.

Note: This ALWAYS updates the user's credits in the database. No option to turn it off.

charge_completion_tokens() async

Charge the user for the completion tokens (so far).

Charges based on cost per model name Resets the completion tokens to charge for each model

charge_prompt_tokens(model_name, prompt_tokens) async

Charge the user for the prompt tokens.

on_chat_model_start(serialized, messages, *, run_id, parent_run_id=None, tags=None, metadata=None, **kwargs) async

on_llm_end(response, *, run_id, parent_run_id=None, tags=None, **kwargs) async

on_llm_error(error, *, run_id, parent_run_id=None, tags=None, **kwargs) async

on_llm_new_token(token, *, chunk=None, run_id, parent_run_id=None, tags=None, **kwargs) async

on_llm_start(serialized, prompts, *, run_id, parent_run_id=None, tags=None, metadata=None, **kwargs) async

UserCreditsInterfaceProtocol

Bases: Protocol

charge_credits(cost) async

Charge credits to user and return the remaining credits.