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. 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: js // 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.