Skip to main content

AI Token & Cost Monitoring SDK for OpenAI, OpenRouter, and Groq

Project description

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-turbo
  • groq/llama3-8b-8192, groq/llama3-70b-8192
  • mistralai/mixtral-8x7b-instruct, mistralai/mistral-7b-instruct
  • anthropic/claude-3-haiku, anthropic/claude-3.5-sonnet

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ai_token_monitor-0.1.0.tar.gz (8.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ai_token_monitor-0.1.0-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

Details for the file ai_token_monitor-0.1.0.tar.gz.

File metadata

  • Download URL: ai_token_monitor-0.1.0.tar.gz
  • Upload date:
  • Size: 8.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for ai_token_monitor-0.1.0.tar.gz
Algorithm Hash digest
SHA256 3e618f0ca5f3ff71128170dcddbd2107535447270b33204759616be9d8b96200
MD5 ef0b9be411d50e3ce61971f4e40a3991
BLAKE2b-256 41f5d66039c34629c774f744d9f5701d973df8c1fa9edf3b32d771051a302ddb

See more details on using hashes here.

File details

Details for the file ai_token_monitor-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for ai_token_monitor-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bdc68d0b1f3a0652dce1e7a50e81f22f121dfbda75b6fb036c5791e8343eb210
MD5 df0d5fa61dd0018bf7c1eb4a4e9c4d49
BLAKE2b-256 c3936d37dd50b684fc179d6161133f26ae2ab7fe168a87d2aee9f9347c8cc9c3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page