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.1.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.1-py3-none-any.whl (11.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: kalaa-1.0.1.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.1.tar.gz
Algorithm Hash digest
SHA256 aaea9edf23d7fc8745157223bfa68500a8ddcdd4e5de0ad4ef64677f16a32244
MD5 9927107b8611ded552d3ebea1a1c079a
BLAKE2b-256 61e0aa22d0d11d636859ac34a61e8be0bd8181bc1f2927af7219704fecaf9ddd

See more details on using hashes here.

File details

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

File metadata

  • Download URL: kalaa-1.0.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e5038b073fbc0b136b8b095b79377a233d5b3f08f33853726a65688ae9001b1a
MD5 3fe8645ad1c92fc5d0b0c45b7be51c38
BLAKE2b-256 992a34154739dd95e9c4002f4a008492fe093947d47411ad8a35a83fcb85a20d

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