All articles
AI & LLM SecurityAI Foundations
Browse Knowledge Base

Context Compression & Distillation for Security AI

16 min read

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:

ChallengeImpact on AI ProcessingCompression Solution
Log verbosityExceeds context window limits within minutesStructured summarization with field selection
Repetitive alert patternsWastes 40-60% of tokens on duplicatesIntelligent deduplication and aggregation
Irrelevant metadata fieldsDilutes security signal with noiseSchema-aware field filtering
Historical context requirementsRequires extensive lookback for correlationHierarchical multi-level summarization
Multi-source SIEM correlationCombines heterogeneous data from 10+ systemsCross-source semantic distillation
Real-time processing demandsLatency constraints limit processing timePre-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 MethodDescriptionCompression RatioPrecisionUse Case
Semantic similarityEmbed entries and rank by relevance to security queries80-95%HighGeneral log analysis
Keyword matchingFilter entries containing security-relevant terms70-90%MediumKnown threat patterns
Severity filteringSelect entries above severity threshold60-80%HighAlert prioritization
Entity extractionKeep entries mentioning critical assets75-90%HighAsset-focused investigation
Temporal windowingSelect entries within incident timeframeVariableHighTimeline reconstruction

Extraction workflow:

  1. Embed log entries — Convert each entry to vector representation using security-tuned embedding models
  2. Define security queries — Create reference embeddings for threat categories (authentication failures, malware, exfiltration, privilege escalation)
  3. Calculate relevance scores — Compute similarity between entries and security queries
  4. Rank and select — Return top-k entries by security relevance score
  5. 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 ComponentPreservation PriorityCompression ApproachQuality Check
IP addressesCritical (100%)Extract verbatimRegex validation
Domain namesCritical (100%)Extract verbatimDNS format check
File hashesCritical (100%)Extract verbatimHash format validation
UsernamesCritical (100%)Extract verbatimEntity recognition
TimestampsHigh (100%)Preserve or convert to relativeSequence validation
Severity levelsHigh (100%)Map to standard scaleClassification check
MITRE ATT&CK IDsHigh (95%+)Extract technique referencesID format validation
Event descriptionsMedium (80%+)Abstractive summarizationSemantic similarity

Abstractive summary structure:

  1. Timeline overview — Chronological sequence of key events with timestamps
  2. Key entities — Systems, users, and network assets involved
  3. Attack indicators — IOCs extracted and categorized by type
  4. Pattern analysis — Correlations and relationships between events
  5. 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:

  1. L0 → L1 (Filtering) — Remove non-security fields, keeping timestamp, IPs, user, action, result, severity, message per NIST SP 800-92 guidelines
  2. L1 → L2 (Clustering) — Group similar events by signature, source, or time window; generate cluster summaries
  3. L2 → L3 (Distillation) — Extract key findings, IOCs, and attack patterns; discard supporting detail
  4. 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 TypePriorityDescriptionSecurity Rationale
Section headersHighestMajor document divisionsPreserve report structure
Horizontal rulesHighIncident boundariesKeep incidents separate
Paragraph breaksMediumLogical groupingsMaintain context coherence
Alert markersMediumAlert boundariesPreserve alert integrity
Event markersMediumEvent boundariesKeep events atomic
Sentence boundariesLowestLast resort splittingAvoid 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 TypeCompression RatioCritical Fields to PreserveRecommended Approach
Syslog (RFC 5424)60-70%Priority, timestamp, hostname, messageField filtering + deduplication
Windows Event Logs50-60%Event ID, timestamp, user, outcomeEvent ID clustering
AWS CloudTrail40-50%Event name, user identity, resourcesAPI call aggregation
Zeek/Bro Network Logs70-80%Conn ID, IPs, ports, protocol infoConnection summarization
NGINX/Apache Access Logs80-90%IP, timestamp, request, status, sizeRequest pattern aggregation

Temporal windowing approach:

  1. Define window size — Typically 30-120 seconds depending on log volume
  2. Group entries by window — Collect all entries within each time bucket
  3. Generate window summary — Extract time range, event count, unique sources, severity distribution
  4. Select representative messages — Keep 2-3 most informative entries per window
  5. Preserve anomalies — Always include entries that deviate from patterns

Compression output structure:

Output FieldDescriptionToken Impact
Time rangeStart and end of window~10 tokens
Event countNumber of entries in window~5 tokens
Unique sourcesDeduplicated source listVariable
Severity summaryDistribution of severity levels~20 tokens
Top messagesRepresentative 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 DimensionGrouping LogicCompression BenefitInformation Preserved
Signature-basedSame alert rule/signature70-90% reductionAttack type, severity
Time-bucketed15-minute windows50-70% reductionTemporal patterns
Source-basedSame source IP/host60-80% reductionAttacker behavior
Target-basedSame destination60-80% reductionAsset exposure
Campaign-basedRelated IOCs80-95% reductionAttack 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 TypeRaw Size (typical)Distilled SizeKey Elements to Retain
STIX/TAXII Feeds50-500 KB per report2-10 KBIOCs, TTPs, relationships
MISP Events10-100 KB1-5 KBAttributes, galaxies, correlations
YARA Rules5-50 KB per rule set0.5-2 KBRule names, conditions, meta
Sigma Rules2-10 KB per rule0.3-1 KBDetection logic, references

STIX object distillation priorities:

STIX Object TypeRetention PriorityKey Fields to ExtractCompression Ratio
IndicatorCriticalPattern, valid_from, labels60-70%
Attack-patternHighName, MITRE ID, kill chain phases70-80%
MalwareHighName, types, aliases75-85%
Threat-actorMediumName, aliases, motivations80-90%
CampaignMediumName, objectives, first/last seen80-90%
RelationshipLowSource, target, relationship type85-95%

Distillation workflow:

  1. Parse STIX bundle — Extract all objects from bundle
  2. Filter by type — Prioritize indicators, attack patterns, and malware
  3. Extract key fields — Keep only essential attributes per object type
  4. Preserve relationships — Maintain links between related objects
  5. 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 TacticTypical Event VolumeCompression ApproachKey Outputs
ReconnaissanceLowPreserve allExternal scanning sources
Initial AccessLow-MediumPreserve allEntry vectors, exploits
ExecutionMediumSummarize by processCommand patterns
PersistenceLowPreserve allPersistence mechanisms
Privilege EscalationLowPreserve allEscalation techniques
Defense EvasionMedium-HighCluster by techniqueEvasion methods
Credential AccessLow-MediumPreserve allCompromised accounts
DiscoveryHighSummarize by targetEnumeration scope
Lateral MovementMediumPreserve allMovement paths
CollectionMediumSummarize by data typeData targets
ExfiltrationLowPreserve allExfil channels, volumes
ImpactLowPreserve allDamage 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 ToolProviderAccuracyUse Case
tiktokenOpenAIExact for GPT modelsProduction token counting
Anthropic APIAnthropicExact for ClaudeClaude-specific applications
Character estimationN/A~4 chars/tokenQuick estimates
Hugging Face tokenizersVariousModel-specificOpen source models

Budget fitting algorithm:

  1. Count tokens per section — Calculate exact token count for each content section
  2. Sort by priority — Order sections by importance (IOCs first, context second)
  3. Greedy selection — Add sections until budget exhausted
  4. Truncation fallback — If final section exceeds remaining budget, truncate intelligently
  5. 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 StrategyTTLUse CaseCache Key
Content hash1-24 hoursStatic logsSHA-256 of raw content
Time-windowed5-15 minutesStreaming logsTime bucket + source
Query-specific30-60 minutesInvestigation contextQuery hash + data hash
Session-scopedSession durationInteractive analysisSession 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 LevelToken BudgetContent IncludedUse Case
Summary~100 tokensTitle, severity, key findingInitial triage
Overview~500 tokensSummary + affected assets, timelineAlert review
Detailed~2000 tokensOverview + IOCs, techniques, evidenceInvestigation
Full~10000 tokensComplete incident dataDeep forensics

Progressive disclosure workflow:

  1. Start with summary — Provide minimal context for initial assessment
  2. Expand on request — Increase detail level when AI needs more information
  3. Section-specific expansion — Allow drilling into specific sections (timeline, IOCs, etc.)
  4. 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:

  1. Compute semantic similarity — Use embedding models like Sentence Transformers to calculate cosine similarity between original and compressed content
  2. Extract and compare IOCs — Use regex patterns to identify all indicators (IPs, domains, hashes, emails) in both versions
  3. Verify timestamp retention — Compare extracted timestamps to ensure temporal integrity
  4. Calculate compression ratio — Measure character/token reduction achieved
  5. 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 TypeOriginal TokensCompressed TokensCompression RatioQuality Score
Firewall logs (1 hour)50,0005,00090%0.92
SIEM alerts (100 alerts)25,0003,50086%0.94
Incident report15,0002,00087%0.91
Threat intel feed (daily)100,0008,00092%0.89
Vulnerability scan80,0006,00092%0.93

Tools and Libraries

ToolPurposeIntegration
LangChainText splitting and document transformationPython SDK
LlamaIndexNode parsing and hierarchical indexingPython SDK
tiktokenToken counting for OpenAI modelsPython library
Anthropic TokenizerToken counting for Claude modelsAPI
spaCyNamed entity recognition for IOC extractionPython NLP
Sentence TransformersSemantic similarity and embeddingPython ML

References