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.0.tar.gz (3.4 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.0-py3-none-any.whl (3.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: kalaa-1.0.0.tar.gz
  • Upload date:
  • Size: 3.4 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.0.tar.gz
Algorithm Hash digest
SHA256 07103960391013e0d456ea25944efa22b12c167b2376985a163bbc4c83c8edf5
MD5 d90ff94ffd93d4c13e0d5c8ae07ba64e
BLAKE2b-256 3c052bfd33fc081801858a33dc7a693502e0212bcb352d1ffc29011d927db51d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: kalaa-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 3.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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fa0751ee06d136ce57a06e3aabbca4eb1ecad89d32aac83808c3e442a5999711
MD5 76f7347fedad1d7d237c8a6ebb6cccd1
BLAKE2b-256 45d9344e24e96735eff8514bc9df40f68ad093f3f81d8857fc4fb082ebaecdfc

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