flatten_messages
Replace large tool_result blocks in Messages API arrays with compact markers to reduce token count, and return extracted data for reversible restoration.
Instructions
Flatten a raw Anthropic Messages API messages[] array in memory: every bulky tool_result block (large text or base64 image) larger than min_size bytes is swapped for a compact [FLATTENED id=...] marker, and the originals are returned in "extracted". Persist "extracted" yourself — you are the store — and feed it back to unflatten_messages to restore the conversation byte-for-byte. Purely functional: no session file, no disk, no network; the input is never mutated. This is the same engine as the flatten-mcp library export; for production use inside your own process, prefer importing the library so the conversation does not travel over a transport.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| messages | Yes | The raw Messages API messages[] array ({ role, content } objects, verbatim). | |
| min_size | No | Only flatten tool_result blocks larger than N serialized bytes. |