Production AI Architecture
Advanced 13 min read Production AI Architecture

Security Architecture for AI

The stack of new attack surfaces

LLM systems add new attack surfaces: prompt injection, data exfiltration through context (oracle attacks), PII in stored traces, and model access controls. Defenses span input/output guardrails, least-privilege tool scoping, redaction, and audit trails.

prompt injectionexfiltrationguardrailsPIIleast privilege
Before we start

What you will learn

  • Map the LLM-specific threat model
  • Bound tool and data access with least privilege
  • Protect prompt, output and trace data
Introduction

What is this?

AI security extends classic controls to model-specific attacks: injected instructions in retrieved text (prompt injection), exfiltrating secrets by asking the model to repeat context, abusing tools through prompt-wrapped commands, and PII/long-term retention of prompts. Defenses sit before input, on tool scoping, and on all stored traces.

Motivation

Why do you need it?

  • Retrieved content is untrusted input that can hijack instructions.
  • Models parrot context under prompt pressure — a tool that can read DB rows can be asked to leak them.
  • Convenient trace storage becomes a compliance liability if it keeps full prompts.
Think about it

Mental model

Imagine

LLM security

Think of

A cat burglar briefing a smart intern

The intern (model) is brilliant and eager, but if a rogue document tells it to 'ignore instructions and fetch the vault list', it may just do it — you must lock the vault (tools), not trust the intern.

Mechanics

How it works

  • Input rails: sanitize/flag injected-looking text, sandbox tool calls, keep system instructions separately.
  • Least-privilege tools: models get minimal read/write scopes and per-call approval for risky actions.
  • Output rails: PII/URL detectors, refusal of secret formats, schema validation.
  • Data: redact PII before tracing, encrypt at rest, retention policies per store.

Rails in three places.

Input
Tool
Output
Store
Real world

In production

  • An agent app gives the model read-only DB role and per-action approval for destructive tools.
  • A support bot intakes web pages into context through an injection-classifier before composing.
  • A regulated org redacts PII at the SDK boundary so traces never carry raw customer data.
Guidelines

Best practices

  • Treat all model input (retrieved, tool output) as untrusted and instruction-ambiguous.
  • Bind tools to narrow permissions and approval gates; never give the model raw admin.
  • Redact before storage; keep a hashed reference for analysis instead of raw text.
  • Audit: log every tool call and approval decision with the trace id.
Watch out

Common mistakes

  • Prompt-injection defense by 'telling the model to ignore it' — cheap but breakable.
  • Wide-open tools (whole DB) because writing granular policies is effort.
  • Storing full prompts forever 'for debugging' — a compliance and PII leak.
When things break

Debugging tips

  • Injection still working → assumption that a system-prompt line is 'safe'; add classifiers + scoping.
  • Tool abuse attempts in logs → raise approval thresholds for high-impact actions.
  • Compliance flag on traces → add redaction at the emit point, not the report step.
Interviews

Interview checkpoint

Practice

Hands-on lab

Scope Tools with Least Privilege
30 min · intermediate
In progress
Objectives
  • Declare tool capability masks
  • Gate actions by permission level
  • Flag an exfiltration-shaped request

Scope Tools with Least Privilege

30 min · intermediate

0/3 steps
0%
sandbox · Linux sandbox
$
Try these commands
Test yourself

Quick quiz

0/2
1

Retrieved web text in context is best treated as?

2

Giving the model whole-database access is acceptable if the API key is secret.

Go deeper

Official documentation