Monitoring AI agent sessions: Leveraging embedding models to aid threat hunting and attack discovery workflows
Embedding models enable deterministic semantic search over AI agent session transcripts, finding risky behaviors that keyword rules and per-session LLM triage miss at a fraction of the cost. The author embedded 7,454 turns from six months of Claude Code and Cursor sessions into Elastic using jina-embeddings-v5-text-small, chunked by four channels (user, reasoning, agent, tool). Semantic vector search with reciprocal rank fusion identified one in seven sessions as review-worthy, with the reasoning channel carrying the highest-fidelity signal. Exemplar retrieval using known bad sessions proved effective for finding repeat workflows, while descriptive hunts found novel behaviors.
Authors: Marcus Jeffes
Source:Elastic Security Labs
Detection / HunterZ.ai
Immediate Actions
- Enable OTel export in Claude Code with OTEL_LOG_USER_PROMPTS, OTEL_LOG_ASSISTANT_RESPONSES, and OTEL_LOG_TOOL_DETAILS set to true
- Deploy Cursor afterAgentThought hooks to capture reasoning channel telemetry
- Add semantic_text field mappings in Elasticsearch to auto-embed agent session events on ingest
Key Takeaways
- Agent reasoning channel outscored user prompt channel for incident detection (MRR 0.339 vs 0.212).
- Vector search beat BM25 keyword search across all four channels on normalized data.
- MITRE ATLAS v2026.07 provided 264 questions across 86 techniques as a baseline query set.
- Embedding cost at scale: $15,000/year for 150M turns vs $117,000+ for generative triage.
- Claude Code OTel export lacks reasoning channel; Cursor afterAgentThought hook captures it.
- Better Binary Quantization reduces vector storage from 2.5TB to 174GB for 600M vectors.
Attack Chain
- Telemetry Collection: Agent session turns captured via OTel export (Claude Code) or hooks (Cursor) into Elastic
- Embedding: Each turn chunked by channel (user, reasoning, agent, tool) and embedded with jina-embeddings-v5-text-small
- Baseline Hunting: MITRE ATLAS-aligned descriptive queries run as vector searches against stored embeddings
- Triage: Sessions scoring above similarity threshold reviewed by analyst or LLM for escalation
- Exemplar Creation: Confirmed bad sessions become exemplar queries for automated ES|QL detection rules
- Rule Automation: New turns compared against exemplar set; correlated with deterministic signals like git push to unfamiliar remotes
Affected Systems
- Claude Code agent sessions
- Cursor IDE agent sessions
- Elasticsearch 9.4+ with Disk BBQ
- Elastic Inference Service
- AWS environments with exposed credentials
Detection Availability
- Published: Other detection logic
- Platforms: Elasticsearch ES|QL
ES|QL vector search queries shown for semantic similarity hunting against stored agent session embeddings in Elastic.
Detection Engineering Assessment
Hunting Hypotheses
| Hypothesis | Telemetry | Stage | FP Risk |
|---|---|---|---|
| Agent sessions where reasoning channel discusses disabling security controls or bypassing certificates indicate defense evasion (T1562). | Agent reasoning channel embeddings and tool call logs | Defense Evasion | Medium — legitimate debugging may discuss certificate issues |
| Agent tool calls reading credential files followed by git push to unfamiliar remotes indicate data exfiltration (T1041). | File access logs, git operation logs, agent tool channel vectors | Exfiltration | Low — combined signal is specific |
| Agent sessions semantically similar to confirmed credential exposure exemplars indicate repeated credential misuse (T1552). | Stored session vectors compared via cosine similarity | Credential Access | Medium — similar workflows may share benign patterns |
| Agent reasoning channel vectors that cluster as outliers from normal session distributions warrant manual review for novel threats. | Vector distance metrics across all session embeddings | Reconnaissance | High — outliers may reflect unusual but benign work |
Required Log Sources
- Claude Code OTel telemetry export
- Cursor agent hooks (afterAgentThought)
- Elasticsearch semantic_text field mappings
- Process execution logs with command line
- Git remote operation logs
- Cloud API call logs (AWS STS)
Control Gaps
- Traditional SIEM rules cannot search unstructured agent session text
- Keyword matching misses paraphrased risky requests
- Per-session LLM triage cannot requery historical data without reprocessing
- Claude Code OTel export omits reasoning channel entirely
- No standard detection content exists for MITRE ATLAS techniques
Recommendations
Infrastructure Hardening
- Enable Disk BBQ in Elasticsearch 9.4+ to reduce vector storage costs by 29x
- Configure field- and document-level security roles to restrict analyst access to session content
- Implement query logging and deterministic prefilters to satisfy DPIA requirements for session monitoring
User Protection
- Block agent-initiated git pushes to personal namespaces via corporate Git proxy policies
- Restrict agent tool access to credential stores using least-privilege IAM policies
- Alert on agent sessions reading .env or ~/.aws/credentials files
MITRE ATT&CK Mapping
- filename.envEnvironment file containing secrets read by agent
- file_path~/.aws/credentialsCredential file read by agent in flagged sessions