DEV Community

George Panos
George Panos

Posted on

Security Basics for AI Apps and APIs

AI apps and APIs can be powerful, but they also create new security risks. If you are building with LLMs, embeddings, or tool-using agents, you need more than normal web-app security; you also need protections for prompts, outputs, and model behavior.

The good news is that the basics are manageable. A few disciplined practices can prevent many of the most common failures before they become incidents.

Start with the threat model
Before writing code, ask a simple question: what could go wrong if this app is attacked?
For AI apps, the risks usually fall into a few buckets: leaking private data, allowing unauthorized actions, malicious prompt manipulation, unsafe tool use, and abuse of expensive model calls. If your app connects to internal systems, the risk grows fast because a weak prompt can become a weak access-control layer.
A good threat model does not need to be perfect. It just needs to identify what the model can see, what it can do, and what should never be allowed.

Protect your inputs
Treat every external input as untrusted. That includes user messages, uploaded files, web pages pulled through retrieval, emails, documents, and anything another system sends into your pipeline.
Do not assume the model can tell the difference between instructions and data. A malicious document can contain text that tries to override your system prompt or trick the model into revealing secrets.
Practical defenses include:
Separating system instructions from user content.
Wrapping retrieved text in clear delimiters.
Filtering obvious malicious payloads before the model sees them.
Rejecting or sanitizing unexpected file types and formats.
The goal is not to make input “safe” in every case. The goal is to make sure untrusted content cannot quietly become instructions.

Minimize data exposure
One of the biggest mistakes in AI apps is sending too much data to the model. If the model does not need a field, do not include it.
This matters because prompts often contain private details by accident: names, tokens, internal notes, customer records, or full documents when only a short excerpt is needed. The less sensitive data you expose, the less damage an attacker can cause.

A useful habit is data minimization:
Send only the text or fields required for the task.
Mask secrets before prompts are built.
Avoid placing API keys, credentials, or internal URLs in prompts.
Use retrieval to fetch only relevant chunks instead of entire sources.
Less data in the prompt means less data available to leak.

Control tool access carefully
If your AI app can call tools, the tools become part of your security boundary. A model that can send emails, query databases, or modify records is no longer just generating text; it is taking actions.
That means you should enforce permissions outside the model. The model can suggest an action, but your application should decide whether that action is allowed.
Good practices include:
Giving each tool the minimum required permission.
Restricting tool use by role, user, and context.
Requiring human approval for sensitive actions.
Logging every tool call with enough detail to audit it later.
Never trust the model to self-police. The application must remain the final authority.

Validate model outputs
Model output should not be treated as trusted code or trusted instructions. This is especially important if the output is used to generate JSON, trigger workflows, call APIs, or render content in a browser.
You should validate format, type, length, and allowed values before anything is executed. If the model produces structured output, parse it strictly and reject anything that does not match the expected schema.
This is especially important when output crosses a trust boundary. A text answer is one thing. A command sent to production systems is something else entirely.

Secure the API itself
AI APIs still need standard API security. Authentication, authorization, rate limiting, and logging remain essential.
Do not expose endpoints without access control. Use short-lived credentials where possible. Rotate secrets regularly. Rate-limit both user traffic and expensive inference calls to reduce abuse and cost spikes.
Also remember that AI endpoints can be abused in unusual ways. Attackers may try prompt flooding, token exhaustion, or repeated requests designed to increase billing. Monitoring usage patterns is part of security, not just ops.

Log carefully
Logs are useful, but they can also become a leak. AI systems often log prompts, outputs, retrieval context, and tool results, which may contain sensitive data.
Only log what you need. Redact secrets, tokens, and personal data. Protect logs with the same care as application data, because they often become a second copy of the same sensitive information.
If you keep prompt traces for debugging, make sure access is restricted. A well-intentioned log can become a privacy problem if it is too easy to read.

Test for failures early
AI security cannot be tested only with normal unit tests. You also need adversarial testing.
Try malicious prompts, weird inputs, oversized payloads, malicious documents, and tool-abuse scenarios. Test whether the model can be pushed into leaking data, ignoring constraints, or taking unsafe actions.
This kind of testing helps you find assumptions before attackers do. It is much easier to fix a weakness in development than after users have already discovered it.

Keep humans in the loop
For risky AI systems, human review is still one of the best defenses. This is especially true for financial actions, account changes, medical guidance, legal content, or anything with real-world consequences.
A human does not need to inspect every token. They just need to review the moments that matter: approvals, escalations, external communications, and destructive actions. That one extra step can stop a bad model suggestion from becoming a bad production event.

Final thoughts
Security for AI apps is not magic. It is mostly disciplined engineering: minimize what the model sees, tightly control what it can do, validate what it outputs, and never let the model replace your application’s security logic.
If you build with that mindset, your AI app becomes much harder to abuse and much easier to trust. The safest systems are not the ones that assume the model behaves perfectly; they are the ones that stay secure even when it does not.

Top comments (0)