Skip to main content
HS
HiSETSuccess
LeetCode Prep

AI & FDE Interview Room

Role paths, mocks, cost studio, chapters, cases, and staff-level depth.

Case study · Architect · 35 min

Prompt Injection Defense for Tool-Using Agents

Hardening an agent against direct and indirect prompt injection when it reads untrusted documents and calls powerful tools.

safetysecurityagents

Interview opener

Harden an app that puts untrusted documents into context.

  1. 1.Indirect injection via a retrieved PDF — mitigations?
  2. 2.Can the model escalate tool privileges via injection?
  3. 3.How do you test defenses continuously?

Strong signals

Hit 0/3

Weak signals (avoid)

  • Just a better system prompt
  • Trusting the model to 'not obey' malicious text

Requirements

  • Threats: jailbreaks, malicious PDF instructions, tool exfiltration
  • Assets: email send, CRM write, code execution sandbox
  • Goal: exploit success < 1% on red-team suite; zero high-impact tool abuse in prod
  • UX: minimize false refusals on benign support workflows

Back-of-envelope Estimation

MetricCalculationResult
Red-team cases400400
CI cost/run400 × 3K × $2/1M~$2.40
Prod QPS2020
Guard model cost20 × 500 tok × 86400 × $0.2/1M~$173/day
High-impact tools/day~500extra auth friction OK

API Design

ComponentMethodDescription
tool_gatewayRPCSchema + policy engine before side effects
POST /v1/guard/classifyPOSTInjection/jailbreak risk score
POST /v1/tools/{id}/confirmPOSTHuman approval for high-risk tools

Data Model

EntityKey fieldsNotes
policiestool_id, risk, requires_humanVersioned
untrusted_spansdoc_id, offset, labelRetrieved content tags
security_eventsts, rule, actionSIEM

High-level Design

Defense in depth
User prompt ──► allow/deny filters
Untrusted docs ─► sanitize + wrap in <untrusted> delimiters
Planner LLM ───► can propose tools only from allow-list
Tool gateway ──► validate args; strip URLs; enforce authz
High-risk ─────► human confirm OR dual control
Output ────────► DLP scan before send
All layers emit security_events

Deep Dive: Indirect Injection

  • Treat retrieved text as data, never as instructions — dual-channel prompts
  • Content detectors for "ignore previous" patterns (imperfect; still useful)
  • Disable tools when context includes untrusted HTML/email until cleared
  • Capability tokens: tools receive short-lived scoped credentials, not master keys

No silver bullet

Assume the model can be socially engineered. Architectural isolation beats prompt-only defenses.

Bottlenecks at Scale

  • Guard model latency — run parallel with retrieval; cache clean docs
  • False positives frustrate users — tiered risk, not blanket bans
  • Red-team drift — refresh suite monthly with new exploits

Failure Modes & Monitoring

SLOTargetAlert
Red-team pass≥ 99%Block release
High-risk tool w/o confirm0Page
False refusal rate< 3%Warn UX
DLP hitstrackedWeekly review

Attempt the case aloud first. Then unlock a staff model answer outline.