A defensive guide to protecting retrieval-augmented applications against malicious instructions, unauthorized retrieval, and sensitive-data leakage.
Threat model
List assets, actors, entry points, trust boundaries, and possible impacts. This makes security work concrete instead of relying on a generic “secure the LLM” goal.
- Define the input and expected output.
- Keep each component responsible for one clear job.
- Measure the failure mode before optimizing it.
Indirect prompt injection
Retrieved documents can contain instructions designed to manipulate the model. Treat retrieved text as untrusted data and ensure it cannot silently change tool permissions or system policy.
Authorization-aware retrieval
A user should never retrieve a document merely because the model thinks it is relevant. The retrieval layer must enforce the caller’s permissions before context reaches the model.
Document trust boundaries
Separate trusted application instructions from untrusted documents and user input. Label sources clearly and avoid concatenating them into one undifferentiated instruction block.
Output controls
Validate structured outputs, apply policy checks to tool arguments, and consider sensitive-data detection where appropriate. The model should not be the only enforcement point.
Logging without leaking secrets
Observability must be designed with privacy in mind. Prefer identifiers and structured metadata over full prompts and retrieved documents when full content is not necessary.
Testing security
Create adversarial tests for prompt injection, unauthorized retrieval, data exfiltration, tool abuse, and malformed inputs. Repeat them after meaningful model or prompt changes.
Incident response
Define how to disable a tool, revoke credentials, quarantine a data source, and investigate traces if a security issue is detected.
Implementation checklist
- Write down the user task and the failure modes.
- Choose the simplest architecture that satisfies the requirement.
- Add validation and permission checks at system boundaries.
- Create a small representative evaluation set.
- Instrument latency, failures, cost, and quality signals.
- Document limitations and the next engineering improvement.
Key takeaway
The strongest AI engineering work is not defined by how many models or frameworks are used. It is defined by clear problem framing, reliable system boundaries, evidence from evaluation, and the ability to explain trade-offs. Use this guide as a starting point and validate every design against the requirements of your own application.