Skip to main content

AI cost observability. Track every LLM call's cost, tokens, and latency with one line of code.

Project description

costkey

AI cost observability. Track every LLM call's cost, tokens, and latency with one line of code.

Install

pip install costkey

Quick Start

import costkey

costkey.init(dsn="https://ck_your_key@app.costkey.dev/your-project")

# That's it. Every AI call is now tracked automatically.
# Works with OpenAI, Anthropic, Google Gemini, Azure OpenAI.

No wrapping. No per-client setup. No manual tagging. CostKey patches httpx and requests and auto-detects AI provider calls.

What You Get (Zero Config)

All of these work automatically after init():

  • Cost tracking — per-call cost computed from built-in pricing (30+ models)
  • Stack trace attribution — see which function, file, and line made each AI call
  • Request tracing — group AI calls per request with start_trace()
  • Feature detection — call chains are analyzed to detect logical "features" in your code
  • Credential scrubbing — API keys, JWTs, and secrets are auto-redacted from captured bodies

How It Works

CostKey patches httpx.Client.send and requests.Session.send. When your code calls any AI provider:

  1. Detects the provider from the URL (OpenAI, Anthropic, Google, Azure)
  2. Extracts token usage from the response
  3. Captures a stack trace for automatic code attribution
  4. Computes cost using built-in model pricing
  5. Ships the event async to your CostKey dashboard

Non-AI HTTP calls pass through untouched with zero overhead.

Tracing

# Group all AI calls in a request into one trace
with costkey.start_trace(name="POST /api/search"):
    intent = classify_intent(query)        # AI call 1
    results = search(query, intent)
    summary = summarize_results(results)   # AI call 2
    reranked = rerank_results(results)     # AI call 3

# Dashboard shows one trace with all 3 calls and total cost

Manual Context

with costkey.with_context(task="summarize", team="search"):
    response = openai.chat.completions.create(...)

Privacy & Security

  • Never captures API keys — request headers are never read
  • Auto-scrubs credentials from captured bodies (OpenAI keys, JWTs, tokens, etc.)
  • before_send hook for custom PII scrubbing:
def scrub(event):
    event.request_body = None  # strip prompts entirely
    return event

costkey.init(dsn="...", before_send=scrub)

Supported Providers

Provider Auto-detected Patches
OpenAI api.openai.com httpx, requests
Anthropic api.anthropic.com httpx, requests
Google Gemini generativelanguage.googleapis.com httpx, requests
Azure OpenAI *.openai.azure.com httpx, requests
Google Vertex AI *-aiplatform.googleapis.com httpx, requests

API

costkey.init(dsn, **options) — Initialize. Call once at startup.

costkey.with_context(**kwargs) — Context manager for custom tags.

costkey.start_trace(name) — Context manager for request tracing.

costkey.shutdown() — Flush and restore original HTTP clients.

costkey.register_extractor(extractor) — Add custom AI provider.

costkey.register_pricing(model, ...) — Add custom model pricing.

Also available for TypeScript

npm install costkey
import { CostKey } from 'costkey'
CostKey.init({ dsn: 'https://ck_...@app.costkey.dev/proj' })

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

costkey-0.2.2.tar.gz (14.7 kB view details)

Uploaded Source

Built Distribution

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

costkey-0.2.2-py3-none-any.whl (15.1 kB view details)

Uploaded Python 3

File details

Details for the file costkey-0.2.2.tar.gz.

File metadata

  • Download URL: costkey-0.2.2.tar.gz
  • Upload date:
  • Size: 14.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for costkey-0.2.2.tar.gz
Algorithm Hash digest
SHA256 04123829e0d67e76dc37f3d454ad15aa03c4ac385a24b82ccdb82acaa911a3f5
MD5 d965e4611ed7d049399e9877a3cc0c91
BLAKE2b-256 874bed47b77e74464514ce3c471f4356bd2de9483484219c23a7b0ef7d921acf

See more details on using hashes here.

File details

Details for the file costkey-0.2.2-py3-none-any.whl.

File metadata

  • Download URL: costkey-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 15.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for costkey-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 7eb66a7bb4168859f872540fdcb4fc9b829d3ff245979468d38b3b51d1fd77f2
MD5 b473a9ba4db82e60766548ad4cf9c70a
BLAKE2b-256 f359e1c61feb031f85b0d1cb5f6f40799f373802f35c9d6ed5162dc31af37bc3

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