Package Exports
- capped-cost
Readme
capped-cost
Fetch monthly OpenAI and Anthropic API spend from the provider Usage APIs. Read-only admin keys, zero runtime dependencies, normalized output.
npm install capped-costBuilt alongside Capped — a Chrome extension that watches your OpenAI and Anthropic spend and alerts you at 80/100/150% of a monthly cap. If you want the budgeting + alerting without writing the cron yourself, try the extension. If you want to build something custom, this package is the primitive.
Why
Both OpenAI and Anthropic publish a usage/cost API. Both expose an admin-key primitive that can read usage without being able to make model calls or spend money. But the two endpoints have different shapes, different auth, different time formats, and different units.
capped-cost fetches both in parallel and returns one normalized shape. Cents everywhere. No floats, no string amounts, no drift when you sum across days.
Usage
OpenAI only
import { fetchOpenAICost } from "capped-cost";
const result = await fetchOpenAICost(process.env.OPENAI_ADMIN_KEY!);
if ("error" in result) {
console.error(result.error);
} else {
console.log(`Month to date: $${(result.totalCents / 100).toFixed(2)}`);
for (const [day, cents] of result.dailyCents) {
console.log(` ${day}: $${(cents / 100).toFixed(2)}`);
}
}Anthropic only
import { fetchAnthropicCost } from "capped-cost";
const result = await fetchAnthropicCost(process.env.ANTHROPIC_ADMIN_KEY!);
// same shape as fetchOpenAICostBoth providers in parallel
import { fetchAllCosts } from "capped-cost";
const { totalCents, dailyCents, providers } = await fetchAllCosts({
openai: process.env.OPENAI_ADMIN_KEY,
anthropic: process.env.ANTHROPIC_ADMIN_KEY,
});
console.log(`Combined month to date: $${(totalCents / 100).toFixed(2)}`);
// Per-provider breakdown
for (const [name, r] of Object.entries(providers)) {
if ("error" in r) {
console.warn(`${name}: ${r.error}`);
} else {
console.log(`${name}: $${(r.totalCents / 100).toFixed(2)}`);
}
}Admin keys
Both providers require an Organization Admin Key — different from a standard API key. Admin keys can read usage but cannot make model calls or spend money.
- OpenAI: https://platform.openai.com/settings/organization/admin-keys (requires Organization Owner role)
- Anthropic: https://console.anthropic.com/settings/admin-keys
Store admin keys the same way you store any production secret. capped-cost does not ship, proxy, or log them anywhere.
API
fetchOpenAICost(adminKey: string, options?: FetchOptions)
=> Promise<CostResult | CostError>
fetchAnthropicCost(adminKey: string, options?: FetchOptions)
=> Promise<CostResult | CostError>
fetchAllCosts(
keys: { openai?: string; anthropic?: string },
options?: FetchOptions
) => Promise<CombinedCostResult>
// Types
interface CostResult {
dailyCents: Map<string, number>; // "YYYY-MM-DD" → cents
totalCents: number;
}
interface CostError {
error: string;
}
interface FetchOptions {
fetch?: typeof globalThis.fetch; // override (tests, custom agents)
signal?: AbortSignal; // for cancellation
}Runtime support
- Node 18+ (native
fetch) - Bun
- Deno
- Every modern browser (CORS permitting)
- Chrome extensions (service workers + content scripts)
Zero runtime dependencies. Total install size: ~5 KB unpacked.
Example: a 50-line CLI budget alert
#!/usr/bin/env node
import { fetchAllCosts } from "capped-cost";
const CAP_USD = Number(process.env.CAP_USD || 100);
const keys = {
openai: process.env.OPENAI_ADMIN_KEY,
anthropic: process.env.ANTHROPIC_ADMIN_KEY,
};
const { totalCents } = await fetchAllCosts(keys);
const totalUsd = totalCents / 100;
const pct = (totalUsd / CAP_USD) * 100;
console.log(`Month-to-date: $${totalUsd.toFixed(2)} / $${CAP_USD} cap · ${pct.toFixed(0)}%`);
if (pct >= 80) {
process.exit(1); // trigger your alerting pipeline
}Run it from cron hourly, wire process.exit(1) to PagerDuty or a Slack webhook. You now have a $0 internal budget alert.
License
MIT.
Context
capped-cost was extracted from the internals of Capped — a Chrome extension that tracks OpenAI and Anthropic spend and alerts you at 80/100/150% of a monthly cap. The extension is free; $9/month Pro adds browser-closed alerts.
If you want budgeting + alerting without writing the cron yourself, try Capped. If you want to build something more custom, use this package.