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.1.tar.gz (13.4 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.1-py3-none-any.whl (15.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: costkey-0.2.1.tar.gz
  • Upload date:
  • Size: 13.4 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.1.tar.gz
Algorithm Hash digest
SHA256 d96cb7e73ba211fbc391899a51ef1430dd0e9844336c390b833d6be1ed42c720
MD5 d0f8aa5733c65a5cf285d441e5f398c6
BLAKE2b-256 def994b5b0c2b89895ea380a9f48e789123badc2fd214e52375e3f424aee8e62

See more details on using hashes here.

File details

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

File metadata

  • Download URL: costkey-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 15.0 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ac3cffae6090925134c1643ca3745b31d3761d0698fc22a662803a436e68921d
MD5 9dd5205a4343e384de09a521f2577e77
BLAKE2b-256 1bd6a539415a894fc8d0cf6ef9674e467448a99b67a6289b02e0f86eff7d2a44

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