Skip to main content

ai-cost-guard

Budget enforcement and cost tracking for LLM applications.

Stop runaway API spend from bugs, prompt injection, or retry loops — before it hits your credit card.

from ai_cost_guard import CostGuard

guard = CostGuard(weekly_budget_usd=5.00)

@guard.protect(model="anthropic/claude-haiku-4-5-20251001")
def call_claude(prompt: str):
    return client.messages.create(...)   # blocked if budget exceeded

Why this exists

When you build with LLMs, three things will eventually go wrong:

  1. A bug creates an infinite retry loop — you wake up to a $300 bill.
  2. A prompt injection attack causes your app to make thousands of unexpected calls.
  3. A junior dev accidentally calls GPT-4o instead of GPT-4o-mini in a tight loop.

ai-cost-guard is a hard stop. It tracks every LLM call, accumulates cost, and raises BudgetExceededError before the next call goes through.

Zero runtime dependencies. Pure Python stdlib. Works with any LLM provider.


Install

pip install ai-cost-guard

Or from source:

git clone https://github.com/yourusername/ai-cost-guard
cd ai-cost-guard
pip install -e ".[dev]"

Quick Start

Decorator (simplest)

from ai_cost_guard import CostGuard
import anthropic

client = anthropic.Anthropic()
guard = CostGuard(weekly_budget_usd=5.00)

@guard.protect(model="anthropic/claude-haiku-4-5-20251001", purpose="summarizer")
def summarize(text: str):
    return client.messages.create(
        model="claude-haiku-4-5-20251001",
        max_tokens=256,
        messages=[{"role": "user", "content": f"Summarize: {text}"}],
    )

Manual check + record

guard.check_budget("openai/gpt-4o", estimated_input=500, estimated_output=200)

response = openai_client.chat.completions.create(...)

guard.record(
    model="openai/gpt-4o",
    input_tokens=response.usage.prompt_tokens,
    output_tokens=response.usage.completion_tokens,
)

Dry-run mode (test without real calls)

guard = CostGuard(weekly_budget_usd=5.00, dry_run=True)
# All calls raise BudgetExceededError("DRY RUN") — safe to use in CI

CLI

# Show current spend vs budget
ai-cost-guard status

# List all calls this period
ai-cost-guard calls

# List all registered models with pricing
ai-cost-guard models

# Check if a model call would be allowed given a budget
ai-cost-guard check anthropic/claude-sonnet-4-6 5.00

# Reset the tracker
ai-cost-guard reset

Supported Providers

Provider Models
Anthropic claude-haiku-4-5, claude-sonnet-4-6, claude-opus-4-6
OpenAI gpt-4o, gpt-4o-mini, gpt-3.5-turbo
Google gemini-1.5-flash, gemini-1.5-pro
Ollama (local) qwen2.5:7b, llama3.2:3b, mistral:7b (always $0.00)

Adding a new model:

from ai_cost_guard import PROVIDERS

PROVIDERS["myprovider/mymodel"] = {
    "input":  1.00 / 1_000_000,   # per token
    "output": 4.00 / 1_000_000,
}

Security properties

  • Hard budget cap — raises BudgetExceededError before the call, not after.
  • No network calls — all data stored locally in ~/.ai-cost-guard/cost_log.json.
  • Atomic writes — cost log uses temp-file + rename to prevent corruption.
  • Zero dependencies — nothing to supply-chain attack.
  • Audit trail — every call logged with timestamp, model, tokens, and purpose.

See SECURITY.md for full security policy.


How it compares

Tool Hard budget stop Multi-provider Zero deps Local storage
ai-cost-guard ✅ ✅ ✅ ✅
LangChain callbacks ❌ (observe only) ✅ ❌ ❌
OpenAI usage limits ✅ ❌ N/A ❌
Manual tracking ❌ depends ✅ depends

Running tests

pip install -e ".[dev]"
pytest tests/ -v

Contributing

PRs welcome. Please:

  • Keep zero runtime dependencies.
  • Add tests for new providers.
  • Update pricing when providers change rates.

License

MIT — free to use, modify, and distribute.

Metadata

Release files for ai-cost-guard 0.1.0

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 0.1.0
File Size Uploaded Provenance
ai_cost_guard-0.1.0.tar.gz 10.0 kB Without provenance Details

Built distribution (wheel)

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

Total release size: 21.2 kB

Release files / ai_cost_guard-0.1.0.tar.gz

Download URL ai_cost_guard-0.1.0.tar.gz
Size 10.0 kB
Tags Source
SHA-256 checksum
How to use checksums
8b8e147e622007d2035e41132aebe1d585ca658800dc6f67aff165d2af43da3a
BLAKE2b-256 checksum
How to use checksums
1fd88aeb19bab73787d87ddba83aad088832b0bb65d73beb7ac491b74d7c26b3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.2

Release files / ai_cost_guard-0.1.0-py3-none-any.whl

Download URL ai_cost_guard-0.1.0-py3-none-any.whl
Size 11.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e39a94cb504b4305451d932294a519bcf10e9661ec373d849654141aae48ab82
BLAKE2b-256 checksum
How to use checksums
584e2e5f5d48b00ebfee2edb4ba1f8517afea30f1ff396568b75c1c03a191a63
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.2

Security for ai-cost-guard 0.1.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.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

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