Skip to main content

Module context_analysis

Module context_analysis 

Source
Expand description

Context analysis — per-tool token breakdown and duplicate detection. Context analysis — per-tool token breakdown and duplicate detection.

Analyzes conversation history to identify where tokens are being spent. Used by compaction decisions, /usage reporting, and microcompact.

§What it reports

  • Per-tool token counts — how many tokens each tool’s results consume (Read, Grep, Bash, etc.)
  • Duplicate file reads — files read multiple times (wasted context)
  • Human vs assistant split — how much of the context is user messages vs model responses
  • Attachment sizes — images and files pasted into the conversation

§How it’s used

  • Auto-compact: triggers compaction when total tokens exceed threshold
  • /usage command: shows the token breakdown to the user
  • Microcompact: identifies old tool results safe to clear

Inspired by Claude Code’s contextAnalysis.ts.

Structs§

ContextAnalysis
Per-tool and per-role token breakdown of a conversation context.
DuplicateRead
Info about a duplicated file read in context.

Functions§

analyze_context
Analyze conversation history and produce a token breakdown.