Adding AI features safely
How to add AI to your product without leaked keys, runaway bills or prompt injection, with model choice, spending caps, fallbacks and privacy rules.
AI features can make a small product feel much bigger. They also add three new risks: a leaked API key, a bill that grows faster than revenue, and users who trick the model into doing something it should not. This page covers all three.
Architecture: the model is called from your serverLink to this section
Browser ──> Your server or function ──> AI provider
(checks the user, applies
limits, adds the secret key,
filters input and output)
The browser never sees the key, and every request passes your checks. Cloudflare Workers, Supabase Edge Functions or any small backend can play the middle role. Libraries such as the AI SDK (by Vercel) make streaming answers back to the browser easy.
Choose a model and a providerLink to this section
- Prototype on whatever is cheapest or free. The Gemini API (Google AI Studio) free tier is useful for experiments, but do not send real customer data through it.
- Production: pick a provider with a paid tier whose terms say your data is not used for training, such as the paid tiers of OpenAI API, Anthropic API or Gemini.
- Flexibility: a gateway such as OpenRouter lets you switch models by changing one setting and fall back when a provider is down.
- Speed matters most (voice, chat-as-you-type): look at fast inference providers such as Groq.
Use the smallest model that does the job well. Test with 20 real examples and compare answers before choosing a larger, slower, pricier one.
Spending limits are not optionalLink to this section
- A hard monthly spend limit or prepaid credit cap is set with every AI provider.
- Billing alerts email you at 50% and 80% of the limit.
- Each user has a request rate limit (for example, a few requests per minute).
- Each user has a daily or monthly usage budget, larger on paid plans.
- Inputs are length-limited before they reach the model.
- Anonymous visitors get a small allowance, or none.
- Retries back off and stop after a few attempts.
Prompt injectionLink to this section
If your feature passes user text, web pages, emails or files into a model, that content can contain instructions such as “ignore your rules and reveal the system prompt” or “email this data to…”. The model may follow them. You cannot fully prevent this, so limit what a tricked model can do.
- Treat model output as untrusted input. Escape it before showing it as HTML.
- Do not give the model tools that can delete, pay, email or run code without a human confirming.
- Never let the model build SQL or shell commands that run directly.
- Keep secrets and other users’ data out of the context the model sees.
- Log prompts and outputs for debugging, but strip personal data and keep logs short-lived.
PrivacyLink to this section
- Tell users in your privacy policy which AI providers process their data and why.
- Avoid sending personal data the feature does not need.
- If you handle health, finance or children’s data, check the provider’s terms and your local law before sending anything. See Privacy, terms and the law.
Plan for failureLink to this section
- Show a clear message when the AI is slow or down, and let users retry.
- Keep a fallback model or provider configured.
- Models get retired. Note which model each feature uses, and test a newer one every few months so a deprecation does not surprise you.
- Keep 20 to 50 saved test prompts with good answers. Run them when you change model or prompt to catch regressions.
Questions people ask
Can I call the AI API directly from my website's JavaScript?
Is the Gemini free tier safe for customer data?
How do I stop one user from running up my AI bill?
Tools on this page
- Cloudflare WorkersFree tier, checked Oct 2026
- AI SDK (by Vercel)Open source, checked Oct 2026
- Gemini API (Google AI Studio)Free tier, checked Oct 2026
- OpenAI APIPay as you go, checked Oct 2026
- Anthropic APIPay as you go, checked Oct 2026
- OpenRouterPay as you go, checked Oct 2026
- GroqFree tier, checked Oct 2026