Monitoring for attacks, testing your defenses, and building a security practice
The security posture of your LLM application is only as good as your ability to detect when it fails and respond before harm is done.
Prevention ──▶ Detection ──▶ Response ──▶ Recovery
(Lessons 7-9) (This lesson) (Incident response) (Improvement)
Comprehensive logging is the foundation of detection:
| Field | Purpose | Example |
|---|---|---|
| Timestamp | Timeline reconstruction | 2026-04-13T14:30:00Z |
| User ID | Identify attackers | user_abc123 |
| Session ID | Group related requests | sess_xyz789 |
| Full prompt | Analyze attack payloads | (full text) |
| Full response | Check for data leaks | (full text) |
| Tool calls | Detect unauthorized actions | send_email(...) |
| Tokens used | Cost anomalies | 1,234 tokens |
| Validation flags | Track filter triggers | ["blocklist_hit"] |
Privacy note: Logging full prompts raises GDPR/privacy concerns. Consider hashing or redacting PII in logs, or using short retention periods.
What signals indicate a potential attack in progress?
Establish baselines for normal usage and flag deviations:
BASELINE_METRICS = {
"avg_input_length": 85, # characters
"avg_output_length": 320, # characters
"avg_session_messages": 4,
"tool_calls_per_session": 1.2,
"topics": ["orders", "returns", "shipping"],
}
ANOMALY_THRESHOLDS = {
"input_length": 3.0, # 3x standard deviation
"output_length": 3.0,
"session_messages": 2.5,
"tool_calls": 3.0,
}
A structured approach to testing your LLM application's security:
Red teaming should happen before launch, after every system prompt change, and periodically (quarterly minimum).
| Tool | Purpose | Type |
|---|---|---|
| Garak | LLM vulnerability scanner with built-in injection probes | Automated |
| promptfoo | Eval-driven testing framework with assertion checks | Automated |
| Custom scripts | Tailored payloads for your specific application | Manual + scripted |
| Manual testing | Creative, adaptive attacks by skilled testers | Manual |
# Garak: scan for vulnerabilities
garak --model_type openai --model_name gpt-4 \
--probes injection
# promptfoo: eval with injection tests
promptfoo eval --config injection-tests.yaml
Organize test cases by attack category for comprehensive coverage:
| Category | Examples | Count |
|---|---|---|
| Instruction override | "Ignore previous...", "New instructions..." | 5-10 |
| Role-play / persona | "You are DAN...", "Pretend to be..." | 5-10 |
| Encoding / obfuscation | Base64, ROT13, reversed text, leetspeak | 5-10 |
| Indirect injection | Payload in retrieved docs, emails, data | 5-10 |
| Tool-use abuse | Unauthorized tool calls, param manipulation | 5-10 |
| Prompt extraction | "Reveal instructions", "Complete: My rules are..." | 5-10 |
Target: 30-60 test cases for comprehensive coverage.
Integrate injection tests into your deployment pipeline:
# .github/workflows/prompt-security.yml
name: Prompt Injection Tests
on:
push:
paths:
- 'prompts/**'
- 'config/system_prompt.txt'
jobs:
injection-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Run injection tests
run: |
python -m pytest tests/injection/ \
--tb=short -v
- name: Run promptfoo eval
run: |
npx promptfoo eval \
--config tests/promptfoo.yaml
How to handle prompt injection vulnerabilities you discover:
OWASP LLM Top 10 provides a standard taxonomy. NIST AI Risk Management Framework provides governance guidance. Use these as references for your security program.
| Year | Defense Advance | Attack Advance |
|---|---|---|
| 2023 | Basic system prompts | Simple "ignore instructions" |
| 2024 | Delimiters, sandwich defense | Encoding, indirect injection |
| 2025 | Dual-LLM, classifiers | Multi-modal, multi-step, tool abuse |
| 2026+ | AI-powered detection | AI-crafted, adaptive attacks |
Security is a continuous practice, not a product.
| Lesson | Key Takeaway |
|---|---|
| 01-02 | Understand the threat: LLMs can't distinguish instructions from data |
| 03-04 | Know the attacks: direct, indirect, and their variants |
| 05-06 | Advanced techniques and real-world impact |
| 07 | Input validation: necessary but never sufficient |
| 08 | System prompt hardening: raise the bar for attackers |
| 09 | Architecture: limit blast radius when injection succeeds |
| 10 | Detection and red teaming: continuous security practice |
Defense-in-depth wins. No single technique solves prompt injection. Layer input validation, prompt hardening, architectural defenses, output filtering, and detection for a robust security posture.