Skip to main content

TokenWatch by Neurify — Track, alert, and control your LLM API spending across OpenAI, Anthropic, and Gemini

Project description

🔭 TokenWatch

by Neurify

Track, alert, and control your LLM API spending — across OpenAI, Anthropic & Gemini

PyPI version Python Downloads License: MIT GitHub stars GitHub issues GitHub forks

TokenWatch is Neurify's first open-source tool — a lightweight Python package that wraps your existing LLM clients transparently, tracks every token consumed, calculates real-time costs, enforces budget limits, and fires alerts before you overspend.

Zero code changes to your existing LLM calls. Just wrap, and watch.


✨ Features

  • 🔌 Zero-change integration — wrap your existing client, all calls tracked automatically
  • 💰 Real-time cost tracking — per call, session, daily, monthly, all-time
  • 🚨 Smart alerts — console, webhook, Slack, email, custom callbacks
  • 🛡️ Budget enforcement — warn, raise error, or block when limits are hit
  • 📊 13 models supported — OpenAI, Anthropic, Claude, Gemini
  • 💾 SQLite persistence — local cost history, exportable to CSV
  • 🖥️ CLI dashboardtokenwatch report, tokenwatch history, tokenwatch models
  • Custom models — add any model with your own pricing

🚀 Installation

# All providers
pip install neurify-tokenwatch[all]

# Individual providers
pip install neurify-tokenwatch[openai]
pip install neurify-tokenwatch[anthropic]
pip install neurify-tokenwatch[gemini]

⚡ Quick Start

OpenAI

import openai
from tokenwatch import CostTracker, Budget

tracker = CostTracker(
    budget=Budget(daily_limit=1.00, alert_threshold=0.80, on_exceed="warn")
)
client = tracker.wrap_openai(openai.OpenAI(api_key="sk-..."))

# Use exactly like normal OpenAI — zero code change
response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": "Hello!"}]
)

print(f"Session cost : ${tracker.get_session_cost():.6f}")
print(f"Daily cost   : ${tracker.get_daily_cost():.6f}")

Anthropic

import anthropic
from tokenwatch import CostTracker, Budget

tracker = CostTracker(budget=Budget(daily_limit=2.00, on_exceed="warn"))
client = tracker.wrap_anthropic(anthropic.Anthropic(api_key="sk-ant-..."))

response = client.messages.create(
    model="claude-haiku-4-5",
    max_tokens=100,
    messages=[{"role": "user", "content": "Hello!"}]
)

print(f"Session cost : ${tracker.get_session_cost():.6f}")

Gemini

import google.generativeai as genai
from tokenwatch import CostTracker

genai.configure(api_key="AI...")
tracker = CostTracker()
model = tracker.wrap_gemini(genai.GenerativeModel("gemini-1.5-flash"))

response = model.generate_content("Hello!")
print(f"Session cost : ${tracker.get_session_cost():.6f}")

🛡️ Budget Enforcement

from tokenwatch import CostTracker, Budget, BudgetExceededError

# Warn mode — alert and continue
tracker = CostTracker(budget=Budget(
    daily_limit=1.00,
    monthly_limit=20.00,
    session_limit=0.50,
    alert_threshold=0.80,   # alert at 80%
    on_exceed="warn"
))

# Raise mode — throws BudgetExceededError
tracker = CostTracker(budget=Budget(session_limit=0.01, on_exceed="raise"))
try:
    client.chat.completions.create(...)
except BudgetExceededError as e:
    print(f"Over budget! Spent ${e.spent:.4f} of ${e.limit:.4f} ({e.period})")

# Block mode — stops the call entirely
tracker = CostTracker(budget=Budget(monthly_limit=50.00, on_exceed="block"))

🔔 Alert System

from tokenwatch import AlertManager, CostTracker

alert_mgr = AlertManager()

# Console (default — rich colored panels)
alert_mgr.add_console_handler(level="WARNING")

# Custom callback
def my_alert(alert_type, message, data):
    print(f"[{alert_type}] {message}")
alert_mgr.add_callback_handler(my_alert)

# Webhook (Slack, Discord, etc.)
alert_mgr.add_webhook_handler("https://hooks.slack.com/...")

# Email
alert_mgr.add_email_handler(
    smtp_config={"host": "smtp.gmail.com", "port": 587, "user": "x", "password": "y"},
    to_email="team@yourcompany.com"
)

tracker = CostTracker(alert_manager=alert_mgr, spike_threshold=0.05)

📊 Cost Queries

tracker.get_session_cost()    # current session
tracker.get_daily_cost()      # today (UTC)
tracker.get_monthly_cost()    # this month
tracker.get_total_cost()      # all time
tracker.get_summary()         # full breakdown dict
tracker.export_report("costs.csv")  # export to CSV

🎨 Usage Patterns

Decorator

@tracker.watch
def run_pipeline():
    client.chat.completions.create(...)
    client.chat.completions.create(...)

run_pipeline()
# prints: [tokenwatch] run_pipeline used $0.000081 (session total: $0.000081)

Context Manager

with CostTracker() as tracker:
    client = tracker.wrap_openai(openai.OpenAI(api_key="..."))
    client.chat.completions.create(...)
# Prints rich summary table on exit

💰 Supported Models & Pricing

Provider Model Input /1M Output /1M
OpenAI gpt-4o $2.50 $10.00
OpenAI gpt-4o-mini $0.15 $0.60
OpenAI gpt-4-turbo $10.00 $30.00
OpenAI gpt-3.5-turbo $0.50 $1.50
Anthropic claude-opus-4-6 $15.00 $75.00
Anthropic claude-sonnet-4-6 $3.00 $15.00
Anthropic claude-haiku-4-5 $0.80 $4.00
Gemini gemini-1.5-pro $1.25 $5.00
Gemini gemini-1.5-flash $0.075 $0.30
Gemini gemini-2.0-flash $0.10 $0.40

Add custom models

from tokenwatch.pricing.tables import add_custom_model
add_custom_model("openai", "gpt-5", input_price_per_1m=10.00, output_price_per_1m=30.00)

🖥️ CLI

tokenwatch report                    # spend today / month / all time
tokenwatch history --limit 20        # last 20 API calls
tokenwatch history --provider openai # filter by provider
tokenwatch models                    # all supported models + pricing
tokenwatch export --output costs.csv # export to CSV
tokenwatch clear                     # clear database

🤝 Contributing

TokenWatch is Neurify's first open-source project and we welcome contributions!

  1. Fork the repo
  2. Create your branch: git checkout -b feature/amazing-feature
  3. Commit your changes: git commit -m 'Add amazing feature'
  4. Push: git push origin feature/amazing-feature
  5. Open a Pull Request

📄 License

MIT License — see LICENSE for details.


Made with ❤️ by Neurify — Our first open-source release 🎉

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

neurify_tokenwatch-0.1.4.tar.gz (39.8 kB view details)

Uploaded Source

Built Distribution

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

neurify_tokenwatch-0.1.4-py3-none-any.whl (28.1 kB view details)

Uploaded Python 3

File details

Details for the file neurify_tokenwatch-0.1.4.tar.gz.

File metadata

  • Download URL: neurify_tokenwatch-0.1.4.tar.gz
  • Upload date:
  • Size: 39.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for neurify_tokenwatch-0.1.4.tar.gz
Algorithm Hash digest
SHA256 754655db23b82b260b517894fff207c3559ebdef8ec5959ba3e8c4a004b52cef
MD5 6ed45ec914e108d0a872adfd4ba2176a
BLAKE2b-256 34e44ec56e48a09a5a3bacd8100249178d6cf75f953cb5ebaf685f2488d9545d

See more details on using hashes here.

File details

Details for the file neurify_tokenwatch-0.1.4-py3-none-any.whl.

File metadata

File hashes

Hashes for neurify_tokenwatch-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 66d175d78696a78283b1f1f56bc8c425b75fd8691233419a5012d071456639f6
MD5 c1b554e4837547053781fe99b98f3731
BLAKE2b-256 cccea463f4364d43998bec30eee152e5427b3f39bffb5ebf7cb084c9d4f1fc28

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