Skip to main content

Drop-in AI cost tracking for Python. Auto-instruments OpenAI, Anthropic, and Gemini with zero code at the call site.

Project description

kalaa

Drop-in AI cost tracking for Python. Patches the OpenAI, Anthropic, and Gemini client libraries at the class level, so every client you create — before or after observe(), sync or async — is already instrumented.

Install

pip install kalaa

Setup

import kalaa
kalaa.observe()
app = kalaa.init_app(app)   # auto-detects Flask vs. FastAPI

That's it. Put KALAA_KEY=cm_live_xxx in your environment and every OpenAI, Anthropic, and Gemini call your app makes from here on is tracked automatically — including the user who made the request, with zero extra code, if you're running Flask or FastAPI.

If init_app doesn't recognize your setup, wire the right middleware directly:

# Flask / any WSGI app:
app.wsgi_app = kalaa.wsgi_middleware(app.wsgi_app)

# FastAPI / Starlette / any ASGI app:
app = kalaa.asgi_middleware(app)

Tagging a user manually

Automatic detection covers most setups (reads X-User-Id, a decoded JWT, or a client-side anonymous id). If yours doesn't fit that shape:

kalaa.set_user(current_user.id)

Background jobs / cron

No request to hang a route off of, so give it a label instead:

with kalaa.with_context("nightly-digest"):
    run_the_job()

What's tracked automatically

  • OpenAI: chat completions, sync and async, including streaming
  • Anthropic: messages, sync and async, including streaming
  • Gemini: generate_content, sync and async (via the current google.genai package — the older google.generativeai package is deprecated and not supported)
  • Whisper/audio transcription duration, read from the file itself and parsed in an isolated subprocess with a timeout — a malformed file can never hang your app, only cost a few seconds and an unmeasured duration

Configuration

Option Env var Default
key KALAA_KEY
endpoint KALAA_ENDPOINT https://api.kalaa.cc/api/ingest
env KALAA_ENV production
debug KALAA_DEBUG=1 false

Failure behavior

This package never touches a database and never sits in the path of your AI calls. If the ingest endpoint is unreachable, events are buffered and retried on the next flush cycle — your app is never blocked or crashed by a tracking failure.

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

kalaa-1.0.2.tar.gz (12.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

kalaa-1.0.2-py3-none-any.whl (11.3 kB view details)

Uploaded Python 3

File details

Details for the file kalaa-1.0.2.tar.gz.

File metadata

  • Download URL: kalaa-1.0.2.tar.gz
  • Upload date:
  • Size: 12.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for kalaa-1.0.2.tar.gz
Algorithm Hash digest
SHA256 64ca5ccd9e51f3f7da9bfe7a3ca32366cd61df5448ad862e5ef90e170124a668
MD5 e224b02ed07b230c15507ce5575cfc0b
BLAKE2b-256 5c4852fbb41f85a62c49a31421a1182f688d124bacc8a2880c9f576034e3b06b

See more details on using hashes here.

File details

Details for the file kalaa-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: kalaa-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 11.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for kalaa-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 109f5f4c7beb44ec17f5876c470af1db2b55e0045d7b0a4375387f5eb693afa1
MD5 b77b7ba3c110f94433379f17b5505665
BLAKE2b-256 c6ccd5806c506b6ac3bb14a58acd38d41cdada4a67dfbb10bdb76eb36181b8da

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page