AI Cost Tracker
Zero-config OpenAI API cost tracking - just one import and you're done!
Track all your OpenAI API usage automatically without modifying your existing code. Supports GPT-4, GPT-3.5, DALL-E, Sora, and more.
✨ Features
- ✅ Zero Configuration - Just one import line
- ✅ Automatic Tracking - No code changes needed
- ✅ Multi-Language Support - Python, Node.js, React, Vue, and more!
- ✅ Multi-Key Support - Track multiple API keys separately
- ✅ Real-time Costs - See costs as they happen
- ✅ SQLite Database - All data stored locally and securely
- ✅ CLI Tools - Easy-to-use command-line interface
- ✅ Easy Email Setup - Interactive wizard with SendGrid/Mailgun support (no passwords!)
- ✅ Daily Reports - Optional email summaries
🚀 Quick Start (2 Steps)
Step 1: Install
pip install ai-cost-tracker
Step 2: Add ONE Line
In your main Python file (the one that runs first):
import openai_cost_tracker.services.zero_config_tracker
That's it! All your OpenAI calls are now automatically tracked.
# Your existing code works as-is - no changes needed!
from openai import OpenAI
client = OpenAI()
response = client.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": "Hello"}]
)
# This call is automatically tracked! ✨
📊 View Your Costs
# View cost summary
openai-cost-view
# View last 7 days
openai-cost-view --days 7
# View by service
openai-cost-view --service my_service
# Export to JSON
openai-cost-view --export costs.json
📝 Examples
Flask App
# app.py
from flask import Flask
import openai_cost_tracker.services.zero_config_tracker # Add this line
app = Flask(__name__)
from openai import OpenAI
client = OpenAI()
@app.route('/chat')
def chat():
# Automatically tracked!
response = client.chat.completions.create(...)
return response
FastAPI App
# main.py
from fastapi import FastAPI
import openai_cost_tracker.services.zero_config_tracker # Add this line
app = FastAPI()
from openai import OpenAI
client = OpenAI()
@app.post("/chat")
async def chat():
# Automatically tracked!
response = await client.chat.completions.create(...)
return response
Python Script
# my_script.py
import openai_cost_tracker.services.zero_config_tracker # Add this line
from openai import OpenAI
client = OpenAI()
# All calls automatically tracked!
response = client.chat.completions.create(...)
🔧 Advanced Usage
Manual Tracking (Optional)
If you prefer manual tracking:
from openai_cost_tracker import get_tracker
from openai import OpenAI
client = OpenAI()
tracker = get_tracker()
response = client.chat.completions.create(...)
usage = response.usage
tracker.log_api_call(
api_key=os.getenv("OPENAI_API_KEY"),
api_type="chat",
model="gpt-4",
operation="create",
cost=tracker.calculate_chat_cost(
model="gpt-4",
tokens_input=usage.prompt_tokens,
tokens_output=usage.completion_tokens
),
tokens_input=usage.prompt_tokens,
tokens_output=usage.completion_tokens
)
Scan Your Codebase
Find all OpenAI usage in your project:
openai-cost-scan
# Scan specific directory
openai-cost-scan --scan-root /path/to/project
Daily Email Reports (Easy Setup!)
Set up daily email summaries with our interactive wizard:
# Install email support
pip install "ai-cost-tracker[email]"
# Run the interactive email configuration wizard
openai-cost-config-email
The wizard supports:
- SendGrid (Recommended - No password needed! Just API key)
- Mailgun (No password needed! Just API key + domain)
- Gmail/Outlook (With helpful step-by-step instructions)
Then start the monitor:
openai-cost-monitor
That's it! You'll receive daily cost reports automatically.
🌍 Multi-Language Support
Python
import openai_cost_tracker.services.zero_config_tracker # That's it!
Node.js / Express
npm install ai-cost-tracker
const { getTracker } = require('ai-cost-tracker');
const tracker = getTracker();
await tracker.trackOpenAI(response, 'gpt-4', apiKey);
React / Next.js
npm install ai-cost-tracker
import { useCostTracker, CostSummary } from 'ai-cost-tracker/react';
function App() {
const { trackOpenAI } = useCostTracker();
return <CostSummary days={7} />;
}
Other Languages
See EXAMPLES.md for Ruby, PHP, Go, Vue.js, and more!
📦 What Gets Tracked
- Chat Completions (GPT-4, GPT-3.5, etc.)
- Image Generation (DALL-E 2, DALL-E 3)
- Video Generation (Sora)
- Embeddings
- API Keys (hashed for privacy)
- Service Names (auto-detected)
- Tokens (input/output)
- Costs (calculated automatically)
💾 Data Storage
All data is stored locally in SQLite:
- Default location:
~/.openai_cost_tracker/openai_usage.db - Custom location: Set via
CostTracker(db_path="/custom/path")
🔍 CLI Commands
openai-cost-view
View cost summaries and usage statistics.
openai-cost-view [--days N] [--service NAME] [--api-key HASH] [--recent N] [--json] [--export FILE]
openai-cost-scan
Scan codebase for OpenAI usage.
openai-cost-scan [--scan-root PATH] [--output FILE]
openai-cost-monitor
Run monitoring service with email reports.
openai-cost-monitor [--config PATH] [--db-path PATH] [--no-scan]
🎯 Pricing Support
Automatically calculates costs based on current OpenAI pricing:
- GPT-4: $30/$60 per 1M tokens (input/output)
- GPT-4o: $2.50/$10 per 1M tokens
- GPT-3.5-turbo: $0.50/$1.50 per 1M tokens
- DALL-E 3: $0.04-0.08/image
- Sora 2: $0.015/second
🛠️ Configuration
Environment Variables
Optional environment variables:
# Enable verbose output
export OPENAI_COST_TRACKER_VERBOSE=1
# Custom database location
export COST_TRACKER_DB=/path/to/openai_usage.db
📚 API Reference
CostTracker
from openai_cost_tracker import CostTracker, get_tracker
tracker = get_tracker() # Get global tracker
# or
tracker = CostTracker(db_path="/custom/path") # Custom tracker
# Calculate costs
cost = tracker.calculate_chat_cost("gpt-4", tokens_input=1000, tokens_output=500)
image_cost = tracker.calculate_image_cost("dall-e-3", size="1024x1024")
video_cost = tracker.calculate_video_cost("sora-2", seconds=8)
# Get summary
summary = tracker.get_usage_summary(days=7)
🤝 Contributing
Contributions welcome! Please see our contributing guidelines.
📄 License
MIT License - see LICENSE file
📚 Documentation
- Setup Guide - Complete setup instructions for all languages
- Examples - Code examples for Python, Node.js, React, Vue, Ruby, PHP, Go
- JavaScript SDK - Full documentation for npm package
🙏 Support
- Issues: GitHub Issues
- Documentation: See docs in repository
- PyPI: https://pypi.org/project/ai-cost-tracker/
- npm: Coming soon!
📦 Installation
pip install ai-cost-tracker
🎉 Status
✅ Published to PyPI: https://pypi.org/project/ai-cost-tracker/0.1.0/
Made with ❤️ for developers who want to track their AI costs without the hassle.
Metadata
Release files for ai-cost-tracker 0.2.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 | |
|---|---|---|---|---|
| ai_cost_tracker-0.2.0.tar.gz | 113.4 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| ai_cost_tracker-0.2.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 154.9 kB
Release files / ai_cost_tracker-0.2.0.tar.gz
| Download URL | ai_cost_tracker-0.2.0.tar.gz |
|---|---|
| Size | 113.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0d54f9ccdd37b6da1b1b1329c8ca9646a14f2967a1faa7dee0d77524deaf4369
|
|
BLAKE2b-256 checksum How to use checksums |
98040f7bee9f9c79c80d206c1f6fb5154d0cba16a84699f533a71b1a8ce367c2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.10.12
|
Release files / ai_cost_tracker-0.2.0-py3-none-any.whl
| Download URL | ai_cost_tracker-0.2.0-py3-none-any.whl |
|---|---|
| Size | 41.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
f68e009357157b7637bbdd2908fcf62b9e150bbff0a865c06b3b9ca04f8835eb
|
|
BLAKE2b-256 checksum How to use checksums |
33acdcdd0af32478228560ea86b2bf1adc785d84c019209ab0429f73a3bf56e3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.10.12
|
Security for ai-cost-tracker 0.2.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.