How to prevent runaway OpenAI API cost loops in Next.js
Updated November 17, 2026
You wake up to an email from OpenAI: you hit your monthly limit. Yesterday you'd spent $4. The app didn't get popular overnight. Something looped.
In Next.js apps, almost every runaway AI bill comes from one of four patterns.
1. Retries with no ceiling
async function ask(prompt: string): Promise<string> {
try {
return await callModel(prompt);
} catch {
return ask(prompt); // retries forever on a 429 or a timeout
}
}
A rate limit (429) or a provider outage turns this into a tight loop that keeps billing for every request that gets through. Cap it and back off:
async function ask(prompt: string, attempt = 1): Promise<string> {
try {
return await callModel(prompt);
} catch (e) {
if (attempt >= 3) throw e;
await new Promise((r) => setTimeout(r, 2 ** attempt * 500));
return ask(prompt, attempt + 1);
}
}
2. A client effect that calls your AI route on every render
useEffect(() => {
fetch("/api/summarize", { method: "POST", body: JSON.stringify({ text }) });
}); // no dependency array: runs after every render
If the response sets state, the state change re-renders, the effect runs again, and you have a loop that only stops when the tab closes. Give every effect that calls a paid API a dependency array, and guard it so it runs once per input.
3. Agent loops with no step limit
Tool-calling agents run "until the model says it's done". A confused model never says it's done. Always pass a hard maximum number of steps, and stop when the same tool is called with the same arguments twice.
4. Public AI routes with no rate limit
app/api/chat/route.ts is a public URL. Anyone who finds it can call it in a loop, and you pay. Put three things in front of every AI route:
- Auth. Only signed-in users can call it.
- A per-user rate limit. For example 20 requests a minute, using Upstash, a database counter or your platform's limiter.
- A max token setting on every model call (
max_tokens/max_completion_tokens).
Set a spend cap at the provider too
OpenAI, Anthropic and most providers let you set a monthly budget or usage limit in the console. Set one. It won't stop a loop from happening, but it stops a loop from becoming a disaster.
Find these patterns automatically
Reading every effect and every catch block by hand doesn't scale, especially when an AI writes half the code. Periderm flags recursion with no exit, polling effects and intervals that never stop, and API routes with no rate limit, and points at the file and line:
npm i -g periderm-cli
periderm scan
Run it after each change, not once at the end. Then run periderm agents so your coding agent scans and fixes on its own after every task.