Designing Safe AI Application Architectures
The architecture does not assume the code is safe.
Why a Single Input Check Falls Short
Most people first think about security as a single gate. You check the user's input, and if it looks fine, you let it through. That approach is simple, and it is not enough for an AI application. An AI application can be harmed from several directions at once, so a protection that watches only the incoming message leaves most of the attack surface uncovered.
What do you think happens?
Before reading on, list the ways danger could enter an AI application that has a model, the ability to run code, and access to a database. How many separate entry points can you identify?
Reveal answer
The source describes four separate directions: a user can type something hostile; the model can produce something harmful even when the input looked innocent; the application may run code the model wrote; and the application may have the power to change a database.
Each of these entry points needs its own protection. A check on the input addresses only the first one. The rest of this article follows the protections that address the other directions.
The source calls this practice system-level defense: building several protections into the application's design so that a failure in one place does not automatically become a failure everywhere. The aim is not one perfect wall. The aim is that when one protection fails, another is still standing between the problem and the harm it could cause.
This article looks at four design practices: isolating generated code, requiring human approval for impactful commands, defining out-of-scope topics, and placing guardrails on inputs and outputs. It also covers anomaly detection, which watches how users behave over time rather than judging single messages in isolation.
Isolating Generated Code in a Virtual Machine
Some AI systems do more than produce text. They produce code, and that code may be executed. Generated code cannot be fully vouched for before it runs, so the safe default is to treat it as untrusted. The protection the source describes is isolation: the generated code runs only inside a virtual machine that is separated from the user's main machine.
Isolation, in this context, means running generated code only inside a virtual machine that is kept separate from the user's main machine, so that any harm the code causes is confined to that virtual machine.
The reasoning is about containment rather than prevention. The architecture does not assume the code is safe. It assumes the code might be harmful and limits where that harm can reach. If the code turns out to be malware or otherwise damaging, the damage stays inside the virtual machine. The user's main machine, which the user relies on, is kept apart from it.
Tracing a Harmful Script Through the Sandbox
A generated script that does damage
An AI system writes a script and executes it. The script turns out to be malware that tries to delete and alter files. Trace what happens when the isolation design is in place.
- 1The script is produced
The model has written code. Because generated code cannot be fully checked before it runs, the system treats it as untrusted.
- 2The script is placed inside the virtual machine
The generated code runs only inside the virtual machine. It does not run directly on the user's main machine.
- 3The malicious actions take effect
The malware performs its harmful operations, but those operations act on the virtual machine's environment, which is separated from the main machine.
- 4The harm is contained
The damage stays confined to the virtual machine. The user's main machine is kept apart from it, so the harm does not spread to the machine the user relies on.
The malware is still harmful inside the virtual machine, but its reach stops at the boundary. This is the containment reasoning the source describes.
Human Approval Before Impactful Commands
A second design practice concerns actions rather than code. Some commands an AI system can issue have consequences that are hard or impossible to undo. The source recommends that such commands should not run without explicit human approval. The approval step is a deliberate pause: the system proposes the action, and a person must agree before it happens.
Consider an AI system with access to an SQL database. The source gives a concrete rule: any query that tries to change the database, such as one containing DELETE, DROP, or UPDATE, must be approved before it is executed. The rule is aimed at operations that modify stored data. Reading data is not singled out in that rule.
- UPDATE modifies stored rows, so the rule requires approval before this runs.
- The WHERE clause limits which rows change, but the statement still alters the database.
What do you think happens?
Under the source's rule, which kinds of queries must be approved before they run?
Reveal answer
Queries that try to change the database, such as those containing DELETE, DROP, or UPDATE.
The rule is about modifying stored data. The source does not single out read queries for approval, which shows the protection targets operations that change the database.
Input and Output Guardrails
Guardrails are mechanisms that filter what goes into an application and what comes out of it, with the aim of preventing harmful or toxic content. The source emphasizes that guardrails belong on both sides of the model. Each side catches a different risk, and the two are not interchangeable.
An input guardrail checks the message before the model sees it. Its job is to stop hostile or harmful content from entering the application at all. An output guardrail checks the model's reply before the user sees it. Its job is to catch harmful content the model produced, even when the input looked acceptable.
Both are needed because an input can pass every check and still lead to a harmful reply. A message that looks innocent on its own may cause the model to produce something harmful. Filtering only the inputs leaves that path open. Filtering only the outputs leaves hostile messages reaching the model. Each guardrail covers a direction the other cannot see.
Defining Out-of-Scope Topics
An application is built for a purpose, and it can fail simply by wandering into subjects it was never prepared to handle. The source describes defining out-of-scope topics to reduce this risk. Its example is a customer support chatbot, which should not answer political or social questions even when a user asks them. Writing the boundary down in advance is the first step. Enforcing it is the second.
The source describes two ways to enforce the boundary. The simplest is keyword filtering, which screens each input for predefined phrases typically tied to controversial subjects and filters out inputs that contain them. The source gives examples such as 'immigration' and 'antivax'. Keyword filtering is easy to build, but it only sees the words in the current message.
More advanced approaches use AI to work out what the user is trying to do. These methods look at the whole conversation, not only the latest message. When they detect a request with an inappropriate intent, they can block it or send it to a human operator. This is a step up from matching phrases, because a request can be off-topic or manipulative even when it avoids every flagged word.
| Approach | What it reads | Main strength | Main limitation |
|---|---|---|---|
| Keyword filtering | The words in the current message | Easy to build | Only sees the current message and misses rephrasings |
| AI intent detection | The whole conversation | Can catch off-topic or manipulative requests without flagged words | More advanced to build than phrase matching |
Two ways to enforce an out-of-scope boundary, as described in the source
Watching Usage Patterns Over Time
Every defense so far has judged a message or a reply. Anomaly detection judges behavior. The source describes using an algorithm to identify unusual prompts, and it notes that bad actors can be detected by their usage patterns, not only by individual inputs and outputs. A single message can look ordinary while the sequence of messages from one user does not.
The source's example is a user who sends many similar-looking requests in a short period of time. A person doing this might be searching for a prompt that gets past the safety filters, because each variation is a small test of where the filter draws its line. Each message on its own may pass. The pattern across the messages is what gives the behavior away.
Mistakes Beginners Make with AI Safety Design
Common mistake Treating a check on the input as the whole defense
What it looks likeA chatbot screens every incoming message, then passes the model's reply straight to the user.
Why it failsAn input can pass every check and still lead to a harmful reply, so the danger can enter from the model's side as well.
Do this insteadPlace guardrails on both the input and the output of the model.
Common mistake Running generated code directly on the main machine
What it looks likeThe application executes a script the model wrote on the same machine the user depends on.
Why it failsGenerated code cannot be fully vouched for before it runs, so any harm it causes lands on the user's main machine.
Do this insteadRun generated code only inside a virtual machine separated from the user's main machine.
Common mistake Executing a database change before a person has approved it
What it looks likeThe AI proposes an UPDATE, and the system runs it immediately.
Why it failsCommands that change stored data can have consequences that are hard or impossible to undo.
Do this insteadHold any query containing DELETE, DROP, or UPDATE until a human explicitly approves it.
Common mistake Relying only on keyword filtering for scope
What it looks likeA support bot blocks the phrase 'immigration' but answers a politically charged question phrased with other words.
Why it failsKeyword filtering only sees the words in the current message, so rephrasings can slip past it.
Do this insteadAdd intent-based checks that look at the whole conversation, and send suspicious requests to a human operator.
Common mistake Judging each message in isolation and ignoring the pattern across many messages
How the Protections Fit Together
The defenses in this article are most useful as a set. Isolation limits where harmful generated code can act. Human approval controls which impactful commands run. Out-of-scope definitions and guardrails shape what the application accepts and says. Anomaly detection watches the user over time, catching patterns that single-message checks would miss.
Practice: Tracing a Risky Request
An AI assistant with database access receives this sequence from one user over a few minutes: a request to show customers in a region, then twelve nearly identical requests that differ only in one search phrase, then a request to UPDATE a customer record. For each step, name the protection that should act and say whether it stops the step on its own, flags it, or only contributes to a wider decision.
Hints
- Ask which layer judges single messages and which layer judges behavior over time.
- The UPDATE is an impactful command, so consider where a human decision point must sit before it runs.
- The twelve similar requests may each look ordinary when read alone.
Key Takeaways
- Generated code should be treated as untrusted and run only inside a virtual machine separated from the user's main machine, so that any harm stays confined to that machine.
- Commands that change stored data, such as those containing DELETE, DROP, or UPDATE, should not run without explicit human approval, and the approval must sit before execution.
- Guardrails belong on both inputs and outputs, because an input can pass every check and still lead to a harmful reply.
- Defining out-of-scope topics, enforced by keyword filtering or by AI that reads the whole conversation, and watching usage patterns over time add protection that single-message checks cannot provide.