Skip to main content
Glama

Flatten Claude Code or Codex session

flatten_session

Flatten a Claude Code or OpenAI Codex CLI session by moving large tool results into a backup, reducing context tokens while preserving conversation integrity. Resume with a lighter copy.

Instructions

Flatten a Claude Code or OpenAI Codex CLI session: move bulky tool results (large text output and, for Claude, 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. Choose the store with agent ("claude" default, or "codex"). Reports diskBytesSaved and contextTokensSaved out of contextTokensTotal. For claude these are estimated locally, or exact when FLATTEN_COUNT_EXACT=1 and ANTHROPIC_API_KEY are both set; for codex the token savings are ALWAYS a local estimate (Anthropic count_tokens does not measure the GPT-5 tokenizer) and contextTokensTotal comes from Codex's own token_count events. With no session_id, flattens the current live session (claude); under codex "current" falls back to "last" (Codex has no live-session linkage). Also accepts a UUID, "last", "last N", or "current"; under codex a UUID reaches any rollout in the tree, while "last"/"last N"/keyword are scoped to the project dir. After flattening, /resume the session (claude) or codex resume the rollout (codex) to load the lighter copy.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
agentNoWhich agent's session store to target: "claude" (default) or "codex".claude
dry_runNoReport what would be flattened without modifying files
min_sizeNoOnly flatten tool results larger than N bytes
breakdownNoInclude the per-tool/resource breakdown of what was flattened. false, true, or "summary" = the aggregated adaptive rows plus byKind/bySlot groupings; "deep" ALSO lists every individual block under its row (tool + arg summary + bytes + per-block token estimate), groups sorted by context tokens then blocks by bytes. Dry runs always include at least the summary; set true/"summary"/"deep" to also get it on a real flatten.
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. Applies to agent "claude" only.
codex_homeNoAbsolute path (or ~/...) to the Codex home whose sessions to target — the dir holding sessions/. Default: $CODEX_HOME if set, else ~/.codex. Applies to agent "codex" only.
session_idNoSession UUID, "last", "last N", or "current". Omit to flatten the current live session (claude); under codex "current" falls back to "last".
project_dirNoAbsolute path to project. Default: the project the CLI runs in (cwd). Under codex, scopes "last"/"last N"/keyword to rollouts whose cwd equals this dir.
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.

Output Schema

TableJSON Schema
NameRequiredDescriptionDefault
sessionsYesOne entry per session flattened (a single entry for one session_id, or several for "last N").
Behavior5/5

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

Discloses crash-safe atomic rewrite, reversible nature, estimation vs exact token savings for each agent, and behavior with 'current' under codex. No contradiction with annotations (readOnlyHint=false, destructiveHint=false). Adds extensive behavioral context beyond annotations.

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?

Front-loaded with core purpose; but the description is long and includes many details that could be condensed. However, given the tool's complexity, the length is justified and well-structured.

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

Completeness5/5

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

Covers all aspects: agent differences, token calculation, session ID handling, directory paths, output fields mentioned. Comprehensive for a 10-parameter tool with output schema.

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 has 100% description coverage, so baseline is 3. The description adds moderate extra context, e.g., explaining dry_run reports without modifying, breakdown options in detail, and behavior of session_id. But the schema descriptions are already good, so only slight improvement.

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?

Description clearly states the tool flattens Claude Code or Codex CLI sessions by moving bulky tool results to a backup copy and leaving compact markers. It distinguishes from siblings like unflatten_session and flatten_messages.

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

Usage Guidelines5/5

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

Provides explicit guidance on when to use (to reduce context tokens), how to choose agent, fallback behaviors for different session_id values, and mentions reversible via unflatten_session. Clearly states alternatives and context.

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