Skip to main content
Glama

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

TableJSON Schema
NameRequiredDescriptionDefault
dry_runNoReport what would be flattened without modifying files
min_sizeNoOnly flatten tool results larger than N bytes
sessionIdNocamelCase alias for session_id (accepted so a camelCase call does not fail validation).
claude_dirNoAbsolute path (or ~/...) to the Claude config dir whose sessions to target — the dir holding projects/. Default: $CLAUDE_CONFIG_DIR if set, else ~/.claude.
session_idNoSession UUID, "last", "last N", or "current". Omit to flatten the current live session.
project_dirNoAbsolute path to project. Default: the project the CLI runs in (cwd)
include_tool_use_resultNoAlso 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.
Behavior4/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations, the description fully discloses key behaviors: atomic rewrite, backup creation, reversibility via unflatten_session, and conditional exact token savings. It also notes that conversation reads identically. Minor omission: does not mention transient disk usage.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness4/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is information-dense but front-loaded with the main action. Every sentence adds value, though it could be slightly more concise without losing clarity.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness4/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the tool's complexity (7 params, no output schema), the description covers purpose, mechanism, safety, return values, and post-operation instructions. The agent can make an informed decision.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters4/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema coverage is 100%, so the description builds on that foundation. It adds meaningful context about how parameters affect the flattening process (e.g., condition for exact counts, session specifiers). The description does not repeat schema details but enriches understanding.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description uses a specific verb 'flatten' and clearly identifies the resource: a Claude Code session. It explains what is moved (bulky tool results) and what remains (compact markers), and distinguishes itself from sibling tools like flatten_messages and unflatten_session.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines4/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides explicit guidance on when to use the tool (to reduce context tokens), mentions it is crash-safe and reversible, and lists valid session_id formats. It lacks explicit when-not-to-use scenarios, but the context is clear.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/shayaShav/flatten-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server