How to Build an AI Cost and Usage Monitoring Dashboard
Track AI API costs and usage patterns in a centralized dashboard.
Jay Banlasan
The AI Systems Guy
An AI cost usage monitoring dashboard is non-negotiable once you run more than two AI-powered systems. I learned this the hard way when a runaway loop burned through $40 of API credits in an hour. Now every project gets cost tracking from day one.
This system logs every API call, tracks token usage and spend per project, and alerts you before costs spike.
What You Need Before Starting
- Python 3.8+
- SQLite (built into Python)
- API keys for the AI services you use (OpenAI, Anthropic, etc.)
- A wrapping layer around your API calls
Step 1: Create the Cost Tracking Database
import sqlite3
def init_cost_db(db_path="ai_costs.db"):
conn = sqlite3.connect(db_path)
conn.execute("""
CREATE TABLE IF NOT EXISTS api_usage (
id INTEGER PRIMARY KEY AUTOINCREMENT,
provider TEXT,
model TEXT,
project TEXT,
input_tokens INTEGER,
output_tokens INTEGER,
cost_usd REAL,
timestamp TEXT
)
""")
conn.commit()
conn.close()
Step 2: Build the API Wrapper
Wrap your AI calls to automatically log usage:
import anthropic
from datetime import datetime
PRICING = {
"claude-sonnet-4-20250514": {"input": 3.00, "output": 15.00},
"claude-opus-4-20250514": {"input": 15.00, "output": 75.00},
"gpt-4o": {"input": 2.50, "output": 10.00},
}
def tracked_claude_call(prompt, model="claude-sonnet-4-20250514", project="default", db_path="ai_costs.db"):
client = anthropic.Anthropic()
response = client.messages.create(
model=model,
max_tokens=1024,
messages=[{"role": "user", "content": prompt}]
)
input_tokens = response.usage.input_tokens
output_tokens = response.usage.output_tokens
rates = PRICING.get(model, {"input": 0, "output": 0})
cost = (input_tokens * rates["input"] + output_tokens * rates["output"]) / 1_000_000
conn = sqlite3.connect(db_path)
conn.execute(
"INSERT INTO api_usage (provider, model, project, input_tokens, output_tokens, cost_usd, timestamp) VALUES (?,?,?,?,?,?,?)",
("anthropic", model, project, input_tokens, output_tokens, round(cost, 6), datetime.utcnow().isoformat())
)
conn.commit()
conn.close()
return response.content[0].text
Step 3: Build Summary Queries
def get_daily_spend(days=7, db_path="ai_costs.db"):
conn = sqlite3.connect(db_path)
rows = conn.execute("""
SELECT DATE(timestamp) as day, SUM(cost_usd) as total, SUM(input_tokens + output_tokens) as tokens
FROM api_usage
WHERE timestamp > datetime('now', ?)
GROUP BY DATE(timestamp)
ORDER BY day DESC
""", (f"-{days} days",)).fetchall()
conn.close()
return rows
def get_spend_by_project(days=30, db_path="ai_costs.db"):
conn = sqlite3.connect(db_path)
rows = conn.execute("""
SELECT project, SUM(cost_usd) as total, COUNT(*) as calls
FROM api_usage
WHERE timestamp > datetime('now', ?)
GROUP BY project
ORDER BY total DESC
""", (f"-{days} days",)).fetchall()
conn.close()
return rows
Step 4: Add Budget Alerts
import requests
DAILY_BUDGET = 5.00
def check_budget(db_path="ai_costs.db"):
conn = sqlite3.connect(db_path)
row = conn.execute("""
SELECT SUM(cost_usd) FROM api_usage
WHERE DATE(timestamp) = DATE('now')
""").fetchone()
conn.close()
today_spend = row[0] or 0
if today_spend > DAILY_BUDGET * 0.8:
requests.post("YOUR_SLACK_WEBHOOK", json={
"text": f"AI spend alert: ${today_spend:.2f} today (budget: ${DAILY_BUDGET:.2f})"
})
return today_spend
Step 5: Create the Dashboard Endpoint
from fastapi import FastAPI
app = FastAPI()
@app.get("/dashboard/ai-costs")
def ai_cost_dashboard():
return {
"daily_spend": get_daily_spend(7),
"by_project": get_spend_by_project(30),
"today": check_budget()
}
What to Build Next
Add per-model breakdowns so you can see which models eat the most budget. Then add a circuit breaker that pauses non-critical projects when you hit 90% of your daily cap.
Related Reading
- The AI Operations Budget Template - how to plan and track AI spending
- Building a Reporting Dashboard from Scratch - dashboard design patterns
- Why Monitoring Is Not Optional - the monitoring mindset for AI systems
Want this system built for your business?
Get a free assessment. We will map every system your business needs and show you the ROI.
Get Your Free Assessment