Observed since 21 Oct, 10:19 UTC
No superseded metadata states collected. Unchanged observations do not create new revisions.
Python SDK for the AICostManager API
The AICostManager SDK reports AI usage to AICostManager, helping you track costs across providers.
AICM_API_KEY or pass it directly to the client or
tracker.uv pip install aicostmanager
# or add to an existing project
uv add aicostmanager
pip install aicostmanager
Every usage event is tied to two identifiers:
api_id for the API you are using, e.g.
openai_chat.service_key for your model, e.g. openai::gpt-5-mini.from aicostmanager import Tracker
service_key = "openai::gpt-5-mini" # copied from the Services tab
with Tracker() as tracker:
tracker.track(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.
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.
Tracker supports multiple delivery components via DeliveryType:
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.
/track responsesThe /track endpoint now distinguishes between ingestion and background
processing. Immediate delivery still returns the first result item, but the
payload may not include cost_events right away. Instead, check the
status field to understand how the event will be processed:
| Status | Meaning |
|---|---|
queued | The service key is recognised and the event has been queued for processing. |
completed | Processing finished synchronously (legacy servers may still return cost events immediately). |
error | The event failed processing and includes descriptive errors. |
service_key_unknown | The service key is not recognised; the event is quarantined for review. |
Unknown services now produce a friendly error message, for example:
{
"response_id": "resp-456",
"status": "service_key_unknown",
"errors": [
"Service key 'unknown::service' is not recognized. Event queued for review."
]
}
Existing integrations should branch on result.status and treat
service_key_unknown differently from error. See the
tracker documentation for detailed
guidance and migration tips.
For real-time insight into the persistent queue, run the queue-monitor
command against the SQLite database created by PersistentDelivery:
uv run queue-monitor ~/.config/aicostmanager/queue.db
Use the context manager shown above to automatically flush the queue.
# 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.
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.
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.
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.
Download counts and popularity will appear here as the collector receives them.
| Version | Published | Direct dependencies | Open version |
|---|---|---|---|
| 0.3.1 | 23 Feb 2026 | 9 |
This describes our collected data; it is not a security assessment.
Shared repository data · GitHub
Observed since 21 Oct, 10:19 UTC
No superseded metadata states collected. Unchanged observations do not create new revisions.
package:0a2a5d354d373f6ec0b2ae42c0f5cb901010f1662b35a9aa0a8154d155950290