Context Compression & Distillation for Security AI
Reduce LLM token usage by 80% with context compression techniques. Learn extractive summarization and hierarchical compression for security logs.
Context compression and distillation are essential techniques for security engineers working with Large Language Models. Security data—logs, alerts, threat intelligence, and documentation—often exceeds LLM context window limits, requiring intelligent strategies to preserve critical information while reducing token consumption.
Effective compression enables security teams to process larger volumes of data, reduce API costs by 60-80%, and improve response latency without sacrificing the semantic richness needed for accurate security analysis. These techniques are foundational for building scalable AI-powered security systems that can handle enterprise-scale security operations. For foundational concepts about how LLMs process security data, see LLM Fundamentals for Security.
Why Context Compression Matters for Security Operations
Security data presents unique challenges for LLM processing that standard text compression approaches cannot address:
| Challenge | Impact on AI Processing | Compression Solution |
|---|---|---|
| Log verbosity | Exceeds context window limits within minutes | Structured summarization with field selection |
| Repetitive alert patterns | Wastes 40-60% of tokens on duplicates | Intelligent deduplication and aggregation |
| Irrelevant metadata fields | Dilutes security signal with noise | Schema-aware field filtering |
| Historical context requirements | Requires extensive lookback for correlation | Hierarchical multi-level summarization |
| Multi-source SIEM correlation | Combines heterogeneous data from 10+ systems | Cross-source semantic distillation |
| Real-time processing demands | Latency constraints limit processing time | Pre-computed compression with caching |
According to NIST SP 800-92 Guide to Computer Security Log Management, enterprise systems generate millions of log entries daily. Without compression, processing even a single incident investigation could consume an entire context window.
Core Compression Techniques
Extractive Summarization
Extractive summarization selects the most relevant portions of security data without modification, preserving exact IOCs, timestamps, and technical details. This approach is preferred when precision is critical and original wording must be maintained for forensic or compliance purposes.
Best suited for:
- Firewall and IDS/IPS log analysis
- Malware detection alerts
- Authentication event processing
- Compliance audit trails
| Extraction Method | Description | Compression Ratio | Precision | Use Case |
|---|---|---|---|---|
| Semantic similarity | Embed entries and rank by relevance to security queries | 80-95% | High | General log analysis |
| Keyword matching | Filter entries containing security-relevant terms | 70-90% | Medium | Known threat patterns |
| Severity filtering | Select entries above severity threshold | 60-80% | High | Alert prioritization |
| Entity extraction | Keep entries mentioning critical assets | 75-90% | High | Asset-focused investigation |
| Temporal windowing | Select entries within incident timeframe | Variable | High | Timeline reconstruction |
Extraction workflow:
- Embed log entries — Convert each entry to vector representation using security-tuned embedding models
- Define security queries — Create reference embeddings for threat categories (authentication failures, malware, exfiltration, privilege escalation)
- Calculate relevance scores — Compute similarity between entries and security queries
- Rank and select — Return top-k entries by security relevance score
- Preserve ordering — Maintain chronological sequence for timeline analysis
Abstractive Summarization
Abstractive summarization generates condensed representations that capture essential meaning, reducing token count by 70-90% while maintaining semantic fidelity.
Key considerations for security contexts:
- Preserve all IP addresses, domains, and hashes exactly
- Maintain temporal relationships between events
- Retain severity and risk classifications
- Include attack technique references (MITRE ATT&CK)
| Summary Component | Preservation Priority | Compression Approach | Quality Check |
|---|---|---|---|
| IP addresses | Critical (100%) | Extract verbatim | Regex validation |
| Domain names | Critical (100%) | Extract verbatim | DNS format check |
| File hashes | Critical (100%) | Extract verbatim | Hash format validation |
| Usernames | Critical (100%) | Extract verbatim | Entity recognition |
| Timestamps | High (100%) | Preserve or convert to relative | Sequence validation |
| Severity levels | High (100%) | Map to standard scale | Classification check |
| MITRE ATT&CK IDs | High (95%+) | Extract technique references | ID format validation |
| Event descriptions | Medium (80%+) | Abstractive summarization | Semantic similarity |
Abstractive summary structure:
- Timeline overview — Chronological sequence of key events with timestamps
- Key entities — Systems, users, and network assets involved
- Attack indicators — IOCs extracted and categorized by type
- Pattern analysis — Correlations and relationships between events
- Investigation steps — Recommended next actions based on findings
Hierarchical Compression
Hierarchical compression builds multi-level summaries for different context requirements, enabling efficient retrieval at varying granularity levels. Each compression level reduces token count while maintaining the semantic fidelity required for that use case.
The following diagram illustrates the hierarchical compression workflow from raw logs to indexed metadata:
flowchart TB
subgraph L0["L0 - Raw (100% tokens)"]
raw[Full Log Entries]
end
subgraph L1["L1 - Filtered (40-60% tokens)"]
filtered[Security-Relevant Fields]
end
subgraph L2["L2 - Summarized (10-20% tokens)"]
summarized[Event Clusters & Patterns]
end
subgraph L3["L3 - Distilled (2-5% tokens)"]
distilled[Key Findings & IOCs]
end
subgraph L4["L4 - Indexed (<1% tokens)"]
indexed[Metadata & Search Terms]
end
raw -->|"Remove non-security fields"| filtered
filtered -->|"Group by signature/time"| summarized
summarized -->|"Extract key findings"| distilled
distilled -->|"Generate metadata"| indexed
raw -.->|"Forensic Analysis"| L0
filtered -.->|"Incident Investigation"| L1
summarized -.->|"Threat Hunting"| L2
distilled -.->|"Executive Briefing"| L3
indexed -.->|"Discovery & Triage"| L4
Hierarchical compression workflow:
- L0 → L1 (Filtering) — Remove non-security fields, keeping timestamp, IPs, user, action, result, severity, message per NIST SP 800-92 guidelines
- L1 → L2 (Clustering) — Group similar events by signature, source, or time window; generate cluster summaries
- L2 → L3 (Distillation) — Extract key findings, IOCs, and attack patterns; discard supporting detail
- L3 → L4 (Indexing) — Generate metadata, search terms, and entity references for discovery
Level selection criteria:
- Forensic deep-dive → L0 (Raw) with unlimited token budget, acceptable delay
- Active investigation → L1 (Filtered) with 40-60% token budget, near real-time
- Threat hunting query → L2 (Summarized) with 10-20% token budget, real-time
- Executive briefing → L3 (Distilled) with 2-5% token budget, immediate
- Initial triage → L4 (Indexed) with <1% token budget, immediate
Semantic Chunking
Semantic chunking divides content based on meaning rather than arbitrary token or character boundaries, preserving logical units essential for security analysis. This approach follows research on semantic text segmentation applied to security contexts.
Security-aware chunking strategies:
- Attack phase boundaries — Separate reconnaissance, exploitation, and post-exploitation activities
- Session boundaries — Keep authentication sessions intact
- Transaction boundaries — Maintain complete request-response pairs
- Temporal boundaries — Group events within incident timeframes
| Separator Type | Priority | Description | Security Rationale |
|---|---|---|---|
| Section headers | Highest | Major document divisions | Preserve report structure |
| Horizontal rules | High | Incident boundaries | Keep incidents separate |
| Paragraph breaks | Medium | Logical groupings | Maintain context coherence |
| Alert markers | Medium | Alert boundaries | Preserve alert integrity |
| Event markers | Medium | Event boundaries | Keep events atomic |
| Sentence boundaries | Lowest | Last resort splitting | Avoid mid-sentence breaks |
Chunking parameters:
- Chunk size — 500-1500 tokens depending on retrieval granularity needs
- Overlap — 10-20% overlap to preserve context across chunk boundaries
- Metadata preservation — Attach source, timestamp, and severity to each chunk
Security-Specific Compression Patterns
These patterns are essential for SIEM-LLM integration and advanced RAG implementations that need to process large volumes of security telemetry efficiently.
Log Compression Strategies
Effective log compression for security AI requires understanding the structure and importance of different log types:
| Log Type | Compression Ratio | Critical Fields to Preserve | Recommended Approach |
|---|---|---|---|
| Syslog (RFC 5424) | 60-70% | Priority, timestamp, hostname, message | Field filtering + deduplication |
| Windows Event Logs | 50-60% | Event ID, timestamp, user, outcome | Event ID clustering |
| AWS CloudTrail | 40-50% | Event name, user identity, resources | API call aggregation |
| Zeek/Bro Network Logs | 70-80% | Conn ID, IPs, ports, protocol info | Connection summarization |
| NGINX/Apache Access Logs | 80-90% | IP, timestamp, request, status, size | Request pattern aggregation |
Temporal windowing approach:
- Define window size — Typically 30-120 seconds depending on log volume
- Group entries by window — Collect all entries within each time bucket
- Generate window summary — Extract time range, event count, unique sources, severity distribution
- Select representative messages — Keep 2-3 most informative entries per window
- Preserve anomalies — Always include entries that deviate from patterns
Compression output structure:
| Output Field | Description | Token Impact |
|---|---|---|
| Time range | Start and end of window | ~10 tokens |
| Event count | Number of entries in window | ~5 tokens |
| Unique sources | Deduplicated source list | Variable |
| Severity summary | Distribution of severity levels | ~20 tokens |
| Top messages | Representative log entries | ~50-100 tokens |
Alert Aggregation
Security alerts often exhibit high redundancy. Intelligent aggregation reduces noise while preserving actionable information per SANS alert fatigue research.
| Aggregation Dimension | Grouping Logic | Compression Benefit | Information Preserved |
|---|---|---|---|
| Signature-based | Same alert rule/signature | 70-90% reduction | Attack type, severity |
| Time-bucketed | 15-minute windows | 50-70% reduction | Temporal patterns |
| Source-based | Same source IP/host | 60-80% reduction | Attacker behavior |
| Target-based | Same destination | 60-80% reduction | Asset exposure |
| Campaign-based | Related IOCs | 80-95% reduction | Attack scope |
Aggregated alert structure:
- Signature — Common alert rule or detection name
- Maximum severity — Highest severity in group (never downgrade)
- Time range — First seen to last seen timestamps
- Count — Total alerts in group
- Unique sources — Deduplicated source IPs
- Unique destinations — Deduplicated target IPs
- Sample alert — One complete alert for full context
Threat Intelligence Distillation
Threat intelligence feeds often contain extensive context that must be distilled for efficient LLM consumption:
| TI Source Type | Raw Size (typical) | Distilled Size | Key Elements to Retain |
|---|---|---|---|
| STIX/TAXII Feeds | 50-500 KB per report | 2-10 KB | IOCs, TTPs, relationships |
| MISP Events | 10-100 KB | 1-5 KB | Attributes, galaxies, correlations |
| YARA Rules | 5-50 KB per rule set | 0.5-2 KB | Rule names, conditions, meta |
| Sigma Rules | 2-10 KB per rule | 0.3-1 KB | Detection logic, references |
STIX object distillation priorities:
| STIX Object Type | Retention Priority | Key Fields to Extract | Compression Ratio |
|---|---|---|---|
| Indicator | Critical | Pattern, valid_from, labels | 60-70% |
| Attack-pattern | High | Name, MITRE ID, kill chain phases | 70-80% |
| Malware | High | Name, types, aliases | 75-85% |
| Threat-actor | Medium | Name, aliases, motivations | 80-90% |
| Campaign | Medium | Name, objectives, first/last seen | 80-90% |
| Relationship | Low | Source, target, relationship type | 85-95% |
Distillation workflow:
- Parse STIX bundle — Extract all objects from bundle
- Filter by type — Prioritize indicators, attack patterns, and malware
- Extract key fields — Keep only essential attributes per object type
- Preserve relationships — Maintain links between related objects
- Validate completeness — Ensure all IOCs are retained
Incident Timeline Compression
Incident timelines require special handling to maintain chronological accuracy while reducing verbosity. Grouping by MITRE ATT&CK tactics provides natural compression boundaries.
| ATT&CK Tactic | Typical Event Volume | Compression Approach | Key Outputs |
|---|---|---|---|
| Reconnaissance | Low | Preserve all | External scanning sources |
| Initial Access | Low-Medium | Preserve all | Entry vectors, exploits |
| Execution | Medium | Summarize by process | Command patterns |
| Persistence | Low | Preserve all | Persistence mechanisms |
| Privilege Escalation | Low | Preserve all | Escalation techniques |
| Defense Evasion | Medium-High | Cluster by technique | Evasion methods |
| Credential Access | Low-Medium | Preserve all | Compromised accounts |
| Discovery | High | Summarize by target | Enumeration scope |
| Lateral Movement | Medium | Preserve all | Movement paths |
| Collection | Medium | Summarize by data type | Data targets |
| Exfiltration | Low | Preserve all | Exfil channels, volumes |
| Impact | Low | Preserve all | Damage assessment |
Timeline compression output:
- Phase name — MITRE ATT&CK tactic
- Time range — First to last event in phase
- Event count — Total events in phase
- Key IOCs — Unique indicators extracted
- Phase summary — Abstractive summary of phase activity
Implementation Best Practices
Token-Aware Processing
Accurate token counting is essential for effective compression. Different models use different tokenization schemes—GPT-4 uses cl100k_base encoding, while Claude uses a proprietary tokenizer per Anthropic's documentation.
| Tokenization Tool | Provider | Accuracy | Use Case |
|---|---|---|---|
| tiktoken | OpenAI | Exact for GPT models | Production token counting |
| Anthropic API | Anthropic | Exact for Claude | Claude-specific applications |
| Character estimation | N/A | ~4 chars/token | Quick estimates |
| Hugging Face tokenizers | Various | Model-specific | Open source models |
Budget fitting algorithm:
- Count tokens per section — Calculate exact token count for each content section
- Sort by priority — Order sections by importance (IOCs first, context second)
- Greedy selection — Add sections until budget exhausted
- Truncation fallback — If final section exceeds remaining budget, truncate intelligently
- Validate completeness — Ensure critical information not lost
Caching and Memoization
Cache compressed representations to avoid redundant processing. Compression is computationally expensive, especially for LLM-based abstractive summarization.
| Caching Strategy | TTL | Use Case | Cache Key |
|---|---|---|---|
| Content hash | 1-24 hours | Static logs | SHA-256 of raw content |
| Time-windowed | 5-15 minutes | Streaming logs | Time bucket + source |
| Query-specific | 30-60 minutes | Investigation context | Query hash + data hash |
| Session-scoped | Session duration | Interactive analysis | Session ID + content hash |
Cache invalidation triggers:
- New data arrival — Invalidate when source data updates
- TTL expiration — Automatic expiration based on data volatility
- Manual refresh — User-triggered recompression for fresh analysis
- Schema changes — Invalidate when compression format changes
Progressive Disclosure
Progressive disclosure patterns provide context at increasing detail levels, allowing AI systems to request more information as needed.
| Detail Level | Token Budget | Content Included | Use Case |
|---|---|---|---|
| Summary | ~100 tokens | Title, severity, key finding | Initial triage |
| Overview | ~500 tokens | Summary + affected assets, timeline | Alert review |
| Detailed | ~2000 tokens | Overview + IOCs, techniques, evidence | Investigation |
| Full | ~10000 tokens | Complete incident data | Deep forensics |
Progressive disclosure workflow:
- Start with summary — Provide minimal context for initial assessment
- Expand on request — Increase detail level when AI needs more information
- Section-specific expansion — Allow drilling into specific sections (timeline, IOCs, etc.)
- Lazy loading — Only retrieve full detail when explicitly requested
Quality Preservation Metrics
Compression must maintain the fidelity needed for security decisions. The following diagram illustrates the quality validation workflow:
flowchart LR
subgraph Input
orig[Original Content]
comp[Compressed Content]
end
subgraph Validation
sem[Semantic Similarity]
ioc[IOC Extraction]
time[Timestamp Check]
ratio[Compression Ratio]
end
subgraph Gates
pass{Pass All?}
use[Use Compressed]
fall[Fallback to Original]
end
orig --> sem
comp --> sem
orig --> ioc
comp --> ioc
comp --> time
comp --> ratio
sem --> pass
ioc --> pass
time --> pass
ratio --> pass
pass -->|Yes| use
pass -->|No| fall
Critical quality thresholds:
- Entity preservation — Named entities retained vs. original (> 95%, monitored via NER comparison)
- Temporal accuracy — Timestamps and sequences correct (100%, sequence validation)
- Severity fidelity — Risk levels accurately represented (100%, classification check)
- Causal relationships — Attack chains preserved (> 90%, graph comparison)
- Actionable details — IOCs and remediation steps retained (100%, pattern matching)
- Semantic similarity — Cosine similarity of embeddings (> 0.85, embedding comparison)
- Reconstruction accuracy — Human evaluation of compressed content (> 90%, expert review)
Quality validation workflow:
- Compute semantic similarity — Use embedding models like Sentence Transformers to calculate cosine similarity between original and compressed content
- Extract and compare IOCs — Use regex patterns to identify all indicators (IPs, domains, hashes, emails) in both versions
- Verify timestamp retention — Compare extracted timestamps to ensure temporal integrity
- Calculate compression ratio — Measure character/token reduction achieved
- Determine pass/fail — Apply minimum thresholds for each metric
Automated validation gates:
- Pre-commit validation — Validate before sending compressed data to LLM
- Quality alerts — Notify when compression falls below thresholds
- Fallback to original — Use uncompressed data if quality too low
- Audit logging — Record compression quality for compliance
Anti-Patterns to Avoid
Security compression introduces unique risks that can compromise investigation integrity:
-
Lossy compression of IOCs — Never compress away indicators of compromise, file hashes, IP addresses, or domain names. These are irreplaceable for threat correlation and blocking.
-
Temporal distortion — Maintain accurate timelines for incident reconstruction. Even small timestamp errors can invalidate forensic analysis per NIST SP 800-86 Guide to Integrating Forensic Techniques.
-
Context stripping — Preserve enough surrounding context for accurate security decisions. An alert without context may be misclassified.
-
Over-aggressive deduplication — Similar events may represent distinct attack stages. Authentication failures from the same IP at different times could indicate password spraying vs. a single failed login.
-
Severity downgrading — Never reduce severity classifications during compression. A critical alert summarized as informational could delay response.
-
Breaking attack chains — Preserve relationships between events that form attack patterns. Isolated events lose their significance.
Compression Benchmarks
| Data Type | Original Tokens | Compressed Tokens | Compression Ratio | Quality Score |
|---|---|---|---|---|
| Firewall logs (1 hour) | 50,000 | 5,000 | 90% | 0.92 |
| SIEM alerts (100 alerts) | 25,000 | 3,500 | 86% | 0.94 |
| Incident report | 15,000 | 2,000 | 87% | 0.91 |
| Threat intel feed (daily) | 100,000 | 8,000 | 92% | 0.89 |
| Vulnerability scan | 80,000 | 6,000 | 92% | 0.93 |
Tools and Libraries
| Tool | Purpose | Integration |
|---|---|---|
| LangChain | Text splitting and document transformation | Python SDK |
| LlamaIndex | Node parsing and hierarchical indexing | Python SDK |
| tiktoken | Token counting for OpenAI models | Python library |
| Anthropic Tokenizer | Token counting for Claude models | API |
| spaCy | Named entity recognition for IOC extraction | Python NLP |
| Sentence Transformers | Semantic similarity and embedding | Python ML |
References
- NIST SP 800-92: Guide to Computer Security Log Management
- NIST SP 800-86: Guide to Integrating Forensic Techniques into Incident Response
- MITRE ATT&CK Framework
- OWASP AI Security and Privacy Guide
- LangChain Text Splitters Documentation
- LlamaIndex Node Parsers
- Anthropic Context Window Best Practices
- OpenAI Tokenizer Tool
- STIX/TAXII Cyber Threat Intelligence Standards
- MISP Threat Intelligence Platform
- Sigma Detection Rules
- Semantic Chunking Research (arXiv)
- Microsoft Security Event Log Reference
- AWS CloudTrail User Guide
- Zeek Network Security Monitor Documentation