Skip to main content

kostlens

Track every AI call your app makes: tokens, cost, latency, and the tags that matter (feature, end user), with wrappers designed so they can't break your app.

Part of KostLens, the financial layer for your AI usage.

Install

pip install kostlens

Quickstart (2 lines)

from openai import OpenAI
from kostlens import KostLens, wrap_openai

kl = KostLens(os.environ["KOSTLENS_KEY"])
client = wrap_openai(OpenAI(), kl, tags={"feature": "chat"})

# Use `client` exactly as before. Streaming included.

Also available: wrap_anthropic and wrap_google_genai. Any OpenAI-compatible API (Groq, OpenRouter, DeepSeek, Kimi...) works through wrap_openai. Pass provider="deepseek" so pricing uses the honest vendor label.

Async clients (AsyncOpenAI, AsyncAnthropic) are supported too. Wrap them the same way and await as usual.

Tag what matters

# inside your request handler, where you know the customer
client = wrap_openai(OpenAI(), kl, tags={
    "feature": "summarize",
    "endUserId": user.id,   # camelCase, a reserved key
})

Tag keys travel verbatim: use endUserId, not end_user_id. A snake_case key is stored as a plain custom tag and the Customers page stays empty.

endUserId powers cost-per-customer; feature powers cost-per-feature.

Tags belong to the client you wrap. feature rarely changes, so wrapping once per module is fine. endUserId changes per request, so that wrap goes in the handler.

Give each set of tags its own client. Wrapping replaces client.chat.completions.create in place and marks the client, so handing the same client over again returns it untouched and every call keeps the tags it started with. The mark is what stops a second wrap from layering over the first and reporting every call once per layer. It also means one shared module-level client cannot carry a different endUserId per request: construct the client inside the handler.

The sacred rule

The SDK is designed so it can never break the host app. track() is non-blocking (the HTTP call happens on a background daemon thread), network failures and KostLens outages degrade to "some events are lost", never to an exception in your request path. Events are batched, retried with backoff on transient failures, and dropped (with an on_error callback) when permanently rejected.

Options

KostLens(
    "kl_live_...",
    "https://ingest.kostlens.com",  # default; override for self-hosted
    flush_interval=5.0,
    max_batch_size=100,
    max_buffer_size=5000,
    on_error=lambda err: logging.warning("kostlens: %s", err),
)

Call kl.shutdown() before a short-lived process exits to flush what is buffered (it is also registered with atexit).

Docs

Full documentation: kostlens.com/docs

MIT © KostLens

Download files

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

Source Distribution

kostlens-0.1.3.tar.gz (26.2 kB view details)

Uploaded Source

Built Distribution

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

kostlens-0.1.3-py3-none-any.whl (14.2 kB view details)

Uploaded Python 3

File details

Details for the file kostlens-0.1.3.tar.gz.

File metadata

  • Download URL: kostlens-0.1.3.tar.gz
  • Upload date:
  • Size: 26.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.29 {"installer":{"name":"uv","version":"0.11.29","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for kostlens-0.1.3.tar.gz
Algorithm Hash digest
SHA256 852da46548f06d47ac6a14ca2f8b7b6de6760a70304e3c8059f7c028e6b21a60
MD5 ef43d69bc0e41791ff64f4fafe4ba248
BLAKE2b-256 3a3aa8d91bbee4538f9dbdaf1a325e7b9cbbc6acf9dbc92b26c096876f7301e3

See more details on using hashes here.

File details

Details for the file kostlens-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: kostlens-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 14.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.29 {"installer":{"name":"uv","version":"0.11.29","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for kostlens-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 d25394c1ae719c58268121936b6d2940d317ae3224dc3a83e9a49b51597c7d4e
MD5 eb4111d6a0c82c6305ca5007d7632b97
BLAKE2b-256 e15351a5766af9d12a92f1b5e44e40e984ec3494a45d7e040ba41eebee3e784b

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