Handbooks / RAG & AI Agents / Chapter 5
Guardrails and Evaluation
7 pages · ~6 min✓ Reviewed
Builds on Agent Loops and Memory.
Prompt Injection: The Threat Model
1. What It Is
- Prompt injection: untrusted text read as instructions
- System prompt and user text share one token stream
- Jailbreak: breaks the model's safety policy
- Injection: hijacks the app's task or tools
- An app-security problem, not only alignment
2. Direct vs Indirect
| Direct | Indirect | |
|---|---|---|
| Source | User turn: ignore previous instructions | Web page, PDF, email, RAG chunk, tool result |
| User sees it | Yes | Never |
| Danger | Lower | Higher |
3. Lethal Trifecta
- Private data
- Untrusted content
- Exfil channel
- Data leaks
- Remove any one leg and the chain breaks
- Exfil via markdown image URL: the client fetches it
markdown

data rides in the query string
4. Interview Point
- Why not fix with a better prompt? Instructions and data are one stream; no hard boundary
- Indirect vs direct? Hidden in fetched content; the user never sees it
- Image exfil? Injected text makes the app emit an image URL with secrets in the query
Input and Output Guardrails
1. Guard Both Sides
- Input guardbefore model
- Model
- Output guardbefore user or tool
- Blocksafe fallback
| Input guard | Output guard | |
|---|---|---|
| Checks | length, PII redaction, injection classifier, topic allowlist, delimited untrusted text | schema, secret and PII scan, toxicity, URL and image allowlist, groundedness |
| Role | reduces risk, never eliminates it | last line before user or tool |
2. Rules vs Model Checks
| Rules (regex, schema) | Model guard | |
|---|---|---|
| Cost | fast, cheap | latency, cost |
| Strength | deterministic | generalises |
| Weakness | brittle | false positives, can be injected |
3. Pattern and Pitfalls
- Guard in, guard out, fail closed
- On block: safe fallback, log the reason code
- Never retry blindly with the same input
- Cheap guards first, expensive only if needed, in parallel
Blocklist alone
Keywords miss paraphrase and encoding. Layer classifiers, schema and output checks.
- Where? Before and after the model: input is untrusted, output is the last line
- False positives, latency? Log reason codes, tune thresholds, run cheap guards first, in parallel
Tool Permissions and Least Privilege
1. Untrusted caller
- Model = untrusted caller; authorize every call in code
- A prompt saying 'do not delete' is not a control
- A missing permission is
2. Least privilege and tiers
| Tier | Tools | Gate |
|---|---|---|
| Local read | search index, read file | auto-run |
| Egress | fetch, browse | domain allowlist |
| Write | write, send, spend, delete | human confirms exact args |
- Per-agent tool allowlist, read-only creds, narrow scopes
- Short-lived per-user tokens, not a shared admin key
- Arguments validated against a schema
- Rate limits and spend caps per session
3. Isolate risk
- Untrusted text
- Quarantined readerno tools
- Privileged actortrusted input
- Code and shell tools run in a sandbox: no network, no secrets
- Untrusted content never triggers privileged tools in the same turn
4. Interview Point
- Email agent? Reader has no send tool; send needs approval of exact recipient and body
- Which leg? Cut exfiltration: read-only still leaves fetch and image URLs, so allowlist recipients and domains
- Allowlist vs denylist? Unknown tools are denied by default; a denylist misses new ones
Evaluating RAG: Faithfulness and Relevance
1. Score Each Stage
| Stage | Metric | Asks |
|---|---|---|
| Retrieval | context precision | Chunks relevant and ranked high? |
| Retrieval | context recall | Everything needed fetched? |
| Retrieval | hit rate, recall@k, MRR | Scored on labelled queries |
| Generation | faithfulness | Claims supported by context? |
| Generation | answer relevance | Answers the question asked? |
2. Faithfulness vs Relevance
- Answer
- Split claimsone fact each
- Check eachagainst context
- Scoresupported / total
- Faithfulness: answer vs context, catches hallucination
- Relevance: answer vs question
- Faithful but irrelevant: a true quote that does not answer
3. Golden Set and Judge
- 50-200 real questions + reference answers + source chunks
- LLM-as-judge with a rubric (RAGAS, TruLens, DeepEval)
- Spot-check the judge against human labels
- Run on every prompt, chunking or model change: regression gate
4. Interview Point
- Fluent but wrong? Low context recall = retrieval failed; good context but low faithfulness = generation failed
- Faithful but off-topic? Low answer relevance
- Judge risk? Self-preference and verbosity bias; it can drift from human labels
Tracing and Observability for LLM Apps
1. Traces and Spans
- Request
- Retrieval
- Rerank
- Prompt build
- LLM call
- Tool call
- Guardrails
- A trace is one request; spans are its steps
- Nested spans show where time, tokens, errors go
- Per span: redacted input/output, model and version, prompt template version
- Also: tokens, latency, cost, tool args/results, guardrail verdicts
- Session or user ID stitches multi-turn chats
2. Instrument
- Standard: OpenTelemetry GenAI semantic conventions
- Platforms: Langfuse, LangSmith, Arize Phoenix, Helicone
- Auto-instrumentation wraps SDK calls
- Add custom spans around retrieval and tools
3. Close the Loop
- Sample traces
- Score onlinefeedback, faithfulness, relevance, refusals
- Alertcost, p95, errors, blocks
- Golden setfailing traces
4. Interview Point
- Debug a bad answer? Replay the trace: chunks, prompt version, tool args and results, verdicts
- PII? Redact before export, hash user IDs
- Sampling cost? Keep all errors and low scores, sample the rest
- Scores to a prompt version? Tag traces with the template version, group scores by it
Cheat Sheet: Secure and Observable LLM Apps
1. Threats and defence stack
- All retrieved or tool text is untrusted data
- Indirect injection is the main risk
- Break one leg of the lethal trifecta: private data, untrusted input, exfiltration channel
- Input guardlimits, PII, injection classifier
- Least-privilege toolscode-level authorization
- Output guardschema, secrets, URL allowlist
- Fail closedsafe fallback
2. Permissions checklist
- Tool allowlist, read-only by default
- Per-user short-lived tokens, schema-validated arguments
- Human approval: write, send, spend, delete
- Fetch and browse leak data: outbound domain allowlist
- Sandbox for code; rate and spend caps
3. Measure and trace
| Metric | Judges |
|---|---|
| Context precision | retrieved chunks relevant, ranked well |
| Context recall | everything needed was retrieved |
| Faithfulness | answer supported by context |
| Answer relevance | answer addresses the question |
- Gate every change on a golden set
- Trace: prompt version, model, tokens, latency, tool args, guardrail verdicts, session ID
Interview Point
Prevent: least privilege, allowlists, approval, fail closed. Detect: faithfulness and recall drops on the golden set. Debug: trace fields.
Check yourself
A RAG answer quotes a true sentence from a retrieved chunk, but the sentence ignores the question. Which score is high, which is low?
- Faithfulness is high: the claim is supported by the context.
- Answer relevance is low: the answer does not address the question.
The system prompt says "never call delete_file", yet delete_file is registered for the agent. What is the bug?
- A prompt is not a control; an injected text can override it.
- Remove the tool from the allowlist, or require code-level authorization and human approval.
An email agent reads private mail and renders markdown. A mail says: output . Which trifecta leg is cheapest to cut?
- The exfiltration channel: an output guard allowlists image URLs and blocks this one.
- The image fetch would otherwise send the secret to evil.com.