{"slug": "how-i-stopped-bots-and-trolls-from-burning-my-ai-chatbot-budget", "title": "How I stopped bots and trolls from burning my AI chatbot budget", "summary": "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.", "body_md": "Putting an AI chatbot on a website is easy now. Keeping it from eating your money is the hard part.\n\nEvery 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.\n\nHere is what can happen to a chatbot that has no protection:\n\nI 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.\n\nThis one is first because it is the worst mistake.\n\nIf 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.\n\nThe 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).\n\nA 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.\n\nCheck 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.\n\nIs this perfect? No. Someone can fake headers from a script. But it stops the lazy copy and paste, which is most of the problem.\n\nThis is the one that saves your budget. Even if everything else fails, a daily cap means the worst day has a known price.\n\nThe idea is simple: count messages per chatbot per day, and when you reach the limit, answer with an error instead of calling the AI.\n\nA basic version in Node looks like this:\n\n``` js\n// very simple daily counter (use Redis in real life, not a Map)\nconst counts = new Map();\n\nfunction underDailyLimit(chatbotId, limit = 500) {\n  const day = new Date().toISOString().slice(0, 10);\n  const key = `${chatbotId}:${day}`;\n  const used = (counts.get(key) || 0) + 1;\n  counts.set(key, used);\n  return used <= limit;\n}\n\napp.post(\"/chat\", async (req, res) => {\n  if (!underDailyLimit(req.body.chatbotId)) {\n    return res.status(429).json({ code: \"daily_limit\" });\n  }\n  // ...call the AI here\n});\n```\n\nTwo things people forget:\n\n`INCR`.\nA daily limit protects your wallet. A rate limit protects you from bursts.\n\nIf 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.\n\nOne 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.\n\nA honeypot is a hidden input that humans never see, but simple bots fill in. If it has a value, you drop the request.\n\n```\n<input type=\"text\" name=\"website\" tabindex=\"-1\" autocomplete=\"off\"\n       style=\"position:absolute;left:-9999px\" aria-hidden=\"true\">\nif (req.body.website) {\n  // a bot filled the hidden field: act like it worked, but do nothing\n  return res.json({ ok: true });\n}\n```\n\nNotice that we return success. Don't tell the bot it was caught, or it will just learn and try again.\n\n(If your site uses a strict Content Security Policy, use a CSS class instead of the inline `style`.)\n\nSomeone 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.\n\nAlso set a maximum length for the AI's answer. A runaway answer costs money too.\n\nThis 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.\n\nWhen 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.\n\nYou 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.\n\nThat is the reason I made [RSFlowHub](https://rsflowhub.com) (so yes, I am biased). The chatbot widget is one script tag:\n\n```\n<script\n  src=\"https://rsflowhub.com/widget/v1/rsflowhub-widget.js\"\n  data-widget-token=\"wt_sec_your_token_here\"\n  async\n></script>\n```\n\nAnd the protection from this list is already there:\n\nThere is no AI key in your page at all, so there is nothing to steal.\n\nBefore launching any AI chat on a public site, ask:\n\nIf you can answer all six, you are in good shape.\n\nAn 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.\n\nIf you want to try the ready version, you can start free at [rsflowhub.com](https://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.", "url": "https://wpnews.pro/news/how-i-stopped-bots-and-trolls-from-burning-my-ai-chatbot-budget", "canonical_source": "https://dev.to/rutvikpatel/how-i-stopped-bots-and-trolls-from-burning-my-ai-chatbot-budget-4cim", "published_at": "2026-12-01 07:41:08+00:00", "updated_at": "2026-12-01 07:44:51.547650+00:00", "lang": "en", "topics": ["ai-tools", "ai-products", "ai-agents"], "entities": [], "also_reported_by": [], "takeaway": "Keep the model provider key server-side, gate requests with Origin/Referer checks and a per-chatbot daily message cap, and add per-IP rate limits plus input/output length limits so a bot flood costs a known maximum.", "recommendation": {"advice": "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.", "playbook": "llm-cost", "playbook_url": "https://wpnews.pro/playbooks/llm-cost"}, "alternates": {"html": "https://wpnews.pro/news/how-i-stopped-bots-and-trolls-from-burning-my-ai-chatbot-budget", "markdown": "https://wpnews.pro/news/how-i-stopped-bots-and-trolls-from-burning-my-ai-chatbot-budget.md", "text": "https://wpnews.pro/news/how-i-stopped-bots-and-trolls-from-burning-my-ai-chatbot-budget.txt", "jsonld": "https://wpnews.pro/news/how-i-stopped-bots-and-trolls-from-burning-my-ai-chatbot-budget.jsonld"}}