Claude API Spend Alerts Setup: A Step-by-Step Guide
If you're asking how to set up Claude API spend alerts, you're probably trying to solve one of two problems: a runaway script or bug burned through budget overnight, or you need visibility before finance asks why the AI line item tripled. Either way, the fix is the same — combine a hard ceiling with real-time usage tracking, then wire notifications on top of both.
There's no single "spend alert" toggle that covers every scenario on its own. A workable setup has three layers: a spending limit set at the account or workspace level as a last line of defense, usage metadata pulled from every API response so you know what's being consumed in near real time, and a notification layer (email, Slack, webhook) that fires before you hit the ceiling, not after.
Layer 1: Hard limits as a safety net
Start with whatever spending or rate limits your provider account exposes at the workspace or organization level. This won't give you granular per-project insight, but it caps worst-case damage from a misconfigured loop or leaked key. Treat this as insurance, not monitoring — by the time a hard limit triggers, you've already spent the money. The real alerting work happens in the layers below.
Layer 2: Track usage metadata on every call
Every Claude API response includes token usage for that request — input tokens, output tokens, and (depending on the setup) cache read/write tokens. This is the raw data your spend alerts are built from. The pattern is simple: capture usage on every response, multiply by your per-token rate, and accumulate it somewhere you can query.
async function callAndTrackUsage(messages) {
const res = await fetch("https://api.subtoapi.app/v1/messages", {
method: "POST",
headers: {
"Authorization": `Bearer ${process.env.SUBTOAPI_KEY}`,
"Content-Type": "application/json"
},
body: JSON.stringify({
model: "claude-sonnet-4-5",
max_tokens: 1024,
messages
})
});
const data = await res.json();
const { input_tokens, output_tokens } = data.usage;
await recordUsage({
key_id: process.env.SUBTOAPI_KEY_LABEL,
input_tokens,
output_tokens,
timestamp: Date.now()
});
return data;
}
recordUsage can write to a database, a time-series store, or even a flat log file you aggregate hourly — the important part is that every call contributes a data point. If you're issuing separate application API keys per project or customer, tag each usage record with the key so you can break down spend later. SubToAPI generates scoped sub_live_... keys per app and surfaces usage metadata in the dashboard, which makes this tagging step free if you're already routing traffic through it — see /docs/messages for the full response shape, including how usage is reported on streamed responses in /docs/streaming.
Layer 3: Define thresholds that trigger before it hurts
Once usage is flowing into a store, define thresholds in actual currency, not token counts — nobody budgets in tokens. A sane threshold structure looks like:
- 50% of monthly budget — informational, posted to a low-priority Slack channel.
- 80% of monthly budget — actionable alert, tagged to whoever owns the project.
- 100% of monthly budget — page someone, and optionally auto-throttle or disable the key.
A simple cron job checking accumulated spend against these thresholds covers most teams:
async function checkSpendThresholds() {
const spend = await getMonthlySpend(); // sums usage * rate
const budget = 500; // EUR
const pct = (spend / budget) * 100;
if (pct >= 100) {
await notifySlack(`🚨 Claude API spend at ${pct.toFixed(0)}% of budget (€${spend.toFixed(2)})`);
} else if (pct >= 80) {
await notifySlack(`⚠️ Claude API spend at ${pct.toFixed(0)}% of budget`);
} else if (pct >= 50) {
await notifySlack(`ℹ️ Claude API spend at ${pct.toFixed(0)}% of budget`);
}
}
Run this hourly or on every N requests, whichever fits your traffic volume. For low-traffic apps, hourly is fine. For high-throughput production use, check more often — spend can spike fast with long context windows or heavy tool-use loops, since tool calls and their results all count as tokens. If you're using tool use heavily, budget for the extra round trips; see /docs/tools for how multi-step tool calls accumulate usage across turns.
Per-project and per-key alerts
Lumping all spend into one number works for solo projects but breaks down fast once you have multiple apps or clients hitting the same account. The fix is issuing a distinct API key per project and tagging every usage record with the key that generated it. Then your threshold check runs per key instead of globally:
const projects = await getActiveKeys();
for (const project of projects) {
const spend = await getMonthlySpend(project.key_id);
if (spend > project.budget) {
await notifySlack(`Project "${project.name}" exceeded its budget: €${spend.toFixed(2)}`);
}
}
This is where having scoped keys per application pays off directly — you get the breakdown for free instead of parsing it out of a single shared stream of requests. If you're setting this up from scratch, /docs/quickstart walks through generating and scoping separate keys per project.
Keep the loop tight
The biggest failure mode in spend alerting isn't missing data — it's missing a cheap alerting channel. A Slack webhook or email via a transactional provider takes ten minutes to wire up and is what actually gets someone to look at the dashboard before the bill arrives. Don't over-engineer this part; a fetch call to an incoming webhook URL, triggered from the same cron job checking thresholds, is enough for 95% of teams.
FAQ
Does Claude's API have built-in spend alert emails? Account and workspace-level spending limits exist as a safety net, but granular, per-project alerting generally has to be built on top using usage metadata from each response — there's no single dashboard toggle that covers project-level thresholds out of the box.
How often should I check accumulated spend? Hourly is fine for most apps. If you run high-volume or tool-heavy workloads where usage can spike quickly within a single session, check every few hundred requests instead of on a fixed schedule.
Can I get per-project spend without building a billing system? Yes — issue a separate API key per project or client and tag usage records with the key ID. Platforms like SubToAPI (see /pricing and /signup) generate scoped keys per app and track usage per key in the dashboard, so the breakdown exists without a custom billing pipeline.