cd /news/ai-tools/how-i-stopped-bots-and-trolls-from-b… · home › topics › ai-tools › article
[ARTICLE · art-179206] src=dev.to ↗ pub= topic=ai-tools verified=true sentiment=↑ positive

How I stopped bots and trolls from burning my AI chatbot budget

A developer outlined a layered defense for public AI chatbots that keeps bot traffic and abuse from draining model API budgets, starting with moving provider keys server-side so they can't be copied from frontend code. The approach combines domain-locked public tokens with Origin/Referer checks, per-chatbot daily message caps, per-IP rate limits, honeypot fields, and input/output length limits, with the developer noting that a daily cap means "the worst day has a known price.

recommendation

Move provider keys to the server, enforce a per-chatbot daily message cap and per-IP rate limit, and cap message and response length to bound worst-case token spend.

more in playbook: Controlling LLM cost
by read5 min views5 publishedDec 1, 2026

Putting an AI chatbot on a website is easy now. Keeping it from eating your money is the hard part.

Every message to an AI model costs something. When a real customer asks a question, that is fine. But a public chat box is open to the whole internet, and the internet is not always nice.

Here is what can happen to a chatbot that has no protection:

I have seen this happen on small business sites, and it is a bad feeling. So let's go through what actually protects a chatbot. This is the list I check before I put any AI chat on a website.

This one is first because it is the worst mistake.

If your frontend code has the provider key, anyone can open DevTools and copy it. No limit you add later will help, because they are not even using your chat anymore. They are using your key directly.

The key must live on a server. Your page should only hold something that is useless on its own, like a public token that is locked to your domain (more on that next).

A token in your HTML is public. Someone can copy it and paste it on their own site. So the token alone is not enough, and you need to say which websites are allowed to use it.

Check the Origin or Referer of each request, and reject anything that is not on your list. Also decide what to do when both headers are missing. For a locked chatbot, I think the right answer is to reject it. Real browsers send these headers, and scripts often don't.

Is this perfect? No. Someone can fake headers from a script. But it stops the lazy copy and paste, which is most of the problem.

This is the one that saves your budget. Even if everything else fails, a daily cap means the worst day has a known price.

The idea is simple: count messages per chatbot per day, and when you reach the limit, answer with an error instead of calling the AI.

A basic version in Node looks like this:

// very simple daily counter (use Redis in real life, not a Map)
const counts = new Map();

function underDailyLimit(chatbotId, limit = 500) {
  const day = new Date().toISOString().slice(0, 10);
  const key = `${chatbotId}:${day}`;
  const used = (counts.get(key) || 0) + 1;
  counts.set(key, used);
  return used <= limit;
}

app.post("/chat", async (req, res) => {
  if (!underDailyLimit(req.body.chatbotId)) {
    return res.status(429).json({ code: "daily_limit" });
  }
  // ...call the AI here
});

Two things people forget:

INCR. A daily limit protects your wallet. A rate limit protects you from bursts.

If one IP sends 30 messages in 10 seconds, that is not a human. Something like "20 messages per minute per IP" is usually fine for real users and painful for bots.

One gotcha: if your site is behind nginx, Cloudflare or a load balancer, make sure your app reads the real visitor IP. If you forget, every visitor looks like the same IP (the proxy), and your rate limit becomes one shared limit for everyone. Then one bot can block all your real users.

A honeypot is a hidden input that humans never see, but simple bots fill in. If it has a value, you drop the request.

<input type="text" name="website" tabindex="-1" autocomplete="off"
       style="position:absolute;left:-9999px" aria-hidden="true">
if (req.body.website) {
  // a bot filled the hidden field: act like it worked, but do nothing
  return res.json({ ok: true });
}

Notice that we return success. Don't tell the bot it was caught, or it will just learn and try again.

(If your site uses a strict Content Security Policy, use a CSS class instead of the inline style.)

Someone can paste a whole book into your chat. You pay for every token you send to the model, so set a maximum length on the visitor's message (for example 1,000 to 2,000 characters), and cut the conversation history you send back to the model too.

Also set a maximum length for the AI's answer. A runaway answer costs money too.

This is not about money, but it is about safety. Strip HTML tags from visitor messages before you save them, and escape text once, when you show it. If a visitor types <script>, it should show as plain text and nothing more.

When something fails because of credits, limits or a provider error, the visitor should only see a generic message like "Chat is not available right now." Never send back things like "your balance is 0 credits" or "provider quota exceeded." That tells attackers how your system works, and it looks bad for your client's brand.

You can build every point above yourself. I did it once, and it took me longer than I want to admit, plus I still had to build the chat window, the knowledge base and the lead capture.

That is the reason I made RSFlowHub (so yes, I am biased). The chatbot widget is one script tag:

<script
  src="https://rsflowhub.com/widget/v1/rsflowhub-widget.js"
  data-widget-token="wt_sec_your_token_here"
  async
></script>

And the protection from this list is already there:

There is no AI key in your page at all, so there is nothing to steal.

Before launching any AI chat on a public site, ask:

If you can answer all six, you are in good shape.

An AI chatbot is not risky because of the AI. It is risky because it is a public door to a paid service. Lock the door, limit what can pass through, and tell yourself when it gets busy.

If you want to try the ready version, you can start free at rsflowhub.com. And I would like to know: what is the worst surprise bill you got from an AI feature? Tell me in the comments.

── more in #ai-tools 4 stories · sorted by recency
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
→ Live at https://your-agent.zahid.host ✓
Get free account → Pricing
from €0/mo · no card required
LIVE [news/how-i-stopped-bots-a…] indexed:0 read:5min 2026-12-01 · —