Database

openwebui_token_tracking.db.migrate_database(database_url)[source]

Creates the tables required for token tracking in the specified database

Parameters:

database_url (str) – A database URL in SQLAlchemy format

openwebui_token_tracking.db.init_db(database_url)[source]

Initialize the database connection.

Creates and returns a SQLAlchemy database engine using the provided connection URL.

Parameters:

database_url (str) – URL for connecting to the database

Returns:

Configured SQLAlchemy database engine

Return type:

sqlalchemy.engine.Engine

Example:

>>> engine = init_db("postgresql://user:password@localhost/dbname")
class openwebui_token_tracking.db.CreditGroup(**kwargs)[source]

Bases: Base

SQLAlchemy model for the credit group table

description

Description of the credit group’s purpose or members

id

Primary key UUID for the credit group

max_credit

Maximum number of credits allocated to members of this group

name

Name of the credit group, must be unique (case-insensitive)

users

Relationship with the CreditGroupUser model, linked via CreditGroupUser.credit_group

class openwebui_token_tracking.db.CreditGroupUser(**kwargs)[source]

Bases: Base

SQLAlchemy model for the credit group user table

credit_group

Relationship with the CreditGroup, linked via CreditGroup.users

credit_group_id

ID of the credit group, references CreditGroup.id

user

Relationship with the User model, linked via User.credit_groups

user_id

ID of a member of the credit group, references User.id

class openwebui_token_tracking.db.ModelPricing(**kwargs)[source]

Bases: Base

SQLAlchemy model for the model pricing table

Stores pricing information for AI models, including credit costs for input and output tokens.

id

Identifier of the model (e.g., ‘gpt-4’, ‘claude-3’), part of the composite primary key

input_cost_credits

Number of credits charged for input tokens

name

Display name of the model

output_cost_credits

Number of credits charged for output tokens

per_input_tokens

Number of input tokens per credit charge (e.g., 1000000 tokens per input_cost_credits)

per_output_tokens

Number of output tokens per credit charge (e.g., 1000000 tokens per output_cost_credits)

provider

Provider of the AI model (e.g., ‘openai’, ‘anthropic’), part of the composite primary key

class openwebui_token_tracking.db.BaseSetting(**kwargs)[source]

Bases: Base

SQLAlchemy model for the baseline settings table

Stores global configuration settings for the token tracking system as key-value pairs. Used for system-wide settings like default credit allowances and rate limits.

description

Human-readable description of what the setting controls and its purpose

setting_key

Primary key representing the unique setting identifier

setting_value

Value of the setting stored as a string (may need conversion to appropriate type when used)

class openwebui_token_tracking.db.SponsoredAllowance(**kwargs)[source]

Bases: Base

SQLAlchemy model for the sponsored allowance table

Represents a credit allowance sponsored by an entity, which can be used by users to access specific AI models without consuming their personal credits.

base_models

Relationship with the SponsoredAllowanceBaseModels model, linked via SponsoredAllowanceBaseModels.sponsored_allowance

creation_date

Timestamp when the sponsored allowance was created

daily_credit_limit

Daily credit limit per user

id

Primary key UUID for the sponsored allowance

name

Unique name of the sponsored allowance

sponsor_id

Identifier for the entity sponsoring this allowance

total_credit_limit

Total credit limit across all users and base models, i.e., maximum sponsored amount

class openwebui_token_tracking.db.SponsoredAllowanceBaseModels(**kwargs)[source]

Bases: Base

SQLAlchemy model for the sponsored allowance base models association table

Junction table that defines which base models are available under a specific sponsored allowance.

base_model

Relationship with the ModelPricing model

base_model_id

Part of composite primary key, references ModelPricing.id

sponsored_allowance

Relationship with the SponsoredAllowance model, linked via SponsoredAllowance.base_models

sponsored_allowance_id

Part of composite primary key, references SponsoredAllowance.id

class openwebui_token_tracking.db.TokenUsageLog(**kwargs)[source]

Bases: Base

SQLAlchemy model for the token usage log table

Records individual token usage events for tracking and billing purposes. Each record represents a single API call with token consumption details.

log_date

Timestamp when the token usage occurred, part of the composite primary key

model_id

ID of the model used (e.g., ‘gpt-4’, ‘claude-3’), part of the composite primary key

prompt_tokens

Number of tokens used in the input/prompt

provider

Provider of the AI model (e.g., ‘openai’, ‘anthropic’), part of the composite primary key

response_tokens

Number of tokens generated in the output/response

sponsored_allowance_id

Optional reference to a SponsoredAllowance if the usage was covered by a sponsor

user_id

ID of the user who consumed the tokens, part of the composite primary key

class openwebui_token_tracking.db.User(**kwargs)[source]

Bases: Base

SQLAlchemy model for the user table.

Mocks (parts of) the user table managed by Open WebUI and is only used for testing purposes.

This model represents users who can consume tokens and be part of credit groups. In production, this would be replaced by the actual user model from Open WebUI.

credit_groups

Relationship with the CreditGroupUser model, linked via CreditGroupUser.user

email

User’s email address

id

Primary key identifier for the user

name

User’s display name