Python SDK for the AICostManager API
Project description
AICostManager Python SDK
The AICostManager SDK reports AI usage to AICostManager, helping you track costs across providers.
Prerequisites
- Create a free account at aicostmanager.com and generate an API key.
- Export the key as
AICM_API_KEYor pass it directly to the client or tracker.
Installation
uv (recommended)
uv pip install aicostmanager
# or add to an existing project
uv add aicostmanager
pip (fallback)
pip install aicostmanager
Quick start
Identify the API and service
Every usage event is tied to two identifiers:
- api_id – the API being called (for example, the OpenAI Chat API)
- service_key – the specific model or service within that API
- Visit the service lookup page and
open the APIs tab. Copy the
api_idfor the API you are using, e.g.openai_chat. - Switch to the Services tab on the same page and copy the full
service_keyfor your model, e.g.openai::gpt-5-mini.
Track usage
from aicostmanager import Tracker
api_id = "openai_chat" # copied from the APIs tab
service_key = "openai::gpt-5-mini" # copied from the Services tab
with Tracker() as tracker:
tracker.track(api_id, service_key, {
"input_tokens": 10,
"output_tokens": 20,
})
Using with Tracker() ensures the background delivery queue is flushed before
the program exits.
Configuration values are read from an AICM.INI file. See
config.md for the complete list of available settings and
their defaults.
LLM wrappers
Wrap popular LLM SDK clients to record usage automatically without calling
track manually:
from aicostmanager import OpenAIChatWrapper
from openai import OpenAI
client = OpenAI()
wrapper = OpenAIChatWrapper(client)
resp = wrapper.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Say hello"}],
)
print(resp.choices[0].message.content)
wrapper.close() # optional for immediate delivery; required for queued delivery
See LLM wrappers for the full list of supported providers and advanced usage.
Choosing a delivery strategy
Tracker supports multiple delivery components via DeliveryType:
- Immediate – send each record synchronously. Ideal for simple scripts or tests.
- Persistent queue (
DeliveryType.PERSISTENT_QUEUE) – durable SQLite-backed queue for reliability across restarts.
Use the persistent queue for long-running services where losing usage data is unacceptable and immediate delivery when every call can block on the API. See Persistent Delivery and the Tracker guide for details.
For real-time insight into the persistent queue, run the queue-monitor
command against the SQLite database created by PersistentDelivery:
uv run queue-monitor ~/.cache/aicostmanager/delivery_queue.db
Tracking in different environments
Python scripts
Use the context manager shown above to automatically flush the queue.
Django
# myapp/apps.py
from django.apps import AppConfig
from aicostmanager import Tracker
tracker = Tracker()
class MyAppConfig(AppConfig):
name = "myapp"
def ready(self):
import atexit
atexit.register(tracker.close)
# myapp/views.py
from .apps import tracker
def my_view(request):
tracker.track("openai", "gpt-4o-mini", {"input_tokens": 10})
...
For a full setup guide, see Django integration guide.
FastAPI
from fastapi import FastAPI
from aicostmanager import Tracker
app = FastAPI()
@app.on_event("startup")
async def startup() -> None:
app.state.tracker = Tracker()
@app.on_event("shutdown")
def shutdown() -> None:
app.state.tracker.close()
For a full setup guide, see FastAPI integration guide.
Streamlit
import streamlit as st
from aicostmanager import Tracker
import atexit
@st.cache_resource
def get_tracker():
tracker = Tracker()
atexit.register(tracker.close)
return tracker
tracker = get_tracker()
if st.button("Generate"):
tracker.track("openai", "gpt-4o-mini", {"input_tokens": 10})
For a full setup guide, see Streamlit integration guide.
Celery
from celery import Celery
from aicostmanager import Tracker
from celery.signals import worker_shutdown
app = Celery("proj")
tracker = Tracker()
@app.task
def do_work():
tracker.track("openai", "gpt-4o-mini", {"input_tokens": 10})
@worker_shutdown.connect
def close_tracker(**_):
tracker.close()
For very short tasks, use with Tracker() as tracker: inside the task
to ensure flushing.
More documentation
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file aicostmanager-0.1.28.tar.gz.
File metadata
- Download URL: aicostmanager-0.1.28.tar.gz
- Upload date:
- Size: 59.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
503dd2639f8c4a41f7ea2cdf9a358c1c434055b5e625cdca7759391b6a75bdc6
|
|
| MD5 |
a308651e570e17067f876773004b05a4
|
|
| BLAKE2b-256 |
b025087fa5725dbd1674d6b133049a4ea3a7aa878d5b863a23af204d863afb25
|
File details
Details for the file aicostmanager-0.1.28-py3-none-any.whl.
File metadata
- Download URL: aicostmanager-0.1.28-py3-none-any.whl
- Upload date:
- Size: 49.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
05084c6580198613dd23bd56fc2856d561735b75e7fafe6eb0ba6f6655b38ce2
|
|
| MD5 |
31816fc197639aeed31d17c7cbace814
|
|
| BLAKE2b-256 |
ca5522ff5eb7b9c0068ab0b7bae8cbfd8b7f0a9f4ff37ad6b170eb603a624d26
|