Single logo
  • Courses
  • Blog
  • About
Join free on SkoolJoin free on Skool

Learning path · 15 steps

AI System Design: Reliable, Fast and Affordable LLM Apps
  1. 1
    Fallback Models for an LLM App: A Chain That Checks Region and Fit, Flags Degraded Answers and Gets Rehearsed
  2. 2
    Back Pressure for LLM Traffic: Bound the Queue, Give Chat Its Own Lane, Count Tokens, Cancel When the Caller Leaves
  3. 3
    Load Balancing LLM Traffic: Count the Work, Weigh the Prefix Cache, Check Real Health, Drain Before Deploys
  4. 4
    A Message Queue for Document Ingestion: At Least Once, Leases, an Idempotent Consumer and a Dead Letter Queue
  5. 5
    Caching for AI Apps: Four Layers, What Goes in Each Key, and How a Delete Travels
  6. 6
    Semantic Caching: Why a Similarity Threshold Can't Stop Confident Wrong Answers
  7. 7
    LLM Model Routing: Rules, a Classifier or a Cascade, Judged on Cost per Win and Every Topic
  8. 8
    Observability and Tracing for LLM Apps: A Span per Stage, Redaction First, Bad Traces Become Tests
  9. 9
    Cutting p95 and Cost in Half for an LLM App: Read the Bill, Match the Lever, Guard Success
  10. 10
    Guardrails for an LLM Agent: Check What Comes In, What It Wants to Do, and What Goes Out
  11. 11
    Embedding Migration With Zero Downtime: Two Indexes, a Dual-Write Backfill, a Shadow Check and a Flag
  12. 12
    Design a Multi-Tenant RAG Assistant, Part 1: Requirements, Rough Numbers and a Contract
  13. 13
    Design a Multi-Tenant RAG Assistant, Part 2: Tenant Isolation, a Scoped Cache and Walking an Outage
  14. 14
    A 500 MB PDF Takes 50 s to Answer, Get It to 2 s: Measure First, Then Promise Per Path
  15. 15
    A 500 MB PDF From 50 s to 2 s, the Fast Path: Queue, Status, Streaming and an Acceptance Test That Fails
  1. Blog
  2. /
  3. AI Engineering
  4. /
  5. AI System Design

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.

Chaand Sheikh

·27 November 2026AI System Design

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, model proposes, action check, tool, output check Input check model or patterns Model proposes an action Action check plain code allow / deny / hold Tool fires only if allowed Output check masks secrets before it leaves The action check never reads the email. It looks at who is asking, which tool and which details. The model proposes. Code decides.
Input, action, output. What you trust with the refund is the deterministic check in the middle.
  • 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.

ActionVerdictWhy
A customer tries to issue a refundDenyRole: each role holds only the tools it needs (least privilege)
Read an order from another companyDenyTenant
Email someone outside the company's own domainDenyRecipient
Refund $20AllowInside the limit
Refund $500HoldA person decides
Refund $950DenyToo big
Refund $30, with a document in the contextHoldA 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.

Attacks that did harm and normal requests that worked, by mode Attacks that did harm (of 8) Normal requests OK (of 11) none8 filter6 policy0 both0 none11 filter9 policy10 both8 Simulated: 8 written attacks, 11 normal requests
Read both halves together. The filter on top of the policy added no protection here and hurt three honest requests.

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.
LevelWhat it sounds like
WeakA strong system prompt and a filter, and calls it safe.
GoodPlain checks outside the model, with roles and approval, but names no cost.
StrongThe 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

IdeaRemember
Phrase filterSimulated: harm 8 to 6 of 8, and 2 honest people blocked
LayersInput, action, output; no layer trusts the one before
Code decidesAllow, deny or hold, outside the model; data isn't authority
Measure both sides0 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.

Tried the exercise? Share your answer or ask a question.

The StudyEasy AI Community is free to join, with the lesson files, notebooks and code.

Join free on Skool

Keep going in the learning path AI System Design: Reliable, Fast and Affordable LLM Apps · step 10 of 15

Previous
Cutting p95 and Cost in Half for an LLM App: Read the Bill, Match the Lever, Guard Success
Next post
Embedding Migration With Zero Downtime: Two Indexes, a Dual-Write Backfill, a Shadow Check and a Flag

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.

On this page

  • An email asks for a refund
  • The airport
  • Where each check sits
  • The gate at work
  • Zero of eight, and what that means
  • False positives and latency
  • The follow-ups
  • Recap
  • Try it yourself
Community · Free to join
Single logo

StudyEasy AI Community

Hosted onSkool

Learn AI engineering, share what you build, and connect with other learners.

  • Free resources: lesson files, notebooks and code

  • Ask questions and join engineering discussions

  • Meet real people learning and building with AI

Join free on Skool

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)

Single logo

Empowering Education, One Course at a Time

About
About usContact usFAQs
Legal
Terms of ServicePrivacy Policy

© 2026 StudyEasy Organization. All rights reserved.