Skip to main content

Getting Started

CostKey is an SDK that auto-tracks every AI API call's cost, tokens, and latency. One line of code, zero wrappers, zero proxies.

Install

pip install costkey

Initialize

Get your DSN from the CostKey dashboard or by running costkey init my-project.

import costkey

costkey.init(
dsn="https://ck_your_key@app.costkey.dev/your_project_id",
release="v1.0.0", # Track cost per deploy
)

That's it

Every AI API call your app makes is now tracked automatically. No wrappers, no decorators, no code changes.

import costkey
from anthropic import Anthropic

costkey.init(dsn="https://ck_your_key@app.costkey.dev/your_project_id")

client = Anthropic()

# This call is automatically tracked — cost, tokens, latency, stack trace
response = client.messages.create(
model="claude-sonnet-4-5-20250514",
max_tokens=1024,
messages=[{"role": "user", "content": "Explain quantum computing in one paragraph"}],
)
print(response.content[0].text)

Open the dashboard and you'll see:

  • Cost per call, calculated server-side from the model and token count
  • Token usage (input, output, cache, reasoning tokens)
  • Latency and streaming metrics (TTFT, tokens/sec)
  • Stack trace showing which function in your code made the call
  • Call site grouping so you can see which parts of your code cost the most
  • Body capture — inspect the exact prompt and completion for any call
  • Cost per deploy — set release to see how costs change across deployments
  • Executive summary — a plain-English summary of your AI spend for stakeholders

Add context

Tag calls with metadata to slice costs by feature, team, or user.

with costkey.with_context(task="summarize", team="search", user_id="u_123"):
response = client.messages.create(
model="claude-sonnet-4-5-20250514",
max_tokens=1024,
messages=[{"role": "user", "content": f"Summarize: {document}"}],
)

Group calls into traces

When a single user action triggers multiple AI calls, group them with start_trace.

with costkey.start_trace(name="POST /api/search"):
intent = classify_intent(query) # AI call 1
results = search(query, intent) # AI call 2
summary = summarize(results) # AI call 3
# All 3 calls appear as one trace in the dashboard

Shut down cleanly

Before your process exits, flush any pending events.

costkey.shutdown()

Next steps