This repository contains another implementation of manifold functions for OpenWebUI integration with major providers (Anthropic, OpenAI, etc), with usage volumetrics and costs tracking.
To track costs for all providers, you need to DISABLE the OpenWebUI built-in OpenAI integration and only rely on these functions.
While OpenWebUI Community has earlier developed a few usage costs tracking Filter functions (example: https://openwebui.com/f/bgeneto/cost_tracker or https://openwebui.com/f/infamystudio/gpt_usage_tracker) they all suffer from common flaws, such as not capturing the usage via APi. OpenWebUI Filter Functions 'outflow' method is only called from the OpenWebUI-specific /chat/completed endpoint, that is only triggered by the Web UI. It is not part of OpenAI-compatible API specs. This method (and therefore any Filter outflow methods) would not be called by any OpenAI-compatible 3rd party tools leveraging OpenWebUI's API.
This implementation fully relies on Pipes (Manifolds) to wrap around the responses streaming and capture usage in a reliable way, so both Web UI and API usage is captured in a similar way. For scalability, usage data is captured to the main database (SQLite or Postgre). For convenient costs retrieval we provide a chatbot interface presenting itself as another model ("Usage Reporting Bot")
- Batch and Streaming completions leverages official OpenAI and Anthropic SDKs
- Usage data logged to DB new table usage_costs in the main SQLite or Postgres DB
- Usage tracking works both for UI and API usage tracking DOES NOT depend on /chat/completed calls
- Calculates usage costs pricing data statically defined in a standalone function module, edit it as needed
- Displays status message with token counts, estimated costs, request status
- Built-in reporting on usage costs with 'Usage Reporting Bot' model pipe. Talk to it via chat
- Python 3.11+
- OpenWebUI v0.4.0+
- In Open WebUI, manually create functions with IDs matching the .py file names from this repo (src/functions). You may reference the screenshot below. Сopy&paste the code each of the Python script.
- All "module_*" functions must to be deployed, and the ID must match the .py file name precisely (ex: module_openai_compatible_pipe). Other pipe functions in this repo are referencing these shared modules by their ID (hardcoded), so all modules must be deployed with an exact ID.
- Provider-specific functions (openai, anthropic, google, databricks, etc.) are at your discretion - you may only deploy those that you need
- Double-check function IDs before saving! Any time you typed or changed the function name (when creating it for the first time), OpenWebUI automatically adjusts the ID. Make sure to fix the ID before saving the function. If you saved the function with a wrong ID, or if you are getting "function_module_... not found" error, recreate the module function with the correct ID.
- Do not forget to also deploy the src/functionsdata/module_usage_tracking_pricing_data.py as one more function with "module_usage_tracking_pricing_data" ID.
- "Usage Reporting Bot" function is optional, but it provides convenient access to users and administrators to view their accumulated usage costs so you may want to deploy it. After deploying usage reporting bot, you may want to go to Workspace - Models section and provide description to this bot "model", and also provide custom prompt suggestions like "/help" and "/usage_stats 30d".
- Enable all deployed functions, and configure Valves to provide API keys where applicable
1. Clone the repo locally
2. (Optional) Activate virtual pyenv
3. Install dependencies:
pip install -r requirements.txt
4. Create .env
file with connection detials
#Your OpenWebUI URL, e.g. http://localhost:3000
OPENWEBUI_API_URL=http://
#Get your API key in OpenWebUI User Settings -> Account -> API Keys
OPENWEBUI_API_KEY=sk-....
5. Deploy pipe functions and modules to OpenWebUI (except Pricing Data):
python deploy_to_openwebui.py src/functions/*
6. Separately deploy one more function (Pricing Data module):
python deploy_to_openwebui.py src/functions/data/module_usage_tracking_pricing_data.py
7. In OpenWebUI Enable all functions and configure Valves
Provide API keys where applicable
For OpenAI also provide comma-separated list of enabled models