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

Uploaded Python 3

File details

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

File metadata

  • Download URL: costkey-0.2.0.tar.gz
  • Upload date:
  • Size: 13.3 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.0.tar.gz
Algorithm Hash digest
SHA256 d10c5b9758496f97f579de9a0281616ae0fd980df4f895582a2b2e303d17334f
MD5 865b4082995e59148f84440f1efa6561
BLAKE2b-256 cf7caa5441465b65e651e5e3c51fd81b1c6c51ce35909cb4d30204274d529ca9

See more details on using hashes here.

File details

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

File metadata

  • Download URL: costkey-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 14.9 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 28e1451cf187e6c4d0748a95e62955508bb3b5b256a05f32fa2aaee52f69da9e
MD5 1d94948e6b52af9262d02f9fbb6503f7
BLAKE2b-256 c6cbc8d52e9a435704424209b819162be4f8281c444b158a6ccc52f081b37403

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