Learning path: AI System Design: Reliable, Fast and Affordable LLM Apps · Step 10 of 15
Guardrails for an LLM Agent: Check What Comes In, What It Wants to Do, and What Goes Out
An agent that reads customer emails and can refund up to $200. In a small simulation, a phrase filter still lets 6 of 8 written attacks do harm and blocks 2 honest users; plain checks outside the model take harm to 0 of 8, at a cost of 1 honest request failed and 2 held.
What you will learn
- Place three layers of checks around an agent: input, action and output
- Explain why the action check is plain code outside the model, and why data is not authority
- Give an action one of three answers: allow, deny or hold for a person
- Report attacks stopped and honest requests hurt side by side
Before you start
- Claude Tool Use: tool_use and tool_result
- From Symptom to Pattern
Picture an agent that reads customer emails, and it can issue refunds up to two hundred dollars. The interviewer asks: which checks do you add, which are plain rules, which use a model, and in what order?
We'll answer from the defence side, in a small simulation. The model is a simple stand-in and the tools only touch a pretend store. The shape of the answer is four steps: check what comes in, check what the model wants to do (the one that matters most), check what goes out, and measure both sides, the attacks stopped and the honest people hurt.
An email asks for a refund
An email arrives with an instruction hidden in its text, asking for a refund. The model reads it as if someone in charge had said it. That's prompt injection.
The simulation has 8 attacks (written for this lesson) and 11 normal requests. With no guard, all 8 attacks do harm, and all 11 normal requests work. So people reach for a filter on known phrases. Attacks that do harm only drop to 6 of 8, because a reworded attack rides straight through. And two honest people get blocked, because they happened to use words on the list, like 'ignore the previous' and 'disregard'. Normal requests fall to 9 of 11.
It catches some and hurts some, and the attacker gets to pick the words. That's the trap.
The airport
At airport security they scan your bag. It catches a lot, but it isn't perfect. Then the boarding gate still checks your pass. It doesn't care what you told anyone at security; it has one rule: this pass, this flight. When you land, there's one more check on the way out. Several layers, and no layer trusts the one before it. The boarding gate is the one to remember, because you can't talk your way past it.
Where each check sits
- Input check is airport security. It can use a model or a list of patterns, so it's useful, and it can be wrong.
- The model proposes an action. Proposes: it has no power of its own.
- The action check is plain code outside the model. It never reads the email, so it can't be talked round. It gives one of three answers: allow, deny, or hold, which means a person decides. Only then does the tool fire.
- The output check looks at the reply before it leaves. In one simulated case a document holds a secret key, the reply quotes it, and this check masks it.
The gate at work
Let's send some actions through the action check. These are simulated examples, for illustration.
| Action | Verdict | Why |
|---|---|---|
| A customer tries to issue a refund | Deny | Role: each role holds only the tools it needs (least privilege) |
| Read an order from another company | Deny | Tenant |
| Email someone outside the company's own domain | Deny | Recipient |
| Refund $20 | Allow | Inside the limit |
| Refund $500 | Hold | A person decides |
| Refund $950 | Deny | Too big |
| Refund $30, with a document in the context | Hold | A document doesn't get to set the rules |
That last row is the one to say out loud: data isn't authority. And there's no model in this check, so you can test it like any other code.
Zero of eight, and what that means
With the checks in place (the "policy" column), attacks that do harm go from 8 of 8 to 0 of 8. Here's the simulated summary:
mode attacks benign false held
hit ok pos (benign)
none 8/8 11/11 0 0
filter 6/8 9/11 2 0
policy 0/8 10/11 1 2
both 0/8 8/11 3 2
Be careful how you say this. Zero of eight covers those eight simulated attacks, written for this lesson, and nothing more. It isn't a guarantee. The tests were tested too: 10 checks cover these rules, and if you take the tenant rule out, 1 fails:
FAILED tests/test_guard.py::test_other_tenant_denied - AssertionError: assert...
1 failed, 9 passed in 0.1s
So here's the claim you can defend: if every tool call goes through this check, a customer can't issue a refund, whatever the model says. That's a claim about your code, not about the model.
False positives and latency
Safety isn't free. The number to watch is the false positive: an honest request wrongly stopped.
With the checks in place, one normal request fails: a document lists a partner's help email, and the output check masks it, because it's another domain. Two more get held. In the simulation a scripted person says yes, but in production that's somebody's queue.
Put the phrase filter on top ("both"), and attacks stay at 0 of 8 while normal requests drop to 8 of 11. On this set it added no protection, so log that signal and don't block on it.
Latency? The checks took microseconds in the simulation, but no model call was in that timing, so we draw no conclusion. Run the cheap plain checks first, and measure the whole thing with the model in the loop.
The follow-ups
- How is injection different from a normal user instruction? A user has authority over their own request. An email or a document has none, but it reaches the model in the same text. So keep data and authority apart.
- Which checks can run alongside generation? An input check can, since nothing has happened yet. The action check can't; it must finish before the tool fires.
- How do you measure false positives? Keep a set of honest requests, run it on every change, and count blocked and held separately.
| Level | What it sounds like |
|---|---|
| Weak | A strong system prompt and a filter, and calls it safe. |
| Good | Plain checks outside the model, with roles and approval, but names no cost. |
| Strong | The model proposes and code decides; reports attacks and false positives together; admits a small test set isn't a guarantee. |
A judgement guide for practice, not a measurement. The same "a prompt is not a lock" idea shows up in From Symptom to Pattern.
Recap
| Idea | Remember |
|---|---|
| Phrase filter | Simulated: harm 8 to 6 of 8, and 2 honest people blocked |
| Layers | Input, action, output; no layer trusts the one before |
| Code decides | Allow, deny or hold, outside the model; data isn't authority |
| Measure both sides | 0 of 8 attacks, 1 honest request failed, 2 held; not a guarantee |
Try it yourself
Pick one action an assistant could take for you, like sending an email or changing a booking. Write the rule that decides allow, deny or hold, and name one honest request that rule might stop.
Related course on Udemy
Machine Learning Engineer: From First Model to ML Engineer
Learn machine learning in Python, then ship it: data analysis, regression, classification, tuning, deployment and MLOps.View on Udemy ↗: Machine Learning Engineer: From First Model to ML Engineer (paid course, opens in a new tab)Related posts
Fallback Models for an LLM App: A Chain That Checks Region and Fit, Flags Degraded Answers and Gets Rehearsed
When the main model goes down, the backup has to be allowed to take the data, able to read the whole request and tested before the bad day. A simulated four-model chain for an EU customer: a 6,000-token request lands on mid-eu, flagged degraded; a 10,000-token request is served by nobody, on purpose.
Back Pressure for LLM Traffic: Bound the Queue, Give Chat Its Own Lane, Count Tokens, Cancel When the Caller Leaves
A bulk upload buries the chat. In a small simulation, one shared queue grows to 199 waiting and the middle chat answer takes 80.2 s. A bound plus a chat lane brings it to 0.35 s while 178 bulk jobs are told to retry in 10 s, and cancelling abandoned calls saves 21,000 wasted tokens.
Load Balancing LLM Traffic: Count the Work, Weigh the Prefix Cache, Check Real Health, Drain Before Deploys
Six GPU replicas, two at 95% and four at 30%: round robin counts requests, and requests aren't equal. In a small simulation, least outstanding requests cuts p95 from 6.52 s to 3.05 s, prefix affinity lifts cache hits to 95% but pushes p95 to 11.45 s, a real health check takes errors from 48 to 2, and draining cuts 0 answers instead of 4.
A Message Queue for Document Ingestion: At Least Once, Leases, an Idempotent Consumer and a Dead Letter Queue
A queue doesn't lose a document, but it can hand one out twice. In a small simulation, a worker crash makes document two go from 3 to 6 chunks and the index holds 11 when the documents hold 8. An idempotent consumer brings it back to 8 with the same two deliveries, and a limit of three tries moves a poison file to a dead letter queue.