Across ~57,600 daily burst calls. Dual RPM (Requests/Min) and TPM (Tokens/Min) limits — enforce per-execution token caps, prompt caching, and circuit-breaker spend ceilings.
Unthrottled webhook bursts and runaway AI agent loops trigger HTTP 429 rate-limit drops or unexpected $500+ overnight LLM token bills. Generate an adaptive n8n rate-limit pacer and PostgreSQL daily spend circuit breaker below.
Across ~57,600 daily burst calls. Dual RPM (Requests/Min) and TPM (Tokens/Min) limits — enforce per-execution token caps, prompt caching, and circuit-breaker spend ceilings.
Estimated monthly overage / token protection: $890/mo in prevented runaway loop token burn.
Included in Vault #03, #09, #31 & #49 (LLM Token-Budget Guard + Model Fallback) inside the $79 StackROI 50-Blueprint Vault.
Inspects Retry-After headers, enforces inter-item pacing for 120 RPM, and checks token budget ceilings before calling the downstream API:
// n8n Code Node: Adaptive Rate-Limit Pacer & HTTP 429 Retry-After Guard (openai-anthropic)
const items = $input.all();
const targetRPM = 120;
const minDelayMs = Math.max(50, Math.ceil(60000 / targetRPM));
const maxTokensPerItem = 4096;
return items.map((item, idx) => {
const headers = item.json.headers || {};
const retryAfterSec = Number(headers['retry-after'] || headers['x-ratelimit-reset-requests'] || 0);
const pauseMs = retryAfterSec > 0 ? (retryAfterSec * 1000) : (idx * minDelayMs);
const promptText = String(item.json.prompt || item.json.body || '');
const estimatedTokens = Math.ceil(promptText.length / 4);
return {
json: {
...item.json,
_rate_limit_guard: {
api_profile: 'openai-anthropic',
batch_index: idx,
scheduled_delay_ms: pauseMs,
estimated_input_tokens: estimatedTokens,
within_token_budget: estimatedTokens <= maxTokensPerItem,
idempotency_key: item.json.event_id || ('sr_' + Date.now() + '_' + String(idx))
}
}
};
});
Atomically tracks daily API request & token spend and flips circuit_state = 'OPEN' if an infinite webhook loop exceeds your daily dollar ceiling:
-- PostgreSQL Daily Token-Budget & API Circuit-Breaker Ledger (openai-anthropic)
CREATE TABLE IF NOT EXISTS stackroi_api_budget_guard (
usage_date DATE NOT NULL DEFAULT CURRENT_DATE,
api_profile TEXT NOT NULL DEFAULT 'openai-anthropic',
requests_count BIGINT NOT NULL DEFAULT 0,
tokens_used BIGINT NOT NULL DEFAULT 0,
estimated_spend_cents BIGINT NOT NULL DEFAULT 0,
daily_limit_cents BIGINT NOT NULL DEFAULT 2500, -- $25.00/day hard ceiling
circuit_state TEXT NOT NULL DEFAULT 'CLOSED', -- CLOSED (allow) | OPEN (trip)
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
PRIMARY KEY (usage_date, api_profile)
);
-- Atomic Pre-Execution Check & Increment (Trips Circuit if Daily Spend Ceiling Reached)
INSERT INTO stackroi_api_budget_guard (usage_date, api_profile, requests_count, tokens_used, estimated_spend_cents)
VALUES (CURRENT_DATE, 'openai-anthropic', 1, $1, $2)
ON CONFLICT (usage_date, api_profile) DO UPDATE
SET requests_count = stackroi_api_budget_guard.requests_count + 1,
tokens_used = stackroi_api_budget_guard.tokens_used + EXCLUDED.tokens_used,
estimated_spend_cents = stackroi_api_budget_guard.estimated_spend_cents + EXCLUDED.estimated_spend_cents,
circuit_state = CASE
WHEN (stackroi_api_budget_guard.estimated_spend_cents + EXCLUDED.estimated_spend_cents) >= stackroi_api_budget_guard.daily_limit_cents
THEN 'OPEN'
ELSE 'CLOSED'
END,
updated_at = NOW()
RETURNING circuit_state, requests_count, tokens_used, estimated_spend_cents, daily_limit_cents;
Get immediate post-checkout access to stackroi-50-blueprint-vault.json including pre-wired rate-limit pacers, LLM token-budget circuit breakers, HMAC webhook verifiers, and PostgreSQL Dead-Letter Queue workers.
Need custom high-throughput n8n queue-mode clustering or dedicated Redis worker orchestration? Contact our engineering team for custom architecture →