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
- Python
- TypeScript
pip install costkey
npm install costkey
Initialize
Get your DSN from the CostKey dashboard or by running costkey init my-project.
- Python
- TypeScript
import costkey
costkey.init(
dsn="https://ck_your_key@app.costkey.dev/your_project_id",
release="v1.0.0", # Track cost per deploy
)
import { CostKey } from '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.
- Python
- TypeScript
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)
import { CostKey } from 'costkey'
import OpenAI from 'openai'
CostKey.init({ dsn: 'https://ck_your_key@app.costkey.dev/your_project_id' })
const openai = new OpenAI()
// This call is automatically tracked — cost, tokens, latency, stack trace
const response = await openai.chat.completions.create({
model: 'gpt-4o',
messages: [{ role: 'user', content: 'Explain quantum computing in one paragraph' }],
})
console.log(response.choices[0].message.content)
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
releaseto 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.
- Python
- TypeScript
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}"}],
)
await CostKey.withContext({ task: 'summarize', team: 'search', userId: 'u_123' }, async () => {
const response = await openai.chat.completions.create({
model: 'gpt-4o',
messages: [{ role: 'user', content: `Summarize: ${document}` }],
})
})
Group calls into traces
When a single user action triggers multiple AI calls, group them with start_trace.
- Python
- TypeScript
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
await CostKey.startTrace({ name: 'POST /api/search' }, async () => {
const intent = await classifyIntent(query) // AI call 1
const results = await search(query, intent) // AI call 2
const summary = await 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.
- Python
- TypeScript
costkey.shutdown()
await CostKey.shutdown()
Next steps
- Python SDK — full API reference, all options, streaming details
- TypeScript SDK — full API reference, Express middleware, sourcemaps
- Providers — all 15 supported AI providers
- Dashboard — what each tab shows
- Privacy & Security — what data is captured, credential scrubbing, data retention