Tokentoll
πΎ TokenToll: is a self-hostable billing infrastructure for LLM APIs and SaaS products. Drop-in SDKs for Node.js and Python, YAML-defined pricing plans, Stripe adapter, and a real-time usage dashboard | The open-source alternative to Lago and Orb.
Install / Use
/learn @AlameerAshraf/TokentollREADME
<a href="https://opensource.org/licenses/MIT"><img src="https://img.shields.io/badge/License-MIT-yellow.svg?style=flat-square" alt="License: MIT" /></a> <a href="https://www.python.org/"><img src="https://img.shields.io/badge/Python-3.11+-3776AB?style=flat-square&logo=python&logoColor=white" alt="Python" /></a> <a href="https://fastapi.tiangolo.com/"><img src="https://img.shields.io/badge/FastAPI-0.109+-009688?style=flat-square&logo=fastapi&logoColor=white" alt="FastAPI" /></a> <a href="https://www.typescriptlang.org/"><img src="https://img.shields.io/badge/TypeScript-5.0+-3178C6?style=flat-square&logo=typescript&logoColor=white" alt="TypeScript" /></a> <a href="https://www.mongodb.com/"><img src="https://img.shields.io/badge/MongoDB-7-47A248?style=flat-square&logo=mongodb&logoColor=white" alt="MongoDB" /></a> <a href="https://redis.io/"><img src="https://img.shields.io/badge/Redis-7-DC382D?style=flat-square&logo=redis&logoColor=white" alt="Redis" /></a> <a href="https://www.docker.com/"><img src="https://img.shields.io/badge/Docker-Ready-2496ED?style=flat-square&logo=docker&logoColor=white" alt="Docker" /></a> <a href="https://stripe.com/"><img src="https://img.shields.io/badge/Stripe-Integrated-635BFF?style=flat-square&logo=stripe&logoColor=white" alt="Stripe" /></a> <a href="http://makeapullrequest.com"><img src="https://img.shields.io/badge/PRs-Welcome-brightgreen?style=flat-square" alt="PRs Welcome" /></a>
<br /> <br />Star History
<a href="https://www.star-history.com/?repos=AlameerAshraf%2Ftokentoll&type=timeline&logscale=&legend=top-left"> <picture> <source media="(prefers-color-scheme: dark)" srcset="https://api.star-history.com/image?repos=AlameerAshraf/tokentoll&type=timeline&theme=dark&logscale&legend=top-left" /> <source media="(prefers-color-scheme: light)" srcset="https://api.star-history.com/image?repos=AlameerAshraf/tokentoll&type=timeline&logscale&legend=top-left" /> <img alt="Star History Chart" src="https://api.star-history.com/image?repos=AlameerAshraf/tokentoll&type=timeline&logscale&legend=top-left" /> </picture> </a>12 AI Providers Β· 5 Pricing Models Β· 30+ API Endpoints Β· Python & Node SDKs
π Quick Start Β· π― Features Β· π¦ SDKs Β· π API Reference Β· βοΈ Configuration Β· π³ Deployment Β· π€ Contributing
<br /></div> <br />
πΎ What is TokenToll?
The problem: You're building a SaaS on top of LLMs. You need to track usage per customer, enforce quotas, support free tiers and prepaid credits, and generate invoices. Building all that from scratch takes weeks.
The solution: TokenToll gives you a single API to handle all of it. Ingest usage events, define pricing in YAML, and let TokenToll compute costs, enforce limits, generate invoices, and sync to Stripe β or run fully self-hosted.
ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ
β β report β β invoice β β
β Your App ββββββββββΆ β TokenToll ββββββββββΆ β Customer β
β calls LLM β tokens β API β auto β gets bill β
β β β β β β
ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ
<br />
π― Features
<table> <tr> <td width="33%" valign="top">π Usage Metering
β Event ingestion via REST or SDK<br/> β Idempotent (duplicate IDs ignored)<br/> β Batch ingestion<br/> β Multi-meter (tokens, calls, compute)<br/>
π° Flexible Pricing
β
Per-unit β qty Γ rate<br/>
β
Tiered β bracket-based rates<br/>
β
Volume β total determines rate<br/>
β
Flat β fixed monthly fee<br/>
β
Prepaid β credit deduction<br/>
β
Free tiers β free allowance<br/>
π Quotas & Entitlements
β Hard limits β block at cap<br/> β Soft limits β alert via webhook<br/> β Budget caps β dollar spending limits<br/> β Alert thresholds β 80%, 100%, etc.<br/>
π§Ύ Invoicing & Payments
β Auto monthly invoice generation<br/> β Per-meter line items<br/> β Stripe sync or self-hosted<br/> β Prepaid credit auto-apply<br/> β Invoice status tracking<br/> β Retry with exponential backoff<br/>
</td> <td width="33%" valign="top">π’ Multi-Tenancy
β Full tenant isolation<br/> β Operator super-admin API<br/> β Per-tenant API key rotation<br/> β SHA-256 hashed key storage<br/>
π Webhooks & Observability
β HMAC-SHA256 signed delivery<br/> β 3 event types with auto-retry<br/> β Full audit logging<br/> β Event replay / reconciliation<br/> β Platform analytics endpoint<br/> β Rate limiting per tenant<br/>
</td> </tr> </table> <br />π€ Built-in AI Provider Pricing
TokenToll ships with pre-configured cost tables for 12 providers and 100+ models. Pass provider and model with your events β costs are calculated automatically.
<br />[!TIP] Rates are YAML files in
config/provider_pricing/β update them anytime and hot-reload without restart.
ποΈ Architecture
βββββββββββββββββββ ββββββββββββββββββββ βββββββββββββββββ
β β β β β β
β Your App ββββββββββΆβ TokenToll API ββββββββββΆβ MongoDB β
β (SDK / HTTP) β β (FastAPI) β β β
β β β β βββββββββββββββββ
βββββββββββββββββββ ββββββββββ¬ββββββββββ
β
ββββββββββΌββββββββββ
β Redis Streams β
ββββββββββ¬ββββββββββ
β
ββββββββββββββββββΌββββββββββββββββ-β
β β β
ββββββββΌβββββββ ββββββββΌβββββββ ββββββββΌβββββββ
β Ingestion β β Invoice β β Webhook β
β Worker β β Worker β β Worker β
βββββββββββββββ ββββββββ¬βββββββ βββββββββββββββ
β
βββββββββΌβββββββββ
β Stripe β
β (optional) β
ββββββββββββββββββ
| Component | Role | |:---|:---| | π API Server | FastAPI REST API β auth, rate limiting, multi-tenant routing | | β‘ Ingestion Worker | Processes usage events, updates wallets, checks entitlements | | π΅ Invoice Worker | Monthly invoice generation, Stripe sync, credit application | | π¨ Webhook Worker | HMAC-signed delivery with retries to your endpoints | | π MongoDB | Persistent storage β tenants, customers, events, invoices, audit logs | | π΄ Redis | Stream-based queue + per-tenant rate limiting |
<br />π Quick Start
[!NOTE] You need Docker & Docker Compose. Stripe is optional β without it, TokenToll runs in manual invoicing mode.
Step 1 β Start the stack
git clone https://github.com/AlameerAshraf/tokentoll.git
cd tokentoll
docker compose -f docker/docker-compose.yml up -d
Step 2 β Verify it's running
curl http://localhost:8000/health | jq
# β
{"status": "ok", "database": "ok"}
Step 3 β Create a tenant (your SaaS company)
curl -s -X POST http://localhost:8000/v1/tenants \
-H "Content-Type: application/json" \
-d '{"name": "My SaaS", "slug": "my-saas"}' | jq
Step 4 β Generate an API key
curl -s -X POST http://localhost:8000/v1/tenan
