Building applications that integrate AI APIs — whether that's OpenAI, Anthropic, Google Gemini, or a locally hosted model — introduces a new layer of security risk that most developers aren't fully prepared for. The data flowing through these systems is often sensitive. The APIs are expensive to run. And the attack surface is larger and stranger than a traditional web application.
I've spent the past year building AI-integrated systems across a range of contexts — from enterprise chatbots to automated workflow tools — and I've made (and fixed) most of the mistakes below. This isn't academic. These are the ten things that actually matter.
Never hardcode API keys or secrets
This sounds obvious, but it's the most common mistake I see — especially from developers moving fast on a prototype. Hardcoded API keys end up in git repositories, Docker images, browser source, and log files. Once exposed, they can be exploited within minutes by automated scanners that watch public repos.
.env file that is explicitly listed in .gitignore). In production, inject secrets at runtime — never bake them into your image or codebase.README.md. Not even temporarily. There is no "I'll remove it later" — later never comes.Implement rate limiting on every AI endpoint
AI API calls are expensive — both in cost and latency. Without rate limiting, a single malicious user (or a runaway loop in your own code) can burn through your entire monthly budget in hours. Beyond cost, unthrottled endpoints are vulnerable to denial-of-service attacks that degrade the experience for all users.
express-rate-limit (Node), slowapi (Python/FastAPI), or rate limiting middleware in your reverse proxy (nginx, Cloudflare). Set hard daily spend limits on your AI provider account.Validate and sanitise all user input before sending to the model
Prompt injection is the AI equivalent of SQL injection. A malicious user can craft input that overrides your system prompt, extracts confidential instructions, causes the model to behave unexpectedly, or exfiltrates data from your context window. This is a real, actively exploited attack vector.
Log everything — but log it safely
Comprehensive logging is essential for debugging, auditing, and detecting abuse. But AI applications handle sensitive data — user queries, personal information, internal business context — and logging naively can create new security vulnerabilities. Logs that contain PII or sensitive business data become a liability if they're not properly secured.
Authenticate and authorise every request
AI features are often bolted onto existing applications without the same authentication rigour applied to the rest of the system. This creates scenarios where an unauthenticated user can access AI endpoints, or where an authenticated user can access data via the AI that they shouldn't be able to access directly.
Use HTTPS everywhere — including internal services
Data sent between your application and an AI API travels over the internet. Data sent between your own internal microservices may travel across networks you don't fully control. Both need to be encrypted in transit. This is table stakes for any application, but it's worth stating explicitly because AI applications often start as quick scripts or prototypes that never get "productionised."
Scope your AI provider permissions to the minimum necessary
Most AI providers allow you to create multiple API keys with different permission levels. A key used by your web application to run completions doesn't need the ability to delete your fine-tuned models, access billing data, or manage other API keys. Applying the principle of least privilege limits the blast radius if a key is compromised.
Implement output filtering before returning model responses to users
Language models can produce harmful, inappropriate, or legally problematic content even when you've done everything right on the input side. Jailbreaks evolve constantly. Models hallucinate. Without output filtering, you may be serving content that violates your terms of service, exposes you to legal risk, or simply embarrasses your organisation.
Monitor costs and set hard spending alerts
A security misconfiguration — an exposed endpoint, a missing rate limit, a runaway automation — can result in a bill for thousands of dollars before you notice. AI APIs price by usage, and abuse can scale costs faster than almost any other attack vector. Cost monitoring is a security control.
Have a kill switch — and test it
Every AI-integrated system should have a clearly defined and tested procedure for disabling AI functionality quickly, without taking down the rest of the application. This matters for security incidents (a prompt injection attack, a compromised key), unexpected behaviour (the model producing harmful outputs at scale), and provider outages (so your application degrades gracefully).
The 10-point checklist
- Never hardcode API keys — use environment variables and secrets managers
- Rate limit every AI endpoint — per IP, per user, per key
- Validate and sanitise all user input before it reaches the model
- Log metadata, not sensitive content — store logs securely
- Authenticate and authorise every request — including RAG retrieval
- Enforce HTTPS everywhere — including between internal services
- Scope AI provider permissions to the minimum necessary
- Filter model outputs before they reach your users
- Set hard spending alerts and monitor cost anomalies
- Build a kill switch — and test it before you need it
Security in AI applications isn't fundamentally different from security in any other software — it's the same principles applied to a new attack surface. The difference is that the attack surface is larger, the cost of exploitation is higher, and the failure modes are more novel. Get the basics right first, then layer on AI-specific controls.
If you're building an AI-integrated application and want a security review or architectural guidance, reach out to MCSL Solutions. We help organisations build AI systems that are not just capable, but secure and governable.