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-22Advanced

Allocating the 200K Context Window in Claude API — Budgeting System, Tools, Memory, and History in Production

Treat Claude API's 200K context as a budget rather than an open shelf. A TypeScript-backed allocation architecture that carves system, tools, memory, history, and headroom into explicit envelopes — built and tuned in a wallpaper app earning real ad revenue.

claude-api81context-window6token-budget3production111architecture10

Premium Article

In March 2026, the Claude-powered chat I had embedded in one of the healing wallpaper apps I run started showing a slow upward drift in input_tokens from one evening on. Conversation quality was fine. Reviews were trending positive. That is exactly why I left it alone for ten days.

When I finally opened the Anthropic Console, the surprise was not in the messages array. It was that system and tools were each carrying roughly twice the tokens I had budgeted for. I had landed the chat-history compression project the previous month, so I assumed the bill was solved. The bill was not solved. The context window was being treated as an open shelf, and a different door was leaking tokens.

Since 2014 I have shipped personal apps to a cumulative 50 million downloads, and I have walked into more than a few server-bill landmines along the way. But Claude API has a particular failure mode: things look healthy because the model still answers well, while a side channel quietly grows. This post is the production write-up of the architecture I now use to carve the 200K window into five explicit envelopes, with both static and dynamic allocation policies. The code is reconstructed from the version I run on Cloudflare Workers with TypeScript.

The 200K Is a Budget, Not a Window

Sonnet 4.6 and Opus 4.6 both ship with 200,000-token windows. On day one that feels like more room than you could possibly need. In production, five distinct demands flow into that same envelope every request.

  • System prompt — persona, guardrails, output format directives
  • Tool definitions — JSON Schema, descriptions, parameter enums
  • Memory tier — retrieved episodes, entities, user preferences
  • Conversation history — the recent messages array
  • Answer headroom — what you reserve for max_tokens on output

My mistake in the wallpaper app was treating those five as a single pile. In reality, tool definitions were silently shipping 18,000 tokens per request, RAG-driven memory was averaging 22,000, history about 9,000, and the system prompt 4,800. The remaining 145,000 was unused. The problem was never "we're not using enough." The problem was that no one had drawn the envelopes, so when something needed to be cut, there was no rule for what to cut.

Draw the Five Envelopes First

The architecture I run today is two-tier: a static budget that is the source of truth, with a dynamic reallocator on top. With Sonnet 4.6 or Opus 4.6, I treat 192K as the per-request budget (200K minus up to 8,000 tokens of answer headroom).

Static budget per category, with a soft cap that may be reached only when other categories shrink:

  • system: base 5,000 / cap 6,000 — persona, guardrails, output format
  • tools: base 15,000 / cap 22,000 — full JSON Schema for every tool
  • memory: base 30,000 / cap 60,000 — RAG, entities, preferences
  • history: base 40,000 / cap 80,000 — last N turns of the conversation
  • reserve: 24,000 fixed — failsafe buffer

The cap is a ceiling other categories may borrow against; reserve is the one envelope I never touch. It exists for the days when an MCP server is added without warning, or when retrieval hits explode, so headroom does not get devoured.

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
A TypeScript BudgetAllocator that partitions the 200K window across five categories with both static and dynamic policies
How I uncovered 18,200 tokens hidden in tool definitions and recovered 23% of monthly input cost
Splitting the memory tier between Sonnet 4.6 and Haiku 4.5 cut monthly API spend from ¥86,000 to ¥38,000 with no quality regression
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-06-15
When a Model Disappears Without Warning: A State Machine for Retirement, Withdrawal, and Overload
A model can become unusable in hours for reasons that have nothing to do with a technical outage. This guide models three distinct flavors of 'unavailable'—retirement, withdrawal, and transient overload—as one availability state machine, with a router that keeps automated pipelines running. Working TypeScript and Python included.
API & SDK2026-06-03
An Anti-Corruption Layer for Claude API Models — Keeping Generation Changes Out of Your Business Logic
Hard-coding model strings into business logic means production breaks quietly every time a generation is retired. Here is an anti-corruption layer that separates logical roles from physical model IDs, with working TypeScript and Python, migration costs, and the judgment calls behind it.
API & SDK2026-05-21
Forecasting Claude API token costs with ±10% accuracy from the first three days
A practical EWMA + seasonality decomposition model that forecasts month-end Claude API costs from only the first three days of token usage, with three-tier automated guardrails for prompt caching, model routing, and rate limiting.
📚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 →