flatten_session
Replace bulky tool results in a Claude Code session JSONL with compact markers to reduce context tokens, while keeping every prompt and event verbatim.
Instructions
Flatten a Claude Code session: move bulky tool results (large text output and base64 image/screenshot blocks) out of the session JSONL into a backup copy, leaving a compact [FLATTENED ...] marker. The conversation reads identically — every prompt and event stays verbatim — but resumes with far fewer context tokens. Crash-safe (atomic rewrite + a single backup holding the complete session) and reversible via unflatten_session. Reports diskBytesSaved and contextTokensSaved out of contextTokensTotal (estimated locally, or exact when FLATTEN_COUNT_EXACT=1 and ANTHROPIC_API_KEY are both set). With no session_id, flattens the current live session; also accepts a UUID, "last", "last N", or "current". After flattening, /resume the session to load the lighter copy.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| dry_run | No | Report what would be flattened without modifying files | |
| min_size | No | Only flatten tool results larger than N bytes | |
| sessionId | No | camelCase alias for session_id (accepted so a camelCase call does not fail validation). | |
| claude_dir | No | Absolute path (or ~/...) to the Claude config dir whose sessions to target — the dir holding projects/. Default: $CLAUDE_CONFIG_DIR if set, else ~/.claude. | |
| session_id | No | Session UUID, "last", "last N", or "current". Omit to flatten the current live session. | |
| project_dir | No | Absolute path to project. Default: the project the CLI runs in (cwd) | |
| include_tool_use_result | No | Also flatten the top-level toolUseResult mirror Claude Code keeps per result line (roughly doubles disk savings; lossless & restorable). Set false to only touch message.content. |