Skip to main content

Track AI API token costs locally. Know your costs before the bill arrives.

Project description

Token Cost Guard

Know your AI costs before the bill arrives.

Track OpenAI and Anthropic API token costs locally. Never be surprised by runaway costs again.

The Problem

Building with AI APIs is amazing until you get the bill.

  • No visibility into token consumption until charges appear
  • Experiments cost money (sometimes thousands)
  • No circuit breakers or alerts
  • Manual tracking is tedious

Token Cost Guard solves this with local-first cost tracking that never sends data to the cloud.

Features

Local SQLite - Your token data never leaves your machine
One-line setup - client = track(OpenAI())
Real-time tracking - Logs every API call automatically
Cost breakdown - View costs by model, day, and session
Threshold alerts - Slack/Discord webhooks when you hit limits
CSV export - Share reports with your team

Installation

pip install git+https://github.com/alexcalderado/token-cost-guard.git

Note: If the token-guard command is not found after installation, add Python scripts to your PATH:

export PATH="$HOME/Library/Python/3.9/bin:$PATH"  # macOS
# or 
export PATH="$HOME/.local/bin:$PATH"  # Linux

Add this line to your shell profile (.zshrc, .bashrc, etc.) to make it permanent.

Quick Start

1. Initialize configuration:

token-guard init

2. Set your API keys:

export OPENAI_API_KEY="sk-..."
export ANTHROPIC_API_KEY="sk-ant-..."

3. Wrap your client (one-line change):

from token_guard import track
from openai import OpenAI

# Before: client = OpenAI()
# After:
client = track(OpenAI())

response = client.chat.completions.create(
    model="gpt-4",
    messages=[{"role": "user", "content": "Hello"}]
)
# Automatically logged to ~/.token-guard/guard.db

4. Check your costs:

token-guard report

Usage

View Cost Summary

token-guard report

╭──────────────────────────────╮
│  Token Cost Guard  Report   │
╰──────────────────────────────╯

Today's Cost:    $2.14
Last 30 Days:    $47.82

Top Models:
  gpt-4...................... $35.20
  claude-opus-4-5............. $12.62

Export to CSV

token-guard export --days 7
# Creates: token-cost-guard-export-2026-02-15.csv

Set Cost Alerts

token-guard alerts add --type daily_cost --threshold 10 \
  --webhook https://hooks.slack.com/services/YOUR/WEBHOOK

When daily costs exceed $10, you'll get a Slack notification.

View Model Pricing

token-guard config --pricing

Model Pricing (per 1M tokens):
  claude-sonnet-4-5................. $3.00 input, $15.00 output
  gpt-4o............................ $2.50 input, $10.00 output
  gpt-5.2........................... $1.75 input, $14.00 output

How It Works

Token Cost Guard wraps your AI client with a function that:

  1. Calls the original API (no proxying, same latency)
  2. Logs input/output tokens to local SQLite
  3. Calculates cost based on model pricing
  4. Returns the response unchanged

If logging fails, the API call still succeeds (graceful degradation).

Supported Models

  • Anthropic: claude-opus-4-5, claude-sonnet-4-5, claude-haiku-4-5
  • OpenAI: gpt-5.2, gpt-5-mini, gpt-4.1, gpt-4.1-mini, gpt-4o

Privacy

No cloud sync
No accounts
No third-party services

Your API cost data stays on your machine. Pricing is hardcoded and can be customized in ~/.token-guard/pricing.json.

v2 Roadmap

  • Async/streaming support
  • Local web dashboard
  • Cost forecasting
  • Optimization suggestions

Support


Made by @AlexCalderAI · MIT License

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

token_cost_guard-0.1.0.tar.gz (32.3 kB view details)

Uploaded Source

Built Distribution

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

token_cost_guard-0.1.0-py3-none-any.whl (14.1 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for token_cost_guard-0.1.0.tar.gz
Algorithm Hash digest
SHA256 229ecc6d27a616829bdd8221b7647ac3b6bb1c6483975806f1d30c75d09a8d1c
MD5 aa7c0586a88ed52ca141c2b514211412
BLAKE2b-256 55ddad26b678bc26ec5ec1e8e19a1a15ea7a83909957f413182ecb070230c364

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for token_cost_guard-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 638d0f6d9ac9e37eb03b85e29f61c2ce5d3c8299259791f4035c4c2ffa3a7269
MD5 b2b2204394f47fd9511f7f0d7e0089dd
BLAKE2b-256 8e6e44a53f8393fc377496a2228c8c5e769c8eeabab6a474ee8475c0d4bbee1e

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