resources

A Pragmatic Engineer’s Guide to Defending Agentic LLM Systems

1. Introduction: agent security is system security

Agentic LLM systems are useful because they can interpret messy inputs, reason over context, retrieve data, call tools, and trigger actions. Those same capabilities create failure modes that traditional ML systems do not fully cover: prompt injection, contaminated retrieval, unsafe tool use, data leakage, over-permissioned actions, and brittle automation under adversarial input.

This guide is for machine learning engineers who need to harden systems that use LLMs. The goal is not to make the model perfectly safe. The goal is to build a layered system where the model is constrained, observed, tested, and prevented from taking actions it should not take.

Core thesis: prompts guide behaviour, but architecture enforces safety.


2. Classify the system before choosing defences

Not every LLM system needs the same security posture. Start by classifying what the model is allowed to do and where its output flows.

System type Main risk Defences to prioritize
Structured extraction The model returns incorrect, unsupported, or adversarially influenced fields. Treat the model like a parser: strict schemas, enum validation, normalization, abstention, deterministic post-processing, adversarial extraction evals.
RAG and LLM workflows Untrusted content contaminates downstream prompts or decisions. Separate instructions from retrieved data, label retrieved text as untrusted, filter sources, check provenance, validate intermediate outputs.
Conversational agents Open-ended user behaviour pushes the model outside product scope, policy, or factual grounding. Define supported intents, ground responses in approved sources, handle refusals and escalation, constrain memory and personalization.
Tool-using agents Model output crosses into real-world execution. Use least-privilege tools, action schemas, allowlists, scoped credentials, confirmation gates, sandboxing, audit trails.

As systems move from extraction to action, prompts become less important as a defence boundary and external controls become more important.


3. Threat model: the attack surfaces that matter

3.1 Direct prompt injection

The user explicitly tries to override the intended behaviour of the system.

Examples:

Engineering takeaway: hostile instructions are expected input, not rare edge cases.


3.2 Indirect prompt injection and RAG contamination

The model reads malicious instructions from retrieved documents, websites, emails, tickets, PDFs, tool outputs, or database fields.

Examples:

Engineering takeaway: retrieved content is data, not authority.


3.3 Unsafe tool execution

The model calls the wrong tool, calls a tool with unsafe parameters, or takes an action the user did not intend.

Examples:

Engineering takeaway: tool calls need policy enforcement outside the model.


3.4 Data leakage and context exposure

The model exposes private, internal, personal, or cross-tenant information because sensitive data was placed in context, retrieved incorrectly, returned from a tool, or mixed across users.

Examples:

Engineering takeaway: context construction is a security boundary.


3.5 Over-trust in model output

Users and downstream systems may treat fluent model output as more reliable than it is, especially in legal, financial, medical, security, or operational workflows.

Engineering takeaway: high-stakes outputs need provenance, uncertainty handling, validation, and fallback paths. Confidence in tone is not confidence in correctness.


4. Layered defences

Defence in depth means every layer has a narrow job. The model can propose. The system should decide what is allowed.

4.1 Prompt and instruction layer

Use prompts to frame the task, not to carry the whole security model.

Key controls:


4.2 Data and context layer

Treat context construction as part of the security boundary.

Key controls:


4.3 Model output layer

Do not pass raw model output directly into downstream systems when the output affects data, decisions, or actions.

Key controls:


4.4 Tool and action layer

Tools are where model behaviour becomes system behaviour. This layer needs explicit enforcement.

Key controls:

The confirmation should describe the actual action, not the model’s vague intent.


4.5 System and operations layer

Production safeguards should assume that some attacks and model failures will get through earlier layers.

Key controls:


5. Evaluation and red teaming

Attacks should be part of normal testing, not a one-off review before launch. The goal is to measure whether the whole system behaves safely: prompt construction, retrieval, model output, validation, tool execution, permissions, and user experience.

5.1 Build adversarial datasets

Include examples for:

For each case, define the expected safe behaviour: refuse, abstain, escalate, ask for clarification, return null, draft without sending, or block the tool call.


5.2 Use layered metrics

Useful metrics include:

Track quality and safety together. A system that completes more tasks by taking unsafe actions is not better.


5.3 Convert findings into regression tests

A red-team finding is only useful if it becomes durable engineering work.

Workflow:

  1. capture the failure
  2. minimize the repro case
  3. define the expected safe behaviour
  4. add it to the eval set
  5. fix the relevant layer of the system
  6. run against existing quality benchmarks
  7. prevent regressions in CI

6. Production hardening checklist

Before launch


During rollout


After launch


Conclusion: build agents like production systems, not demos

The practical path is not to rely on the model to always choose safe behaviour. It is to design a system where untrusted content is contained, model outputs are validated, tools are mediated by policy, high-impact actions require approval, and failures become regression tests.

Prompts guide behaviour. Architecture enforces safety.