Systems Library / Infrastructure / How to Build an AI Cost and Usage Monitoring Dashboard
Infrastructure monitoring

How to Build an AI Cost and Usage Monitoring Dashboard

Track AI API costs and usage patterns in a centralized dashboard.

Jay Banlasan

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

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

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

Related Systems