AI Token Monitor 🚀
Track token usage and cost for OpenRouter, Groq, and OpenAI — with FastAPI integration.
Features
- ✅ Token tracking per request and per chat session
- ✅ Cost estimation (real model pricing)
- ✅ Multi-turn chat session management
- ✅ FastAPI integration with middleware
- ✅ Production logging
Install
pip install ai-token-monitor
Quick Start
from ai_token_monitor import TokenMonitor, ChatManager
from ai_token_monitor.utils import normalize_response, extract_reply
monitor = TokenMonitor()
chat_manager = ChatManager()
chat_id = chat_manager.create_chat("user_123")
chat_manager.add_message(chat_id, "user", "Hello!")
# After getting response from OpenAI/OpenRouter:
data = normalize_response(raw_response)
answer = extract_reply(data)
monitor.track(data, model="openai/gpt-4o-mini", chat_manager=chat_manager, chat_id=chat_id)
print(monitor.summary())
Environment Variables
Copy .env.example to .env and set:
OPENROUTER_API_KEY=your_key_here
Run the FastAPI Example
uvicorn examples.fastapi_app:app --reload
Supported Models
openai/gpt-4o,openai/gpt-4o-mini,openai/gpt-3.5-turbogroq/llama3-8b-8192,groq/llama3-70b-8192mistralai/mixtral-8x7b-instruct,mistralai/mistral-7b-instructanthropic/claude-3-haiku,anthropic/claude-3.5-sonnet
License
MIT
Metadata
Release files for ai-token-monitor 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| ai_token_monitor-0.1.0.tar.gz | 8.9 kB | Without provenance | Details for ai_token_monitor-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| ai_token_monitor-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for ai_token_monitor-0.1.0-py3-none-any.whl |
Total release size: 18.3 kB
Release files / ai_token_monitor-0.1.0.tar.gz
| Download URL | ai_token_monitor-0.1.0.tar.gz |
|---|---|
| Size | 8.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3e618f0ca5f3ff71128170dcddbd2107535447270b33204759616be9d8b96200
|
|
BLAKE2b-256 checksum How to use checksums |
41f5d66039c34629c774f744d9f5701d973df8c1fa9edf3b32d771051a302ddb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.10
|
Release files / ai_token_monitor-0.1.0-py3-none-any.whl
| Download URL | ai_token_monitor-0.1.0-py3-none-any.whl |
|---|---|
| Size | 9.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
bdc68d0b1f3a0652dce1e7a50e81f22f121dfbda75b6fb036c5791e8343eb210
|
|
BLAKE2b-256 checksum How to use checksums |
c3936d37dd50b684fc179d6161133f26ae2ab7fe168a87d2aee9f9347c8cc9c3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.10
|
Security for ai-token-monitor 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.