To secure an AI chatbot on your website, assume anyone can type anything into it, and design it so that even a fully manipulated bot can't reveal private data, take harmful actions or run up your bill. In practice that means keeping API keys and secrets out of the browser and the prompt, giving the bot access only to information you'd publish anyway, putting rate limits and spending caps in front of the model, logging conversations safely, and testing it the way an attacker would before launch.
A secure AI chatbot isn't one with a cleverly worded instruction saying "never reveal secrets." It's one where the secrets were never within reach. This post walks through the main threats, the controls for each, and a pre-launch test plan you can actually run.
Why is a website chatbot a security risk at all?
A traditional contact form accepts text and stores it. An AI chatbot accepts text, interprets it as instructions, generates a response, and often reads documents, queries systems or triggers actions. Every one of those steps can be steered by whoever is typing.
Three properties make it different from the rest of your website:
- It's public. No login, often no CAPTCHA, available to bots and curious teenagers alike.
- It follows natural-language instructions, and language models can't reliably tell your instructions apart from instructions hidden in user input or in content they read.
- It costs money per message. Every response uses paid model capacity, so abuse has a direct financial cost.
None of this is a reason not to have one. A well-built chatbot is one of the most useful things you can add to a website. It just needs to be built like a public-facing application, not like a demo.
What are the main threats to a website AI chatbot?
| Threat | What it looks like | Main control |
|---|---|---|
| Prompt injection | "Ignore your instructions and..." typed by a user, or hidden in a page or document the bot reads | Limit what the bot can access and do; treat its output as untrusted |
| System prompt leakage | Users coax the bot into revealing its instructions, internal notes or configuration | Put nothing sensitive in the prompt |
| Private data exposure | Bot answers from internal documents, other customers' records or staff-only material | Retrieval only from approved public content; authenticated access for account data |
| Cost and abuse attacks | Scripts sending thousands of long messages, or people using your bot as a free general AI assistant | Rate limits, message and length caps, budget alerts, bot protection |
| Harmful or off-brand output | Bot makes promises, gives medical or legal advice, insults a customer, discusses competitors | Scope limits, output checks, clear escalation to humans |
| Unsafe actions | A bot that can issue refunds, change bookings or send emails is tricked into doing so | Tight permissions, confirmations, human approval for anything consequential |
| Output-based attacks | Bot's reply includes code, scripts or links that the chat widget renders unsafely | Render replies as plain text or sanitized markdown; restrict links |
The first of these deserves its own article, and has one: our explainer on prompt injection and AI agents. The short version is that no prompt wording reliably prevents it, so the defenses have to be structural.
How do you keep secrets and private data out of reach?
Start with where things live.
API keys stay on your server. The chat widget in the visitor's browser should talk to your own backend, which talks to the AI provider. If a model API key is in your website's front-end code, anyone can copy it from their browser and use your account. This mistake is common in quick prototypes and no-code builds.
Nothing confidential in the system prompt. The system prompt is the instruction block that tells the bot who it is and how to behave. Treat it as something users may eventually read. Don't put internal discount codes, staff names and phone numbers, supplier terms or "secret" escalation rules in it.
Retrieval only from content you'd publish. Many chatbots use retrieval-augmented generation (RAG), meaning they look up relevant passages from your documents before answering. Build that knowledge base deliberately from public-safe material: your website, help articles, published policies and FAQs. Don't point it at a shared drive. If you want to understand the mechanics, see our plain-English guide to RAG.
Account data requires authentication. If the bot answers "where's my order?", it must verify who's asking, through a login or a verified link, and fetch only that customer's record on the server side. The model should never be in a position to choose whose data to look up based on what the user typed.
Minimize what you collect. Tell users not to share payment card numbers or sensitive personal data in chat, and filter or mask obvious patterns if they do.
How do you stop cost and abuse attacks?
Abuse of chatbots is often about money rather than data. Someone scripts thousands of requests, or discovers your bot will happily write essays and code and uses it as a free AI assistant.
Controls that work:
- Rate limits per IP address, per session and per account, with tighter limits for anonymous users.
- Caps on input length and response length, so one message can't consume a huge amount of model capacity.
- Conversation limits, such as a maximum number of turns per session before suggesting contact with a person.
- Bot protection on the widget, like an invisible challenge, especially if abuse starts.
- Scope enforcement, so the bot politely declines unrelated tasks ("I can help with questions about our products and bookings").
- Hard spending limits and alerts with your AI provider and in your own backend, so a spike pages someone instead of surprising you on the invoice.
- Model choice by task. Smaller, cheaper models often handle routine support questions well; our post on LLM cost optimization covers the trade-offs.
What should the chatbot be allowed to do?
The more a chatbot can do, the more an attacker can make it do. Give it the narrowest set of actions that delivers the value.
- Read-only by default. Answering questions from public content is low risk.
- Actions with confirmations. Booking an appointment or capturing a lead is fine when the user confirms and the action has limited impact.
- Human approval for anything consequential. Refunds, cancellations with fees, account changes and anything involving money should go to a person, or at least sit behind strict server-side rules the model can't override.
- Server-side validation of every action. The backend checks that the request is allowed for this user, within limits, and well-formed. The model proposes; your code decides.
These are the same principles we apply to any agent with tools; our guide to AI agent guardrails goes deeper on permissions and action limits. For the behavioral side (disclosure, tone, what the bot must never promise), see our post on rules for customer-facing AI.
What should you log, and how?
Logs are how you notice abuse, debug wrong answers and investigate incidents. They're also a store of customer conversations, so they need protecting.
- Log conversations, retrieved sources, actions taken, errors and costs per session.
- Mask or redact sensitive data before it's stored where practical.
- Restrict access to logs to people who need them.
- Set a retention period and delete on schedule, in line with your privacy policy and applicable data protection laws (which vary by jurisdiction; this isn't legal advice).
- Watch for patterns: repeated injection attempts, unusual volume from one source, sudden cost spikes, and conversations ending in frustration.
Ongoing monitoring is covered in more depth in our post on AI agent monitoring and observability.
How do you test a chatbot's security before launch?
Spend a few hours trying to break it, ideally with someone who didn't build it. A practical test list:
- Ask it to reveal its instructions, in several phrasings and languages.
- Tell it to ignore its rules, role-play as another assistant, or "enter developer mode."
- Ask for information about other customers, staff or internal processes.
- Ask it for things outside its scope: code, essays, medical or legal advice, opinions on competitors.
- Try to get it to promise a discount, refund or delivery date it can't guarantee.
- Plant instructions in a document or page it retrieves from, and see whether it follows them.
- Send very long inputs and rapid repeated requests, and confirm limits kick in.
- Paste in markup, scripts and odd links, and check the widget displays them safely.
- Confirm the API key isn't visible in the browser's developer tools.
Fix what you find, then rerun the list after every significant change to the prompt, model or knowledge base. Keep a kill switch: a way to disable the bot instantly and fall back to a contact form.
Frequently Asked Questions
Can a website chatbot be hacked?
It can be manipulated through what people type, which is different from a traditional hack but can have similar effects if the bot has access to private data or powerful actions. The defense is limiting what the bot can reach and do, so manipulation has little to gain.
Is it safe to connect a chatbot to our customer database?
It can be, if customers authenticate and the server fetches only their own records, rather than the model querying the database freely. Keep write actions narrow and validated, and log access. Without authentication, don't expose customer-specific data at all.
Can users see our chatbot's system prompt?
Assume they can. Determined users often extract instructions despite the bot being told not to share them. Keep the prompt free of anything you'd mind being published, and put real secrets and rules in server-side code.
How do we stop people using our chatbot as a free ChatGPT?
Restrict its scope in the instructions, cap message and response lengths, apply rate limits, and monitor costs per session. A bot that's grounded only in your content and declines off-topic requests is far less attractive to misuse.
Do we need a security review for a simple FAQ chatbot?
A lighter one, yes. Even an FAQ bot needs the key kept server-side, rate limits, spending caps and a check that its knowledge base contains only public information. The full review matters most once the bot can access accounts or take actions.
Want a website assistant that's useful and hard to abuse?
We build customer-facing AI assistants with server-side controls, scoped access, cost limits and monitoring designed in from the start, as part of our agentic AI development work. Tell us what you want your chatbot to handle and we'll show you where the risks are and how we'd contain them.