◉CLAUDE LABJP
●2.1.301 — Adds the vim % motion, LaTeX math rendering in the VS Code panel, and a retry-watchdog limit for usage-limit waits (10/15)●2.1.299 — Conversations over 200K tokens left idle about 54 minutes get compacted; MCP allow rules now match only the server they name●SONNET 4.5 — Retires on the Claude API on 11/30, 45 days left. Migrate to Sonnet 5.5●WINDOWS — Reports say hooks break silently only on Japanese-locale Windows; check the character encoding first●FAST MODE — Opus 5.5 fast mode is out of research preview and no longer needs the beta header (10/15)●NEW — I Put a Ten-Line Promise File at the To…●2.1.301 — Adds the vim % motion, LaTeX math rendering in the VS Code panel, and a retry-watchdog limit for usage-limit waits (10/15)●2.1.299 — Conversations over 200K tokens left idle about 54 minutes get compacted; MCP allow rules now match only the server they name●SONNET 4.5 — Retires on the Claude API on 11/30, 45 days left. Migrate to Sonnet 5.5●WINDOWS — Reports say hooks break silently only on Japanese-locale Windows; check the character encoding first●FAST MODE — Opus 5.5 fast mode is out of research preview and no longer needs the beta header (10/15)●NEW — I Put a Ten-Line Promise File at the To…
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-api83context-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-05-26
Stabilizing Claude API Structured Responses in Production — Notes on tool_use, JSON Schema, and Layered Validation
Getting Claude to return JSON takes a few lines. Keeping that JSON usable in production is a different problem. Here is the layered design I landed on after running a wallpaper classification pipeline through Claude API, built around tool_use, JSON Schema, and domain validation.
⬡ API & SDK2026-05-22
Why tool_result could not be submitted Keeps Coming Back, and How to Build a Recovery Handler That Actually Holds
Run a Claude agent long enough and one day it starts: 'tool_result could not be submitted', back to back, and retries change nothing. The error message hides four completely different root causes. Here is what I learned debugging it on the always-on agent jobs I run as an indie developer, with the TypeScript recovery handler I now ship in production.
⬡ API & SDK2026-05-19
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.