Skip to main content

AI Cost Guard — Python SDK

Track, analyze, and optimize your AI/LLM API costs with a single line of code. Supports 50+ models from OpenAI, Anthropic, Google Gemini, Cohere, Mistral, and more.

PyPI version Python Downloads License: MIT

Why AI Cost Guard?

Problem Solution
💸 Surprise AI bills at month end Real-time cost tracking with budget alerts
🔍 Can't tell which features cost most Per-feature & per-user cost breakdowns
📊 No visibility into token usage Automatic token counting & analytics
🤝 Works only with one provider Universal support for OpenAI, Anthropic, Gemini, Cohere, Mistral

Installation

pip install ai-cost-guard-sdk

Before You Start — Get Your Free API Key

You cannot use this SDK without an API key. The key tells the server which project to save your cost data into. Getting one is free and takes less than 2 minutes.

Step 1 — Create a Free Account

Go to https://aicostguard.com and click the "Get Started" button (top-right corner).

Fill in:

  • Your name
  • Your email address
  • A password

Click "Sign Up". You land on your dashboard automatically.

Step 2 — Create a Project

A project is a container for one app. One project = one API key = one set of cost data in your dashboard.

  1. You are now on the Dashboard: https://aicostguard.com/dashboard
  2. Click "Projects" in the left sidebar
  3. Click the "New Project" button (top-right of the page)
  4. Fill in:
    • Project Name — e.g. My Chatbot App
    • Description — e.g. Tracks GPT-4o costs for my support bot
  5. Click "Create Project"
  6. Your new project card appears on the Projects page

Step 3 — Copy Your API Key

  1. On your project card, click the "API Keys" button
  2. You will see a default API key that looks like:
    acg_live_abc123def456ghi789...
    
  3. Click "Copy" to copy it to your clipboard
  4. Keep it safe — treat it like a password

💡 Tip: Click "Generate New Key" at any time to get a fresh key.


Quick Start

from ai_cost_guard import AICostGuard

guard = AICostGuard(api_key="acg_live_your_key_here")

# Track any AI API call
guard.track(
    provider="openai",
    model="gpt-4",
    input_tokens=500,
    output_tokens=150,
    latency_ms=1200,
    feature="chat",
)

# Provider-specific helpers
import openai
response = openai.chat.completions.create(model="gpt-4", messages=[...])
guard.track_openai(model=response.model, usage=response.usage)

# Don't forget to flush on shutdown
guard.shutdown()

Provider Helpers

OpenAI

guard.track_openai(model="gpt-4", usage=response.usage, feature="chat")

Anthropic

guard.track_anthropic(model="claude-3-opus", usage=response.usage, feature="analysis")

Google Gemini

guard.track_gemini(model="gemini-pro", usage=response.usage_metadata, feature="search")

Cohere

guard.track_cohere(model="command-r-plus", usage=response.meta.billed_units, feature="rag")

Configuration

guard = AICostGuard(
    api_key="acg_live_...",
    api_url="https://api.aicostguard.com/api/v1",  # Custom API URL
    debug=True,                    # Enable debug logging
    batch_events=True,             # Batch events (default: True)
    batch_interval_ms=5000,        # Batch flush interval (default: 5000ms)
    max_batch_size=50,             # Max events per batch (default: 50)
    max_retries=3,                 # Max retry attempts (default: 3)
    max_events_per_second=100,     # Rate limit (default: 100)
    default_feature="my-app",      # Default feature tag
)

Context Manager Support

from ai_cost_guard import AICostGuard

# Automatically flushes and shuts down on exit
with AICostGuard(api_key="acg_live_...") as guard:
    guard.track(provider="openai", model="gpt-4o", input_tokens=500, output_tokens=100)

Async Support

import asyncio
from ai_cost_guard import AsyncAICostGuard

async def main():
    guard = AsyncAICostGuard(api_key="acg_live_...")

    await guard.track(
        provider="openai",
        model="gpt-4o",
        input_tokens=1000,
        output_tokens=200,
        feature="async-chatbot",
    )

    await guard.shutdown()

asyncio.run(main())

FastAPI Integration

from fastapi import FastAPI
from ai_cost_guard import AICostGuard

app = FastAPI()
guard = AICostGuard(api_key="acg_live_...")

@app.post("/chat")
async def chat(prompt: str):
    import openai
    response = openai.chat.completions.create(
        model="gpt-4o",
        messages=[{"role": "user", "content": prompt}],
    )

    guard.track_openai(model=response.model, usage=response.usage, feature="chat-api")
    return {"reply": response.choices[0].message.content}

@app.on_event("shutdown")
def shutdown():
    guard.shutdown()

Django Integration

# settings.py
from ai_cost_guard import AICostGuard
AI_GUARD = AICostGuard(api_key="acg_live_...")

# views.py
from django.conf import settings

def chat_view(request):
    response = openai.chat.completions.create(model="gpt-4o", messages=[...])
    settings.AI_GUARD.track_openai(model=response.model, usage=response.usage, feature="django-chat")
    return JsonResponse({"reply": response.choices[0].message.content})

Supported Models (50+)

Provider Models
OpenAI GPT-4o, GPT-4o mini, GPT-4 Turbo, GPT-4, GPT-3.5, o3, o3-mini, o1, o1-mini
Anthropic Claude 3.5 Sonnet, Claude 3 Opus, Claude 3 Haiku, Claude 3 Sonnet
Google Gemini 1.5 Pro, Gemini 1.5 Flash, Gemini 1.0 Pro
Cohere Command R+, Command R, Command, Embed
Mistral Large, Medium, Small, Tiny

Ecosystem

Links

License

MIT

Metadata

Release files for ai-cost-guard-sdk 1.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ai-cost-guard-sdk 1.0.2
File Size Uploaded Provenance
ai_cost_guard_sdk-1.0.2.tar.gz 13.1 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for ai-cost-guard-sdk 1.0.2
File Interpreter ABI Platform
ai_cost_guard_sdk-1.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 22.5 kB

Release files / ai_cost_guard_sdk-1.0.2.tar.gz

Download URL ai_cost_guard_sdk-1.0.2.tar.gz
Size 13.1 kB
Tags Source
SHA-256 checksum
How to use checksums
ea81dff58085a4297c3674276f8724f283d2c6e9b3b76b1918bc70c7a30b1544
BLAKE2b-256 checksum
How to use checksums
f01f707fc813644ec4236408e05efafb92d785320fb8c08c804f3e1dab820864
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.5

Release files / ai_cost_guard_sdk-1.0.2-py3-none-any.whl

Download URL ai_cost_guard_sdk-1.0.2-py3-none-any.whl
Size 9.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
66ccfb71de192c4c474bdcb11aeb6e487c23abbf4756df4a4f18834f0367a9a4
BLAKE2b-256 checksum
How to use checksums
02145160086bfa911e43f0639f3bb51f724ab362a220201f8a4b89ce89ec5aa6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.5

Security for ai-cost-guard-sdk 1.0.2

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.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page