CLAUDE LABJP
FORK — Claude Code 2.1.212 changes what /fork does: it copies your conversation into a new background session with its own row in claude agents, so you can keep working. The old in-session subagent is now /subtaskLIMITS — WebSearch calls are now capped at 200 per session by default, and subagent spawns get the same 200 ceiling, so a runaway search or delegation loop stops on its ownMCPBG — MCP tool calls running past two minutes now move to the background automatically, keeping the session usable. Tune the threshold with CLAUDE_CODE_MCP_AUTO_BACKGROUND_MSPLANFIX — Fixed plan mode auto-running file-modifying Bash commands such as touch and rm without a permission prompt or an SDK canUseTool callbackSONNET5 — Claude Sonnet 5 is running on introductory pricing of $2 per million input tokens and $10 per million output. After August 31 it moves to $3 and $15IPO — Bankers are reportedly lining up investor meetings for Anthropic ahead of a possible public listing as soon as OctoberFORK — Claude Code 2.1.212 changes what /fork does: it copies your conversation into a new background session with its own row in claude agents, so you can keep working. The old in-session subagent is now /subtaskLIMITS — WebSearch calls are now capped at 200 per session by default, and subagent spawns get the same 200 ceiling, so a runaway search or delegation loop stops on its ownMCPBG — MCP tool calls running past two minutes now move to the background automatically, keeping the session usable. Tune the threshold with CLAUDE_CODE_MCP_AUTO_BACKGROUND_MSPLANFIX — Fixed plan mode auto-running file-modifying Bash commands such as touch and rm without a permission prompt or an SDK canUseTool callbackSONNET5 — Claude Sonnet 5 is running on introductory pricing of $2 per million input tokens and $10 per million output. After August 31 it moves to $3 and $15IPO — Bankers are reportedly lining up investor meetings for Anthropic ahead of a possible public listing as soon as October
Articles/API & SDK
API & SDK/2026-05-19Advanced

Hierarchical Chat History Summarization with Claude API: A 3-Tier Design That Cut Tokens by 70%

Working TypeScript design for compressing long in-app chat histories into three tiers — recent turns kept verbatim, mid-range episodes summarized with Haiku 4.5, and long-range memory distilled to JSON by Sonnet 4.6. Includes seven weeks of production data showing input tokens down 70% and monthly API cost down from $480 to $145.

claude-api81chat-historysummarization2token-optimizationproduction111

Premium Article

In mid-April 2026, the in-app AI chat I run inside one of my wallpaper apps started silently doubling its per-session API cost. I did not notice it from the monthly graph. I noticed it from a review that said "the assistant feels slower these days," which finally pushed me to open the dashboard. The cause was simple: heavy users had long histories, and every single turn was sending the whole transcript back to the model. Sessions with 40 turns easily passed 18,000 input_tokens.

I have been a solo iOS and Android developer since 2014, with around 50 million cumulative downloads across an AdMob-driven app catalog of wallpapers and relaxation apps. I have lived through plenty of server-side cost incidents over those years. The Claude API failure mode is different though: nothing is broken, users are not complaining about quality, but input_tokens climbs in a straight line because history is linear. I spent a few days rewriting the design around hierarchical compression rather than trimming, and over seven weeks of running totals the average input tokens dropped by 70%.

This article is the implementation note for that hierarchical summarizer. It splits the history into three tiers — Tier 1 keeps the recent turns verbatim, Tier 2 compresses mid-range episodes with Haiku 4.5, and Tier 3 distills long-range memory into a JSON object using Sonnet 4.6. Trimming alone could not reach the compression ratio and the contextual fidelity we needed. The code is adapted from the production version that runs on Cloudflare Workers in TypeScript.

What "Bloated History" Really Looks Like in an Indie App

Every chat-style integration that appends turns to the messages array eventually hits this wall. Short sessions are fine. The top 5% of my long-tail users were burning 38% of the monthly API budget, and because that app monetizes via AdMob rather than subscriptions, every long session was a net cost. That structural mismatch made it impossible to ignore.

Why Naive Trimming Broke User Trust

The first thing I tried was a fixed cap: keep only the last 10 turns. That works on cost, but it throws away context the user expects me to remember. Color preferences from earlier in the session, themes the user rejected, the project they are slowly building — these still need to be referenced 20 turns later. Cheap trimming creates the "the assistant keeps forgetting" feeling. I got that exact complaint multiple times in App Store reviews.

"Compressed Memory" Instead of "Forgetting"

Hierarchical summarization separates the recent flow from the long-range context. Keep recent turns raw, replace older ones with summaries, and reduce the oldest into an abstracted entity set. If trimming is forgetting, then hierarchical summarization is "compressed memory." This is the design philosophy I keep coming back to as an indie developer running multiple apps where I cannot afford either expensive sessions or shallow assistants.

The Three-Tier Architecture at a Glance

The production design partitions history into three tiers.

TierContentCompressionTarget tokens
Tier 1Last 6 turnsVerbatim~2,500
Tier 2Mid-range episodes (summarized)Haiku 4.5, ~200 tokens each800–1,200 total
Tier 3Long-range entities and intentsJSON via Sonnet 4.6~600

Why Tier 1 Is Verbatim

Tier 1 preserves conversational continuity. Six turns is the sweet spot for my wallpaper app because users who say "redo your last suggestion" or "go back to the idea three turns ago" almost always refer to something inside that window. Fewer than six and the conversation breaks; more and token savings hit diminishing returns.

Why Haiku 4.5 Handles Tier 2

Tier 2 is mid-range memory. Once history grows past the Tier 1 budget, the overflow is sliced into six-turn episodes and Haiku 4.5 condenses each to roughly 200 tokens. Haiku is the right model for summarization-style inference: in my own scoring, Haiku-generated summaries hit about 88% of the subjective quality of Sonnet summaries while costing roughly one sixth per call.

Why Sonnet 4.6 Handles Tier 3

Tier 3 is long-range memory. After three episodes accumulate, I run them through Sonnet 4.6 to distill stable preferences, rejected ideas, the user's ongoing intent, and named anchors into a small JSON object. Distillation is harder than summarization, and Haiku occasionally drops important entities here, so the extra spend on Sonnet is worth it.

Thank you for reading this far.

Continue Reading

What follows includes implementation code, benchmarks, and practical content we hope you'll find useful. This site runs without ads — server and development costs are supported entirely by members like you. If it's been helpful, we'd be truly grateful for your support.

WHAT YOU'LL LEARN
Full TypeScript implementation of a Tier 1–3 hierarchical summarizer with concrete threshold numbers
Cost breakdown of using Haiku 4.5 for episodic summaries and Sonnet 4.6 for distillation, with seven weeks of running totals
An async-first compression schedule on Cloudflare Queues so summarization never pushes user-visible latency up
Secure payment via Stripe · Cancel anytime

Unlock This Article

Get full access to the rest of this article. Buy once, read anytime. This site is ad-free — your support goes directly toward keeping it running.

or
Unlock all articles with Membership →
Share

Thank You for Reading

Claude Lab is ad-free, supported entirely by members like you. We publish practical guides daily with implementation code, benchmarks, and production-ready patterns. If you've found it useful, we'd love to have you on board.

  • Copy-paste ready implementation code
  • New advanced guides published daily
  • $5/mo or $10 for lifetime access
View Membership →

Related Articles

API & SDK2026-07-13
Coalescing Concurrent Claude API Calls: Single-Flight Against Duplicate Inference and Cache Stampede
A design for collapsing identical prompts that fire at the same instant into a single upstream Claude call, using single-flight (request coalescing). In-process and distributed implementations, jittered retries, and negative caching, with measured results.
API & SDK2026-07-03
How Many Concurrent Claude API Requests Can You Actually Hold? Sizing Production Infrastructure with Little's Law and Measured Memory
Concurrency, queue depth, and memory are numbers you can derive, not guess. A working method for sizing Claude API production deployments with Little's Law, a memory probe, and a 30-minute load check — learned the hard way from an OOM crash.
API & SDK2026-06-28
A Silent Drop to a Weaker Model Is Scarier Than an Error: Designing a Capability Floor for Claude API Fallback
When a model becomes unavailable in an unattended pipeline, automatically dropping to a weaker model is dangerous. Drawing on years of running automated indie pipelines, this is how to use per-task capability contracts and a degradation budget to decide where to stop.
📚RECOMMENDED BOOKS
Build a Large Language Model (From Scratch)
Sebastian Raschka
LLM Dev
Prompt Engineering for LLMs
Berryman & Ziegler
Prompting
AI Engineering
Chip Huyen
AI Eng
* Contains affiliate links
See all →