Vibe Coding Guide · Vibe Coding / Costs

Vibe coding costs: stop the surprise bill

Seven limits for an app that calls a paid AI API, with the code, so a stolen key or a bug cannot run up a bill you never planned for.

· 5 min read · by Hadi Vahidi

The first sign of a leaked key is often not a security alert but an invoice. This guide goes with my reel on surprise bills: seven limits to put on an app that calls a paid AI API, each with the code from a small test app of mine, so a stolen key, a bug or one keen user cannot run up a bill you never planned for.

The reel is from @hadify_dev; its chart, budget and counters are drawings. The code below comes from a test app of mine that was never deployed and holds no keys, except the caching example, which is an illustration. The voice in the video is synthetic.

01 Two stolen keys, likely six figures

The reel: Stop the surprise bill

The leaked-key reel ends on "Do you know how to cap the bill if someone uses it?" This is the answer.

Wiz's research on attacks against AI developer platforms in October 2026 looked at two cases of stolen credentials and estimated the cost: "At list price, these two cases alone likely ran up over $100k in usage." Someone else's code, running on your account, billed to you.

Keeping keys secret is the first defence (see the API keys section). The limits below are the second: they make sure that when something goes wrong, the damage has a ceiling.

02 Spend limits and billing alerts, everywhere you pay

  1. On every AI API you use, find the spend limit or the budget alert. They are not the same thing: a limit stops the spending when you reach it; an alert only sends you an email. Some providers offer one, some both. Know which one you have.
  2. On your host too. Hosting that bills by use (functions, bandwidth, database reads) can grow with a bug or a crawler as easily as an AI bill can. Set the usage alert, and the cap where your host offers one.
An alert you set at the amount you are willing to lose arrives too late. Set it lower, at the first figure that would surprise you.

03 Rate-limit AI calls per user

Without a limit, one signed-in account (or one script using it) can call your AI endpoint thousands of times an hour, and you pay for every call. A limit per user keeps everyone to a fair number. In an Express server, with the express-rate-limit package:

import rateLimit from 'express-rate-limit';

// 20 AI calls an hour for each signed-in user
export const aiLimit = rateLimit({
  windowMs: 60 * 60 * 1000,
  limit: 20,
  keyGenerator: (req) => req.user.id,
});

The keyGenerator line is the important one: it counts per user rather than per IP address, so one account cannot spread its calls across many networks. Put the limiter on the routes that call the AI, and send a clear message when someone reaches it.

04 Cap the tokens per request

AI APIs charge by the token, and an answer with no cap can run as long as the model likes. Set the maximum on every request. With Anthropic's SDK it is max_tokens:

const reply = await anthropic.messages.create({
  model: 'claude-haiku-5-5',
  max_tokens: 500,
  messages: [{ role: 'user', content: question }],
});

In OpenAI's Responses API the same setting is max_output_tokens. Pick the smallest number that still fits a good answer for your feature.

05 Cache answers you've already paid for

If many people ask the same thing, you do not need to pay the AI to answer it again each time. Keep the answer and serve it from your own store. The idea, as an illustration:

const cache = new Map();

export async function askCached(question) {
  const key = question.trim().toLowerCase();
  if (cache.has(key)) return cache.get(key);   // second time: no new call
  const answer = await ask(question);
  cache.set(key, answer);
  return answer;
}

A real app would keep the cache in its database or a cache service so it survives a restart, and would only cache answers that are the same for everyone, never anything personal.

06 Watch for loops that call your API on every render

This one is easy for an AI to write and hard to spot. In React, a useEffect with no dependency list runs after every render. If that effect fetches an answer and stores it in state, storing it causes another render, which runs the effect again, which calls your API again:

useEffect(() => {
  fetch(`/api/ask?q=${encodeURIComponent(question)}`).then((r) => r.json()).then(setAnswer);
});

The fix is one line: tell React to run the effect only when the question changes.

useEffect(() => {
  fetch(`/api/ask?q=${encodeURIComponent(question)}`).then((r) => r.json()).then(setAnswer);
}, [question]);

When you test a feature that calls an AI, keep the Network tab of DevTools open. One click should make one call.

07 Use the smallest model that does the job

The largest model is rarely needed for sorting a message, summarising a short text or answering a simple question, and the price difference between model sizes is large. Start with the smallest model that gives good answers for your feature, and only move up where you can see it fails.

Tell your AI

Before we ship anything that calls a paid AI API: rate-limit AI calls per signed-in user, set max_tokens (or max_output_tokens) on every request, cache answers that are the same for everyone, make sure no useEffect calls the API on every render, and use the smallest model that does the job. Tell me which spend limits and billing alerts I should set on each service we use.

This reel ends on "Could a stranger find your API key in a minute?" That is in the vibe coding security guide.

08 Sources

  • Wiz, "Black October" for AI developer platforms, 30 October 2026: wiz.io
  • express-rate-limit (windowMs, limit, keyGenerator): github.com
  • Anthropic, Messages API (max_tokens): docs.anthropic.com
  • OpenAI, Responses API (max_output_tokens): platform.openai.com
  • React, useEffect (an effect with no dependency array runs after every render): react.dev

Sources read between 3 and 9 November 2026.