[{"data":1,"prerenderedAt":4199},["ShallowReactive",2],{"navigation":3,"page-\u002Fchapters\u002Fcontext-window-as-resource":102,"surround-\u002Fchapters\u002Fcontext-window-as-resource":4194},[4,8],{"title":5,"path":6,"stem":7},"Home","\u002F","index",{"title":9,"path":10,"stem":11,"children":12,"page":101},"Chapters","\u002Fchapters","2.chapters",[13,17,21,25,29,33,37,41,45,49,53,57,61,65,69,73,77,81,85,89,93,97],{"title":14,"path":15,"stem":16},"Chapter 1. What an Agent Actually Is","\u002Fchapters\u002Fwhat-an-agent-actually-is","2.chapters\u002F01.what-an-agent-actually-is",{"title":18,"path":19,"stem":20},"Chapter 2. The Minimum Viable Loop","\u002Fchapters\u002Fminimum-viable-loop","2.chapters\u002F02.minimum-viable-loop",{"title":22,"path":23,"stem":24},"Chapter 3. Messages, Turns, and the Transcript","\u002Fchapters\u002Fmessages-turns-transcript","2.chapters\u002F03.messages-turns-transcript",{"title":26,"path":27,"stem":28},"Chapter 4. The Tool Protocol","\u002Fchapters\u002Ftool-protocol","2.chapters\u002F04.tool-protocol",{"title":30,"path":31,"stem":32},"Chapter 5. Streaming, Interruption, and Error Handling","\u002Fchapters\u002Fstreaming-interruption-errors","2.chapters\u002F05.streaming-interruption-errors",{"title":34,"path":35,"stem":36},"Chapter 6. Safe Tool Execution","\u002Fchapters\u002Fsafe-tool-execution","2.chapters\u002F06.safe-tool-execution",{"title":38,"path":39,"stem":40},"Chapter 7. The Context Window Is a Resource","\u002Fchapters\u002Fcontext-window-as-resource","2.chapters\u002F07.context-window-as-resource",{"title":42,"path":43,"stem":44},"Chapter 8. Compaction","\u002Fchapters\u002Fcompaction","2.chapters\u002F08.compaction",{"title":46,"path":47,"stem":48},"Chapter 9. External State: The Scratchpad","\u002Fchapters\u002Fscratchpad","2.chapters\u002F09.scratchpad",{"title":50,"path":51,"stem":52},"Chapter 10. Retrieval","\u002Fchapters\u002Fretrieval","2.chapters\u002F10.retrieval",{"title":54,"path":55,"stem":56},"Chapter 11. Designing Tools Models Can Actually Use","\u002Fchapters\u002Fdesigning-tools","2.chapters\u002F11.designing-tools",{"title":58,"path":59,"stem":60},"Chapter 12. The Tool Cliff and Dynamic Loading","\u002Fchapters\u002Ftool-cliff","2.chapters\u002F12.tool-cliff",{"title":62,"path":63,"stem":64},"Chapter 13. MCP: Tools From the Outside World","\u002Fchapters\u002Fmcp","2.chapters\u002F13.mcp",{"title":66,"path":67,"stem":68},"Chapter 14. Sandboxing and Permissions","\u002Fchapters\u002Fsandboxing-permissions","2.chapters\u002F14.sandboxing-permissions",{"title":70,"path":71,"stem":72},"Chapter 15. Sub-agents","\u002Fchapters\u002Fsub-agents","2.chapters\u002F15.sub-agents",{"title":74,"path":75,"stem":76},"Chapter 16. Structured Plans and Verified Completion","\u002Fchapters\u002Fplans-verified-completion","2.chapters\u002F16.plans-verified-completion",{"title":78,"path":79,"stem":80},"Chapter 17. Parallelism and Shared State","\u002Fchapters\u002Fparallelism-shared-state","2.chapters\u002F17.parallelism-shared-state",{"title":82,"path":83,"stem":84},"Chapter 18. Observability","\u002Fchapters\u002Fobservability","2.chapters\u002F18.observability",{"title":86,"path":87,"stem":88},"Chapter 19. Evals","\u002Fchapters\u002Fevals","2.chapters\u002F19.evals",{"title":90,"path":91,"stem":92},"Chapter 20. Cost Control","\u002Fchapters\u002Fcost-control","2.chapters\u002F20.cost-control",{"title":94,"path":95,"stem":96},"Chapter 21. Resumability and Durable State","\u002Fchapters\u002Fresumability","2.chapters\u002F21.resumability",{"title":98,"path":99,"stem":100},"Chapter 22. What Transfers, Where to Go","\u002Fchapters\u002Fwhat-transfers","2.chapters\u002F22.what-transfers",false,{"id":103,"title":38,"body":104,"description":117,"extension":4189,"meta":4190,"navigation":4191,"path":39,"seo":4192,"stem":40,"__hash__":4193},"content\u002F2.chapters\u002F07.context-window-as-resource.md",{"type":105,"value":106,"toc":4177},"minimark",[107,111,118,121,124,136,139,255,258,263,266,269,272,283,291,293,297,300,306,312,318,324,334,351,354,382,385,387,391,394,408,431,453,459,490,492,496,2282,2285,2287,2291,2294,2305,3209,3212,3245,3251,3259,3261,3265,3268,4025,4032,4034,4038,4041,4051,4057,4063,4065,4069,4072,4079,4081,4085,4130,4134,4162,4164,4173],[108,109,38],"h1",{"id":110},"chapter-7-the-context-window-is-a-resource",[112,113,114],"p",{},[115,116,117],"em",{},"Previously: the registry validates arguments and detects loops. All five breaks from Chapter 2 are handled — except Break 5, which is now the subject of the next five chapters. A tool that returns 200KB of JSON still poisons the loop on turn four.",[112,119,120],{},"The context window is the single most misunderstood resource in agent engineering. People treat it like disk space: fixed size, linear consumption, obvious when it's full. Three of those intuitions are wrong.",[112,122,123],{},"The size is not fixed in a useful sense — every provider quotes a headline number (200K, 1M) but model performance degrades continuously as you approach it. Consumption is not linear — tool results, retrieved documents, and prior turns have very different ratios of tokens-to-value. And it's not obvious when it's full — models don't gracefully degrade; they fail silently, get lost in the middle, invent facts to fill gaps they can't find evidence for.",[112,125,126,127,130,131,135],{},"This chapter turns the context window into something your harness can ",[115,128,129],{},"see",". We build a ",[132,133,134],"code",{},"ContextAccountant"," that tracks what's in the window, broken down by component, and exposes utilization thresholds that the next three chapters will use to drive compaction, scratchpad offloading, and retrieval.",[112,137,138],{},"We don't act on the accounting yet — that's Chapter 8. This chapter is strictly about measurement, because you can't decide how to react to context pressure until you can see it.",[140,141,145,183,204,247],"figure",{"className":142},[143,144],"not-prose","my-8",[146,147,156,164,168,173,177],"div",{"className":148},[149,150,151,152,153,154,155],"flex","w-full","h-10","rounded-lg","overflow-hidden","border","border-default",[146,157],{"className":158,"style":163},[149,159,160,161,162],"items-center","justify-center","text-xs","text-default","width:0.3%;background:color-mix(in oklab, currentColor 30%, transparent);",[146,165],{"className":166,"style":167},[149,159,160,161,162],"width:1%;background:color-mix(in oklab, currentColor 22%, transparent);",[146,169,172],{"className":170,"style":171},[149,159,160,161,162],"width:6%;background:color-mix(in oklab, currentColor 18%, transparent);","history",[146,174],{"className":175,"style":176},[149,159,160,161,162],"width:2%;background:color-mix(in oklab, currentColor 14%, transparent);",[146,178,182],{"className":179,"style":181},[149,159,160,161,180],"text-muted","width:90.7%;background:color-mix(in oklab, currentColor 6%, transparent);","headroom",[146,184,188,192,195,198,201],{"className":185},[149,186,161,180,187],"justify-between","mt-2",[189,190,191],"span",{},"system ~500",[189,193,194],{},"schemas ~2K",[189,196,197],{},"history ~12K",[189,199,200],{},"retrieved ~4K",[189,202,203],{},"headroom ~180K",[146,205,209,225,236],{"className":206},[149,159,160,207,208,161],"gap-4","mt-4",[146,210,213,221],{"className":211},[149,159,212],"gap-1",[189,214],{"className":215},[216,217,218,219,220],"inline-block","w-3","h-3","rounded-full","bg-green-500",[189,222,224],{"className":223},[180],"\u003C 60% green",[146,226,228,232],{"className":227},[149,159,212],[189,229],{"className":230},[216,217,218,219,231],"bg-yellow-500",[189,233,235],{"className":234},[180],"60–80% yellow",[146,237,239,243],{"className":238},[149,159,212],[189,240],{"className":241},[216,217,218,219,242],"bg-red-500",[189,244,246],{"className":245},[180],"> 80% red",[248,249,254],"figcaption",{"className":250},[161,180,251,252,253],"mt-3","text-center","italic","Context as a layered budget, not a bag of tokens. Utilization thresholds drive every compaction decision in Chapters 8 through 11.",[256,257],"hr",{},[259,260,262],"h2",{"id":261},"_71-what-the-research-actually-says","7.1 What the Research Actually Says",[112,264,265],{},"Three findings anchor this chapter, and together they make the case that context deserves its own accounting layer rather than being treated as a generous pile you don't need to watch.",[112,267,268],{},"Chroma's 2025 \"Context Rot\" study tested 18 SOTA models on synthetic retrieval tasks — needle-in-a-haystack scenarios where the \"needle\" is a specific fact the model must find and use. Performance degraded continuously with input length, even when the input was 10% of the model's quoted context window. The degradation was model-specific but universal: no tested model was immune. Two separate mechanisms were at play: dilution of attention across more tokens, and interference from semantically-similar-but-irrelevant distractor content.",[112,270,271],{},"Liu et al.'s 2023 \"Lost in the Middle: How Language Models Use Long Contexts\" showed that retrieval accuracy follows a U-curve: content at the beginning and end of the context is retrieved at high accuracy, content in the middle significantly less so. This is not a bug in any single model — it's an artifact of how attention is trained — and it has stayed consistent across GPT, Claude, and open-source models through multiple generations.",[112,273,274,275,278,279,282],{},"Hsieh et al.'s 2024 \"RULER: What's the Real Context Size of Your Long-Context Language Models?\" formalized the gap between a model's ",[115,276,277],{},"claimed"," context window and its ",[115,280,281],{},"effective"," one. RULER tested models across thirteen task types at escalating context lengths — needle-in-a-haystack at varying depths, multi-hop tracing, aggregation, frequent-words extraction — and found that every model's effective length (the length at which it still performs comparably to short-context baselines) was substantially shorter than its nominal window, often by 4–8×. A model advertised at 128K might be meaningfully reliable only up to 32K; a 1M-token model might rot noticeably past 128K. The RULER numbers are the empirical backbone of the rule of thumb this chapter leans on.",[112,284,285,286,290],{},"Together these three findings imply a practical rule: ",[287,288,289],"strong",{},"a 200K context window is not a 200K budget",". The effective budget — the amount you can fill before quality degrades — is typically 50–70% of the headline number for retrieval-heavy work, and the placement within that budget matters. Chapter 10 handles placement (put critical facts at the ends of the window). This chapter handles budgeting.",[256,292],{},[259,294,296],{"id":295},"_72-what-to-count","7.2 What to Count",[112,298,299],{},"A context window is not a pile of tokens; it's a layered composition. Most production harnesses track at least five components:",[112,301,302,305],{},[287,303,304],{},"System prompt."," The instructions, persona, tool-use guidelines, and safety policies that run before any user input. Typically 500–3000 tokens, stable across a session.",[112,307,308,311],{},[287,309,310],{},"Tool schemas."," Every tool's schema — name, description, input schema — rendered into the prompt once, per provider convention. Our four tools cost perhaps 400 tokens. A 50-tool harness might spend 5000+ tokens here, which — remember the tool cliff — is not just a cost concern but a quality one.",[112,313,314,317],{},[287,315,316],{},"Conversation history."," The user messages, assistant messages, and tool results accumulated across the session. Grows monotonically in the naive loop.",[112,319,320,323],{},[287,321,322],{},"Retrieved context."," Any documents, search results, or scratchpad contents pulled in for the current turn. In Chapter 10 we'll make this dynamic; for now we count whatever's there.",[112,325,326,329,330,333],{},[287,327,328],{},"Headroom."," The room we need to leave for the model's own response. Anthropic's ",[132,331,332],{},"max_tokens"," parameter, OpenAI's equivalent. A minimum we subtract from the total.",[112,335,336,339,340,343,344,347,348,350],{},[287,337,338],{},"Reasoning tokens, if preserved."," When a ",[132,341,342],{},"ReasoningBlock"," (Chapter 3) survives in the transcript — which happens with Anthropic's extended thinking + tools combo, or when a consumer chose to preserve reasoning for auditability — those tokens count against history like any other block. The accountant's ",[132,345,346],{},"_count_block"," handles ",[132,349,342],{}," alongside the others, using the text body as its weight. If you turn extended thinking on, expect history to grow noticeably faster per turn; reasoning can easily be 5–10× the size of the final answer on hard tasks.",[112,352,353],{},"Total = sum of the above. Utilization = total \u002F context window size. The critical thresholds, by rule of thumb:",[355,356,357,364,370,376],"ul",{},[358,359,360,363],"li",{},[287,361,362],{},"≤ 60%",": green. No action needed.",[358,365,366,369],{},[287,367,368],{},"60–80%",": yellow. Consider pruning, summarizing, or offloading soon.",[358,371,372,375],{},[287,373,374],{},"> 80%",": red. Compact now; you're in the rot zone.",[358,377,378,381],{},[287,379,380],{},"> 95%",": emergency. The next turn probably won't fit.",[112,383,384],{},"These numbers are defensible rules of thumb, not laws. You'll tune them for your workload; Chapter 19 gives you the evals that let you tune them empirically.",[256,386],{},[259,388,390],{"id":389},"_73-counting-tokens","7.3 Counting Tokens",[112,392,393],{},"Every provider has its own tokenizer. Counting on one and estimating for another is a recipe for mid-session surprises. Three approaches, each with tradeoffs.",[112,395,396,399,400,403,404,407],{},[287,397,398],{},"Use the provider's official counter."," Anthropic's ",[132,401,402],{},"count_tokens"," endpoint returns exact billing-grade counts; the OpenAI SDK has ",[132,405,406],{},"tiktoken"," for OpenAI models. Accurate, but network round-trips for Anthropic's endpoint make it unsuitable for per-message counting (latency adds up). Use it for calibration, not hot-path accounting.",[112,409,410,413,414,416,417,420,421,424,425,427,428,430],{},[287,411,412],{},"Use a local approximation."," ",[132,415,406],{}," with the appropriate encoding (",[132,418,419],{},"cl100k_base"," for GPT-4\u002F4o, ",[132,422,423],{},"o200k_base"," for GPT-5) gives you byte-exact counts for OpenAI models. For Anthropic and others, the closest local approximation is still ",[132,426,406],{},"'s ",[132,429,419],{},", which is off by maybe 5% on typical English text — usable as a budget proxy, not for billing.",[112,432,433,436,437,440,441,444,445,448,449,452],{},[287,434,435],{},"Rely on the provider's response."," Every ",[132,438,439],{},"ProviderResponse"," from Chapter 3 carries ",[132,442,443],{},"input_tokens"," and ",[132,446,447],{},"output_tokens",". This is ground-truth for the last turn but tells you nothing about what the ",[115,450,451],{},"next"," turn will cost, since you don't yet know what the model will produce.",[112,454,455,456,458],{},"Our accountant uses a combination: local ",[132,457,406],{}," for estimates before a call, provider-reported counts after a call for reconciliation.",[460,461,466],"pre",{"className":462,"code":463,"language":464,"meta":465,"style":465},"language-bash shiki shiki-themes material-theme-lighter github-light github-dark","uv add 'tiktoken>=0.8'\n","bash","",[132,467,468],{"__ignoreMap":465},[189,469,472,476,480,484,487],{"class":470,"line":471},"line",1,[189,473,475],{"class":474},"sbgvK","uv",[189,477,479],{"class":478},"s_sjI"," add",[189,481,483],{"class":482},"sjJ54"," '",[189,485,486],{"class":478},"tiktoken>=0.8",[189,488,489],{"class":482},"'\n",[256,491],{},[259,493,495],{"id":494},"_74-the-accountant","7.4 The Accountant",[460,497,501],{"className":498,"code":499,"language":500,"meta":465,"style":465},"language-python shiki shiki-themes material-theme-lighter github-light github-dark","# src\u002Fharness\u002Fcontext\u002Faccountant.py\nfrom __future__ import annotations\n\nimport json\nfrom dataclasses import dataclass, field\nfrom typing import Literal\n\nimport tiktoken\n\nfrom ..messages import (\n    Block, Message, ReasoningBlock, TextBlock, ToolCall, ToolResult, Transcript,\n)\n\n\nComponent = Literal[\"system\", \"tools\", \"history\", \"retrieved\", \"headroom\"]\n\n\n@dataclass\nclass ContextBudget:\n    window_size: int = 200_000\n    headroom: int = 4096  # reserved for the model's response\n    yellow_threshold: float = 0.60\n    red_threshold: float = 0.80\n\n    @property\n    def usable(self) -> int:\n        return self.window_size - self.headroom\n\n\n@dataclass\nclass ContextSnapshot:\n    totals: dict[Component, int] = field(default_factory=dict)\n    budget: ContextBudget = field(default_factory=ContextBudget)\n\n    @property\n    def total_used(self) -> int:\n        return sum(v for k, v in self.totals.items() if k != \"headroom\")\n\n    @property\n    def utilization(self) -> float:\n        return self.total_used \u002F max(self.budget.usable, 1)\n\n    @property\n    def state(self) -> Literal[\"green\", \"yellow\", \"red\"]:\n        u = self.utilization\n        if u >= self.budget.red_threshold:\n            return \"red\"\n        if u >= self.budget.yellow_threshold:\n            return \"yellow\"\n        return \"green\"\n\n\nclass ContextAccountant:\n    \"\"\"Counts tokens per component across a transcript.\"\"\"\n\n    def __init__(self, encoding_name: str = \"cl100k_base\",\n                 budget: ContextBudget | None = None) -> None:\n        self._enc = tiktoken.get_encoding(encoding_name)\n        self.budget = budget or ContextBudget()\n\n    def snapshot(\n        self,\n        transcript: Transcript,\n        tools: list[dict] | None = None,\n        retrieved: list[str] | None = None,\n    ) -> ContextSnapshot:\n        totals: dict[Component, int] = {\n            \"system\": self._count_text(transcript.system or \"\"),\n            \"tools\": sum(self._count_text(json.dumps(t)) for t in (tools or [])),\n            \"history\": sum(self._count_message(m) for m in transcript.messages),\n            \"retrieved\": sum(self._count_text(r) for r in (retrieved or [])),\n            \"headroom\": self.budget.headroom,\n        }\n        return ContextSnapshot(totals=totals, budget=self.budget)\n\n    def _count_text(self, s: str) -> int:\n        return len(self._enc.encode(s))\n\n    def _count_message(self, m: Message) -> int:\n        # message overhead is ~4 tokens per message in most providers' formats\n        total = 4\n        for block in m.blocks:\n            total += self._count_block(block)\n        return total\n\n    def _count_block(self, block: Block) -> int:\n        match block:\n            case TextBlock(text=t):\n                return self._count_text(t)\n            case ToolCall(name=n, args=a):\n                return self._count_text(n) + self._count_text(json.dumps(a)) + 6\n            case ToolResult(content=c):\n                return self._count_text(c) + 4\n            case ReasoningBlock(text=t):\n                # Only present when an adapter preserves reasoning on the\n                # transcript (Anthropic thinking + tools, or explicit\n                # consumer choice). Weight is the text body; the opaque\n                # signature \u002F encrypted_content in metadata is negligible.\n                return self._count_text(t)\n            case _:\n                # Defensive fallthrough: new block types added later should\n                # be undercounted (not crash the measurement component).\n                return 0\n","python",[132,502,503,509,527,534,543,563,576,581,589,594,610,649,655,660,665,727,732,737,748,761,781,799,815,830,835,844,870,896,901,906,913,923,965,991,996,1003,1023,1083,1088,1095,1115,1154,1159,1166,1214,1229,1254,1267,1289,1300,1311,1316,1321,1331,1345,1350,1383,1412,1440,1462,1467,1478,1485,1497,1525,1552,1564,1589,1626,1684,1730,1774,1797,1803,1833,1838,1867,1896,1901,1930,1936,1947,1967,1989,1997,2002,2032,2042,2062,2080,2109,2154,2173,2194,2211,2217,2223,2229,2235,2252,2262,2268,2274],{"__ignoreMap":465},[189,504,505],{"class":470,"line":471},[189,506,508],{"class":507},"sutJx","# src\u002Fharness\u002Fcontext\u002Faccountant.py\n",[189,510,512,516,520,523],{"class":470,"line":511},2,[189,513,515],{"class":514},"sVHd0","from",[189,517,519],{"class":518},"s_hVV"," __future__",[189,521,522],{"class":514}," import",[189,524,526],{"class":525},"su5hD"," annotations\n",[189,528,530],{"class":470,"line":529},3,[189,531,533],{"emptyLinePlaceholder":532},true,"\n",[189,535,537,540],{"class":470,"line":536},4,[189,538,539],{"class":514},"import",[189,541,542],{"class":525}," json\n",[189,544,546,548,551,553,556,560],{"class":470,"line":545},5,[189,547,515],{"class":514},[189,549,550],{"class":525}," dataclasses ",[189,552,539],{"class":514},[189,554,555],{"class":525}," dataclass",[189,557,559],{"class":558},"sP7_E",",",[189,561,562],{"class":525}," field\n",[189,564,566,568,571,573],{"class":470,"line":565},6,[189,567,515],{"class":514},[189,569,570],{"class":525}," typing ",[189,572,539],{"class":514},[189,574,575],{"class":525}," Literal\n",[189,577,579],{"class":470,"line":578},7,[189,580,533],{"emptyLinePlaceholder":532},[189,582,584,586],{"class":470,"line":583},8,[189,585,539],{"class":514},[189,587,588],{"class":525}," tiktoken\n",[189,590,592],{"class":470,"line":591},9,[189,593,533],{"emptyLinePlaceholder":532},[189,595,597,599,602,605,607],{"class":470,"line":596},10,[189,598,515],{"class":514},[189,600,601],{"class":558}," ..",[189,603,604],{"class":525},"messages ",[189,606,539],{"class":514},[189,608,609],{"class":558}," (\n",[189,611,613,616,618,621,623,626,628,631,633,636,638,641,643,646],{"class":470,"line":612},11,[189,614,615],{"class":525},"    Block",[189,617,559],{"class":558},[189,619,620],{"class":525}," Message",[189,622,559],{"class":558},[189,624,625],{"class":525}," ReasoningBlock",[189,627,559],{"class":558},[189,629,630],{"class":525}," TextBlock",[189,632,559],{"class":558},[189,634,635],{"class":525}," ToolCall",[189,637,559],{"class":558},[189,639,640],{"class":525}," ToolResult",[189,642,559],{"class":558},[189,644,645],{"class":525}," Transcript",[189,647,648],{"class":558},",\n",[189,650,652],{"class":470,"line":651},12,[189,653,654],{"class":558},")\n",[189,656,658],{"class":470,"line":657},13,[189,659,533],{"emptyLinePlaceholder":532},[189,661,663],{"class":470,"line":662},14,[189,664,533],{"emptyLinePlaceholder":532},[189,666,668,671,675,678,681,684,687,689,691,694,697,699,701,703,705,707,709,711,714,716,718,720,722,724],{"class":470,"line":667},15,[189,669,670],{"class":525},"Component ",[189,672,674],{"class":673},"smGrS","=",[189,676,677],{"class":525}," Literal",[189,679,680],{"class":558},"[",[189,682,683],{"class":482},"\"",[189,685,686],{"class":478},"system",[189,688,683],{"class":482},[189,690,559],{"class":558},[189,692,693],{"class":482}," \"",[189,695,696],{"class":478},"tools",[189,698,683],{"class":482},[189,700,559],{"class":558},[189,702,693],{"class":482},[189,704,172],{"class":478},[189,706,683],{"class":482},[189,708,559],{"class":558},[189,710,693],{"class":482},[189,712,713],{"class":478},"retrieved",[189,715,683],{"class":482},[189,717,559],{"class":558},[189,719,693],{"class":482},[189,721,182],{"class":478},[189,723,683],{"class":482},[189,725,726],{"class":558},"]\n",[189,728,730],{"class":470,"line":729},16,[189,731,533],{"emptyLinePlaceholder":532},[189,733,735],{"class":470,"line":734},17,[189,736,533],{"emptyLinePlaceholder":532},[189,738,740,744],{"class":470,"line":739},18,[189,741,743],{"class":742},"stp6e","@",[189,745,747],{"class":746},"sGLFI","dataclass\n",[189,749,751,755,758],{"class":470,"line":750},19,[189,752,754],{"class":753},"sbsja","class",[189,756,757],{"class":474}," ContextBudget",[189,759,760],{"class":558},":\n",[189,762,764,767,770,774,777],{"class":470,"line":763},20,[189,765,766],{"class":525},"    window_size",[189,768,769],{"class":558},":",[189,771,773],{"class":772},"sZMiF"," int",[189,775,776],{"class":673}," =",[189,778,780],{"class":779},"srdBf"," 200_000\n",[189,782,784,787,789,791,793,796],{"class":470,"line":783},21,[189,785,786],{"class":525},"    headroom",[189,788,769],{"class":558},[189,790,773],{"class":772},[189,792,776],{"class":673},[189,794,795],{"class":779}," 4096",[189,797,798],{"class":507},"  # reserved for the model's response\n",[189,800,802,805,807,810,812],{"class":470,"line":801},22,[189,803,804],{"class":525},"    yellow_threshold",[189,806,769],{"class":558},[189,808,809],{"class":772}," float",[189,811,776],{"class":673},[189,813,814],{"class":779}," 0.60\n",[189,816,818,821,823,825,827],{"class":470,"line":817},23,[189,819,820],{"class":525},"    red_threshold",[189,822,769],{"class":558},[189,824,809],{"class":772},[189,826,776],{"class":673},[189,828,829],{"class":779}," 0.80\n",[189,831,833],{"class":470,"line":832},24,[189,834,533],{"emptyLinePlaceholder":532},[189,836,838,841],{"class":470,"line":837},25,[189,839,840],{"class":742},"    @",[189,842,843],{"class":772},"property\n",[189,845,847,850,853,856,860,863,866,868],{"class":470,"line":846},26,[189,848,849],{"class":753},"    def",[189,851,852],{"class":746}," usable",[189,854,855],{"class":558},"(",[189,857,859],{"class":858},"smCYv","self",[189,861,862],{"class":558},")",[189,864,865],{"class":558}," ->",[189,867,773],{"class":772},[189,869,760],{"class":558},[189,871,873,876,879,882,886,889,891,893],{"class":470,"line":872},27,[189,874,875],{"class":514},"        return",[189,877,878],{"class":518}," self",[189,880,881],{"class":558},".",[189,883,885],{"class":884},"skxfh","window_size",[189,887,888],{"class":673}," -",[189,890,878],{"class":518},[189,892,881],{"class":558},[189,894,895],{"class":884},"headroom\n",[189,897,899],{"class":470,"line":898},28,[189,900,533],{"emptyLinePlaceholder":532},[189,902,904],{"class":470,"line":903},29,[189,905,533],{"emptyLinePlaceholder":532},[189,907,909,911],{"class":470,"line":908},30,[189,910,743],{"class":742},[189,912,747],{"class":746},[189,914,916,918,921],{"class":470,"line":915},31,[189,917,754],{"class":753},[189,919,920],{"class":474}," ContextSnapshot",[189,922,760],{"class":558},[189,924,926,929,931,934,936,939,941,943,946,948,952,954,958,960,963],{"class":470,"line":925},32,[189,927,928],{"class":525},"    totals",[189,930,769],{"class":558},[189,932,933],{"class":525}," dict",[189,935,680],{"class":558},[189,937,938],{"class":525},"Component",[189,940,559],{"class":558},[189,942,773],{"class":772},[189,944,945],{"class":558},"]",[189,947,776],{"class":673},[189,949,951],{"class":950},"slqww"," field",[189,953,855],{"class":558},[189,955,957],{"class":956},"s99_P","default_factory",[189,959,674],{"class":673},[189,961,962],{"class":772},"dict",[189,964,654],{"class":558},[189,966,968,971,973,976,978,980,982,984,986,989],{"class":470,"line":967},33,[189,969,970],{"class":525},"    budget",[189,972,769],{"class":558},[189,974,975],{"class":525}," ContextBudget ",[189,977,674],{"class":673},[189,979,951],{"class":950},[189,981,855],{"class":558},[189,983,957],{"class":956},[189,985,674],{"class":673},[189,987,988],{"class":950},"ContextBudget",[189,990,654],{"class":558},[189,992,994],{"class":470,"line":993},34,[189,995,533],{"emptyLinePlaceholder":532},[189,997,999,1001],{"class":470,"line":998},35,[189,1000,840],{"class":742},[189,1002,843],{"class":772},[189,1004,1006,1008,1011,1013,1015,1017,1019,1021],{"class":470,"line":1005},36,[189,1007,849],{"class":753},[189,1009,1010],{"class":746}," total_used",[189,1012,855],{"class":558},[189,1014,859],{"class":858},[189,1016,862],{"class":558},[189,1018,865],{"class":558},[189,1020,773],{"class":772},[189,1022,760],{"class":558},[189,1024,1026,1028,1032,1034,1037,1040,1043,1045,1048,1051,1053,1055,1058,1060,1063,1066,1069,1072,1075,1077,1079,1081],{"class":470,"line":1025},37,[189,1027,875],{"class":514},[189,1029,1031],{"class":1030},"sptTA"," sum",[189,1033,855],{"class":558},[189,1035,1036],{"class":950},"v ",[189,1038,1039],{"class":514},"for",[189,1041,1042],{"class":950}," k",[189,1044,559],{"class":558},[189,1046,1047],{"class":950}," v ",[189,1049,1050],{"class":514},"in",[189,1052,878],{"class":518},[189,1054,881],{"class":558},[189,1056,1057],{"class":884},"totals",[189,1059,881],{"class":558},[189,1061,1062],{"class":950},"items",[189,1064,1065],{"class":558},"()",[189,1067,1068],{"class":514}," if",[189,1070,1071],{"class":950}," k ",[189,1073,1074],{"class":673},"!=",[189,1076,693],{"class":482},[189,1078,182],{"class":478},[189,1080,683],{"class":482},[189,1082,654],{"class":558},[189,1084,1086],{"class":470,"line":1085},38,[189,1087,533],{"emptyLinePlaceholder":532},[189,1089,1091,1093],{"class":470,"line":1090},39,[189,1092,840],{"class":742},[189,1094,843],{"class":772},[189,1096,1098,1100,1103,1105,1107,1109,1111,1113],{"class":470,"line":1097},40,[189,1099,849],{"class":753},[189,1101,1102],{"class":746}," utilization",[189,1104,855],{"class":558},[189,1106,859],{"class":858},[189,1108,862],{"class":558},[189,1110,865],{"class":558},[189,1112,809],{"class":772},[189,1114,760],{"class":558},[189,1116,1118,1120,1122,1124,1127,1130,1133,1135,1137,1139,1142,1144,1147,1149,1152],{"class":470,"line":1117},41,[189,1119,875],{"class":514},[189,1121,878],{"class":518},[189,1123,881],{"class":558},[189,1125,1126],{"class":884},"total_used",[189,1128,1129],{"class":673}," \u002F",[189,1131,1132],{"class":1030}," max",[189,1134,855],{"class":558},[189,1136,859],{"class":518},[189,1138,881],{"class":558},[189,1140,1141],{"class":884},"budget",[189,1143,881],{"class":558},[189,1145,1146],{"class":884},"usable",[189,1148,559],{"class":558},[189,1150,1151],{"class":779}," 1",[189,1153,654],{"class":558},[189,1155,1157],{"class":470,"line":1156},42,[189,1158,533],{"emptyLinePlaceholder":532},[189,1160,1162,1164],{"class":470,"line":1161},43,[189,1163,840],{"class":742},[189,1165,843],{"class":772},[189,1167,1169,1171,1174,1176,1178,1180,1182,1184,1186,1188,1191,1193,1195,1197,1200,1202,1204,1206,1209,1211],{"class":470,"line":1168},44,[189,1170,849],{"class":753},[189,1172,1173],{"class":746}," state",[189,1175,855],{"class":558},[189,1177,859],{"class":858},[189,1179,862],{"class":558},[189,1181,865],{"class":558},[189,1183,677],{"class":525},[189,1185,680],{"class":558},[189,1187,683],{"class":482},[189,1189,1190],{"class":478},"green",[189,1192,683],{"class":482},[189,1194,559],{"class":558},[189,1196,693],{"class":482},[189,1198,1199],{"class":478},"yellow",[189,1201,683],{"class":482},[189,1203,559],{"class":558},[189,1205,693],{"class":482},[189,1207,1208],{"class":478},"red",[189,1210,683],{"class":482},[189,1212,1213],{"class":558},"]:\n",[189,1215,1217,1220,1222,1224,1226],{"class":470,"line":1216},45,[189,1218,1219],{"class":525},"        u ",[189,1221,674],{"class":673},[189,1223,878],{"class":518},[189,1225,881],{"class":558},[189,1227,1228],{"class":884},"utilization\n",[189,1230,1232,1235,1238,1241,1243,1245,1247,1249,1252],{"class":470,"line":1231},46,[189,1233,1234],{"class":514},"        if",[189,1236,1237],{"class":525}," u ",[189,1239,1240],{"class":673},">=",[189,1242,878],{"class":518},[189,1244,881],{"class":558},[189,1246,1141],{"class":884},[189,1248,881],{"class":558},[189,1250,1251],{"class":884},"red_threshold",[189,1253,760],{"class":558},[189,1255,1257,1260,1262,1264],{"class":470,"line":1256},47,[189,1258,1259],{"class":514},"            return",[189,1261,693],{"class":482},[189,1263,1208],{"class":478},[189,1265,1266],{"class":482},"\"\n",[189,1268,1270,1272,1274,1276,1278,1280,1282,1284,1287],{"class":470,"line":1269},48,[189,1271,1234],{"class":514},[189,1273,1237],{"class":525},[189,1275,1240],{"class":673},[189,1277,878],{"class":518},[189,1279,881],{"class":558},[189,1281,1141],{"class":884},[189,1283,881],{"class":558},[189,1285,1286],{"class":884},"yellow_threshold",[189,1288,760],{"class":558},[189,1290,1292,1294,1296,1298],{"class":470,"line":1291},49,[189,1293,1259],{"class":514},[189,1295,693],{"class":482},[189,1297,1199],{"class":478},[189,1299,1266],{"class":482},[189,1301,1303,1305,1307,1309],{"class":470,"line":1302},50,[189,1304,875],{"class":514},[189,1306,693],{"class":482},[189,1308,1190],{"class":478},[189,1310,1266],{"class":482},[189,1312,1314],{"class":470,"line":1313},51,[189,1315,533],{"emptyLinePlaceholder":532},[189,1317,1319],{"class":470,"line":1318},52,[189,1320,533],{"emptyLinePlaceholder":532},[189,1322,1324,1326,1329],{"class":470,"line":1323},53,[189,1325,754],{"class":753},[189,1327,1328],{"class":474}," ContextAccountant",[189,1330,760],{"class":558},[189,1332,1334,1338,1342],{"class":470,"line":1333},54,[189,1335,1337],{"class":1336},"s2W-s","    \"\"\"",[189,1339,1341],{"class":1340},"sithA","Counts tokens per component across a transcript.",[189,1343,1344],{"class":1336},"\"\"\"\n",[189,1346,1348],{"class":470,"line":1347},55,[189,1349,533],{"emptyLinePlaceholder":532},[189,1351,1353,1355,1358,1360,1362,1364,1368,1370,1373,1375,1377,1379,1381],{"class":470,"line":1352},56,[189,1354,849],{"class":753},[189,1356,1357],{"class":1030}," __init__",[189,1359,855],{"class":558},[189,1361,859],{"class":858},[189,1363,559],{"class":558},[189,1365,1367],{"class":1366},"sFwrP"," encoding_name",[189,1369,769],{"class":558},[189,1371,1372],{"class":772}," str",[189,1374,776],{"class":673},[189,1376,693],{"class":482},[189,1378,419],{"class":478},[189,1380,683],{"class":482},[189,1382,648],{"class":558},[189,1384,1386,1389,1391,1393,1396,1400,1402,1404,1406,1408,1410],{"class":470,"line":1385},57,[189,1387,1388],{"class":1366},"                 budget",[189,1390,769],{"class":558},[189,1392,975],{"class":525},[189,1394,1395],{"class":673},"|",[189,1397,1399],{"class":1398},"s39Yj"," None",[189,1401,776],{"class":673},[189,1403,1399],{"class":1398},[189,1405,862],{"class":558},[189,1407,865],{"class":558},[189,1409,1399],{"class":1398},[189,1411,760],{"class":558},[189,1413,1415,1418,1420,1423,1425,1428,1430,1433,1435,1438],{"class":470,"line":1414},58,[189,1416,1417],{"class":518},"        self",[189,1419,881],{"class":558},[189,1421,1422],{"class":884},"_enc",[189,1424,776],{"class":673},[189,1426,1427],{"class":525}," tiktoken",[189,1429,881],{"class":558},[189,1431,1432],{"class":950},"get_encoding",[189,1434,855],{"class":558},[189,1436,1437],{"class":950},"encoding_name",[189,1439,654],{"class":558},[189,1441,1443,1445,1447,1449,1451,1454,1457,1459],{"class":470,"line":1442},59,[189,1444,1417],{"class":518},[189,1446,881],{"class":558},[189,1448,1141],{"class":884},[189,1450,776],{"class":673},[189,1452,1453],{"class":525}," budget ",[189,1455,1456],{"class":673},"or",[189,1458,757],{"class":950},[189,1460,1461],{"class":558},"()\n",[189,1463,1465],{"class":470,"line":1464},60,[189,1466,533],{"emptyLinePlaceholder":532},[189,1468,1470,1472,1475],{"class":470,"line":1469},61,[189,1471,849],{"class":753},[189,1473,1474],{"class":746}," snapshot",[189,1476,1477],{"class":558},"(\n",[189,1479,1481,1483],{"class":470,"line":1480},62,[189,1482,1417],{"class":858},[189,1484,648],{"class":558},[189,1486,1488,1491,1493,1495],{"class":470,"line":1487},63,[189,1489,1490],{"class":1366},"        transcript",[189,1492,769],{"class":558},[189,1494,645],{"class":525},[189,1496,648],{"class":558},[189,1498,1500,1503,1505,1508,1510,1512,1514,1517,1519,1521,1523],{"class":470,"line":1499},64,[189,1501,1502],{"class":1366},"        tools",[189,1504,769],{"class":558},[189,1506,1507],{"class":525}," list",[189,1509,680],{"class":558},[189,1511,962],{"class":772},[189,1513,945],{"class":558},[189,1515,1516],{"class":673}," |",[189,1518,1399],{"class":1398},[189,1520,776],{"class":673},[189,1522,1399],{"class":1398},[189,1524,648],{"class":558},[189,1526,1528,1531,1533,1535,1537,1540,1542,1544,1546,1548,1550],{"class":470,"line":1527},65,[189,1529,1530],{"class":1366},"        retrieved",[189,1532,769],{"class":558},[189,1534,1507],{"class":525},[189,1536,680],{"class":558},[189,1538,1539],{"class":772},"str",[189,1541,945],{"class":558},[189,1543,1516],{"class":673},[189,1545,1399],{"class":1398},[189,1547,776],{"class":673},[189,1549,1399],{"class":1398},[189,1551,648],{"class":558},[189,1553,1555,1558,1560,1562],{"class":470,"line":1554},66,[189,1556,1557],{"class":558},"    )",[189,1559,865],{"class":558},[189,1561,920],{"class":525},[189,1563,760],{"class":558},[189,1565,1567,1570,1572,1574,1576,1578,1580,1582,1584,1586],{"class":470,"line":1566},67,[189,1568,1569],{"class":525},"        totals",[189,1571,769],{"class":558},[189,1573,933],{"class":525},[189,1575,680],{"class":558},[189,1577,938],{"class":525},[189,1579,559],{"class":558},[189,1581,773],{"class":772},[189,1583,945],{"class":558},[189,1585,776],{"class":673},[189,1587,1588],{"class":558}," {\n",[189,1590,1592,1595,1597,1599,1601,1603,1605,1608,1610,1613,1615,1617,1620,1623],{"class":470,"line":1591},68,[189,1593,1594],{"class":482},"            \"",[189,1596,686],{"class":478},[189,1598,683],{"class":482},[189,1600,769],{"class":558},[189,1602,878],{"class":518},[189,1604,881],{"class":558},[189,1606,1607],{"class":950},"_count_text",[189,1609,855],{"class":558},[189,1611,1612],{"class":950},"transcript",[189,1614,881],{"class":558},[189,1616,686],{"class":884},[189,1618,1619],{"class":514}," or",[189,1621,1622],{"class":482}," \"\"",[189,1624,1625],{"class":558},"),\n",[189,1627,1629,1631,1633,1635,1637,1639,1641,1643,1645,1647,1649,1652,1654,1657,1659,1662,1665,1668,1671,1673,1676,1679,1681],{"class":470,"line":1628},69,[189,1630,1594],{"class":482},[189,1632,696],{"class":478},[189,1634,683],{"class":482},[189,1636,769],{"class":558},[189,1638,1031],{"class":1030},[189,1640,855],{"class":558},[189,1642,859],{"class":518},[189,1644,881],{"class":558},[189,1646,1607],{"class":950},[189,1648,855],{"class":558},[189,1650,1651],{"class":950},"json",[189,1653,881],{"class":558},[189,1655,1656],{"class":950},"dumps",[189,1658,855],{"class":558},[189,1660,1661],{"class":950},"t",[189,1663,1664],{"class":558},"))",[189,1666,1667],{"class":514}," for",[189,1669,1670],{"class":950}," t ",[189,1672,1050],{"class":514},[189,1674,1675],{"class":558}," (",[189,1677,1678],{"class":950},"tools ",[189,1680,1456],{"class":673},[189,1682,1683],{"class":558}," [])),\n",[189,1685,1687,1689,1691,1693,1695,1697,1699,1701,1703,1706,1708,1711,1713,1715,1718,1720,1723,1725,1728],{"class":470,"line":1686},70,[189,1688,1594],{"class":482},[189,1690,172],{"class":478},[189,1692,683],{"class":482},[189,1694,769],{"class":558},[189,1696,1031],{"class":1030},[189,1698,855],{"class":558},[189,1700,859],{"class":518},[189,1702,881],{"class":558},[189,1704,1705],{"class":950},"_count_message",[189,1707,855],{"class":558},[189,1709,1710],{"class":950},"m",[189,1712,862],{"class":558},[189,1714,1667],{"class":514},[189,1716,1717],{"class":950}," m ",[189,1719,1050],{"class":514},[189,1721,1722],{"class":950}," transcript",[189,1724,881],{"class":558},[189,1726,1727],{"class":884},"messages",[189,1729,1625],{"class":558},[189,1731,1733,1735,1737,1739,1741,1743,1745,1747,1749,1751,1753,1756,1758,1760,1763,1765,1767,1770,1772],{"class":470,"line":1732},71,[189,1734,1594],{"class":482},[189,1736,713],{"class":478},[189,1738,683],{"class":482},[189,1740,769],{"class":558},[189,1742,1031],{"class":1030},[189,1744,855],{"class":558},[189,1746,859],{"class":518},[189,1748,881],{"class":558},[189,1750,1607],{"class":950},[189,1752,855],{"class":558},[189,1754,1755],{"class":950},"r",[189,1757,862],{"class":558},[189,1759,1667],{"class":514},[189,1761,1762],{"class":950}," r ",[189,1764,1050],{"class":514},[189,1766,1675],{"class":558},[189,1768,1769],{"class":950},"retrieved ",[189,1771,1456],{"class":673},[189,1773,1683],{"class":558},[189,1775,1777,1779,1781,1783,1785,1787,1789,1791,1793,1795],{"class":470,"line":1776},72,[189,1778,1594],{"class":482},[189,1780,182],{"class":478},[189,1782,683],{"class":482},[189,1784,769],{"class":558},[189,1786,878],{"class":518},[189,1788,881],{"class":558},[189,1790,1141],{"class":884},[189,1792,881],{"class":558},[189,1794,182],{"class":884},[189,1796,648],{"class":558},[189,1798,1800],{"class":470,"line":1799},73,[189,1801,1802],{"class":558},"        }\n",[189,1804,1806,1808,1810,1812,1814,1816,1818,1820,1823,1825,1827,1829,1831],{"class":470,"line":1805},74,[189,1807,875],{"class":514},[189,1809,920],{"class":950},[189,1811,855],{"class":558},[189,1813,1057],{"class":956},[189,1815,674],{"class":673},[189,1817,1057],{"class":950},[189,1819,559],{"class":558},[189,1821,1822],{"class":956}," budget",[189,1824,674],{"class":673},[189,1826,859],{"class":518},[189,1828,881],{"class":558},[189,1830,1141],{"class":884},[189,1832,654],{"class":558},[189,1834,1836],{"class":470,"line":1835},75,[189,1837,533],{"emptyLinePlaceholder":532},[189,1839,1841,1843,1846,1848,1850,1852,1855,1857,1859,1861,1863,1865],{"class":470,"line":1840},76,[189,1842,849],{"class":753},[189,1844,1845],{"class":746}," _count_text",[189,1847,855],{"class":558},[189,1849,859],{"class":858},[189,1851,559],{"class":558},[189,1853,1854],{"class":1366}," s",[189,1856,769],{"class":558},[189,1858,1372],{"class":772},[189,1860,862],{"class":558},[189,1862,865],{"class":558},[189,1864,773],{"class":772},[189,1866,760],{"class":558},[189,1868,1870,1872,1875,1877,1879,1881,1883,1885,1888,1890,1893],{"class":470,"line":1869},77,[189,1871,875],{"class":514},[189,1873,1874],{"class":1030}," len",[189,1876,855],{"class":558},[189,1878,859],{"class":518},[189,1880,881],{"class":558},[189,1882,1422],{"class":884},[189,1884,881],{"class":558},[189,1886,1887],{"class":950},"encode",[189,1889,855],{"class":558},[189,1891,1892],{"class":950},"s",[189,1894,1895],{"class":558},"))\n",[189,1897,1899],{"class":470,"line":1898},78,[189,1900,533],{"emptyLinePlaceholder":532},[189,1902,1904,1906,1909,1911,1913,1915,1918,1920,1922,1924,1926,1928],{"class":470,"line":1903},79,[189,1905,849],{"class":753},[189,1907,1908],{"class":746}," _count_message",[189,1910,855],{"class":558},[189,1912,859],{"class":858},[189,1914,559],{"class":558},[189,1916,1917],{"class":1366}," m",[189,1919,769],{"class":558},[189,1921,620],{"class":525},[189,1923,862],{"class":558},[189,1925,865],{"class":558},[189,1927,773],{"class":772},[189,1929,760],{"class":558},[189,1931,1933],{"class":470,"line":1932},80,[189,1934,1935],{"class":507},"        # message overhead is ~4 tokens per message in most providers' formats\n",[189,1937,1939,1942,1944],{"class":470,"line":1938},81,[189,1940,1941],{"class":525},"        total ",[189,1943,674],{"class":673},[189,1945,1946],{"class":779}," 4\n",[189,1948,1950,1953,1956,1958,1960,1962,1965],{"class":470,"line":1949},82,[189,1951,1952],{"class":514},"        for",[189,1954,1955],{"class":525}," block ",[189,1957,1050],{"class":514},[189,1959,1917],{"class":525},[189,1961,881],{"class":558},[189,1963,1964],{"class":884},"blocks",[189,1966,760],{"class":558},[189,1968,1970,1973,1976,1978,1980,1982,1984,1987],{"class":470,"line":1969},83,[189,1971,1972],{"class":525},"            total ",[189,1974,1975],{"class":673},"+=",[189,1977,878],{"class":518},[189,1979,881],{"class":558},[189,1981,346],{"class":950},[189,1983,855],{"class":558},[189,1985,1986],{"class":950},"block",[189,1988,654],{"class":558},[189,1990,1992,1994],{"class":470,"line":1991},84,[189,1993,875],{"class":514},[189,1995,1996],{"class":525}," total\n",[189,1998,2000],{"class":470,"line":1999},85,[189,2001,533],{"emptyLinePlaceholder":532},[189,2003,2005,2007,2010,2012,2014,2016,2019,2021,2024,2026,2028,2030],{"class":470,"line":2004},86,[189,2006,849],{"class":753},[189,2008,2009],{"class":746}," _count_block",[189,2011,855],{"class":558},[189,2013,859],{"class":858},[189,2015,559],{"class":558},[189,2017,2018],{"class":1366}," block",[189,2020,769],{"class":558},[189,2022,2023],{"class":525}," Block",[189,2025,862],{"class":558},[189,2027,865],{"class":558},[189,2029,773],{"class":772},[189,2031,760],{"class":558},[189,2033,2035,2038,2040],{"class":470,"line":2034},87,[189,2036,2037],{"class":514},"        match",[189,2039,2018],{"class":525},[189,2041,760],{"class":558},[189,2043,2045,2048,2050,2052,2055,2057,2059],{"class":470,"line":2044},88,[189,2046,2047],{"class":514},"            case",[189,2049,630],{"class":950},[189,2051,855],{"class":558},[189,2053,2054],{"class":956},"text",[189,2056,674],{"class":673},[189,2058,1661],{"class":950},[189,2060,2061],{"class":558},"):\n",[189,2063,2065,2068,2070,2072,2074,2076,2078],{"class":470,"line":2064},89,[189,2066,2067],{"class":514},"                return",[189,2069,878],{"class":518},[189,2071,881],{"class":558},[189,2073,1607],{"class":950},[189,2075,855],{"class":558},[189,2077,1661],{"class":950},[189,2079,654],{"class":558},[189,2081,2083,2085,2087,2089,2092,2094,2097,2099,2102,2104,2107],{"class":470,"line":2082},90,[189,2084,2047],{"class":514},[189,2086,635],{"class":950},[189,2088,855],{"class":558},[189,2090,2091],{"class":956},"name",[189,2093,674],{"class":673},[189,2095,2096],{"class":950},"n",[189,2098,559],{"class":558},[189,2100,2101],{"class":956}," args",[189,2103,674],{"class":673},[189,2105,2106],{"class":950},"a",[189,2108,2061],{"class":558},[189,2110,2112,2114,2116,2118,2120,2122,2124,2126,2129,2131,2133,2135,2137,2139,2141,2143,2145,2147,2149,2151],{"class":470,"line":2111},91,[189,2113,2067],{"class":514},[189,2115,878],{"class":518},[189,2117,881],{"class":558},[189,2119,1607],{"class":950},[189,2121,855],{"class":558},[189,2123,2096],{"class":950},[189,2125,862],{"class":558},[189,2127,2128],{"class":673}," +",[189,2130,878],{"class":518},[189,2132,881],{"class":558},[189,2134,1607],{"class":950},[189,2136,855],{"class":558},[189,2138,1651],{"class":950},[189,2140,881],{"class":558},[189,2142,1656],{"class":950},[189,2144,855],{"class":558},[189,2146,2106],{"class":950},[189,2148,1664],{"class":558},[189,2150,2128],{"class":673},[189,2152,2153],{"class":779}," 6\n",[189,2155,2157,2159,2161,2163,2166,2168,2171],{"class":470,"line":2156},92,[189,2158,2047],{"class":514},[189,2160,640],{"class":950},[189,2162,855],{"class":558},[189,2164,2165],{"class":956},"content",[189,2167,674],{"class":673},[189,2169,2170],{"class":950},"c",[189,2172,2061],{"class":558},[189,2174,2176,2178,2180,2182,2184,2186,2188,2190,2192],{"class":470,"line":2175},93,[189,2177,2067],{"class":514},[189,2179,878],{"class":518},[189,2181,881],{"class":558},[189,2183,1607],{"class":950},[189,2185,855],{"class":558},[189,2187,2170],{"class":950},[189,2189,862],{"class":558},[189,2191,2128],{"class":673},[189,2193,1946],{"class":779},[189,2195,2197,2199,2201,2203,2205,2207,2209],{"class":470,"line":2196},94,[189,2198,2047],{"class":514},[189,2200,625],{"class":950},[189,2202,855],{"class":558},[189,2204,2054],{"class":956},[189,2206,674],{"class":673},[189,2208,1661],{"class":950},[189,2210,2061],{"class":558},[189,2212,2214],{"class":470,"line":2213},95,[189,2215,2216],{"class":507},"                # Only present when an adapter preserves reasoning on the\n",[189,2218,2220],{"class":470,"line":2219},96,[189,2221,2222],{"class":507},"                # transcript (Anthropic thinking + tools, or explicit\n",[189,2224,2226],{"class":470,"line":2225},97,[189,2227,2228],{"class":507},"                # consumer choice). Weight is the text body; the opaque\n",[189,2230,2232],{"class":470,"line":2231},98,[189,2233,2234],{"class":507},"                # signature \u002F encrypted_content in metadata is negligible.\n",[189,2236,2238,2240,2242,2244,2246,2248,2250],{"class":470,"line":2237},99,[189,2239,2067],{"class":514},[189,2241,878],{"class":518},[189,2243,881],{"class":558},[189,2245,1607],{"class":950},[189,2247,855],{"class":558},[189,2249,1661],{"class":950},[189,2251,654],{"class":558},[189,2253,2255,2257,2260],{"class":470,"line":2254},100,[189,2256,2047],{"class":514},[189,2258,2259],{"class":525}," _",[189,2261,760],{"class":558},[189,2263,2265],{"class":470,"line":2264},101,[189,2266,2267],{"class":507},"                # Defensive fallthrough: new block types added later should\n",[189,2269,2271],{"class":470,"line":2270},102,[189,2272,2273],{"class":507},"                # be undercounted (not crash the measurement component).\n",[189,2275,2277,2279],{"class":470,"line":2276},103,[189,2278,2067],{"class":514},[189,2280,2281],{"class":779}," 0\n",[112,2283,2284],{},"The accountant is pure measurement. It doesn't mutate the transcript, it doesn't prune anything, it doesn't call the provider. It answers one question: given this transcript and these tools, how much of my usable window am I consuming, broken down by where it went?",[256,2286],{},[259,2288,2290],{"id":2289},"_75-per-turn-accounting-in-the-loop","7.5 Per-Turn Accounting in the Loop",[112,2292,2293],{},"The loop threads the accountant through every turn. After each provider response, we reconcile the estimated counts with the provider's reported counts (for calibration), and we expose the snapshot to any caller that wants to observe.",[112,2295,2296,2297,2300,2301,2304],{},"The merged version keeps the Chapter 5 interrupt-safety (",[132,2298,2299],{},"partial_text"," + ",[132,2302,2303],{},"CancelledError"," handling) and adds the accountant + snapshot hook. Paste the whole thing — don't cherry-pick just the new lines:",[460,2306,2308],{"className":498,"code":2307,"language":500,"meta":465,"style":465},"# src\u002Fharness\u002Fagent.py (updated)\nfrom typing import Callable\n\nfrom .context.accountant import ContextAccountant, ContextSnapshot\nfrom .providers.events import StreamEvent\n\n\nasync def arun(\n    provider: Provider,\n    registry: ToolRegistry,\n    user_message: str,\n    transcript: Transcript | None = None,\n    system: str | None = None,\n    on_event: Callable[[StreamEvent], None] | None = None,\n    on_tool_call: Callable[[ToolCall], None] | None = None,\n    on_tool_result: Callable[[ToolResult], None] | None = None,\n    on_snapshot: Callable[[ContextSnapshot], None] | None = None,   # NEW\n    accountant: ContextAccountant | None = None,                    # NEW\n) -> str:\n    if transcript is None:\n        transcript = Transcript(system=system)\n    transcript.append(Message.user_text(user_message))\n    accountant = accountant or ContextAccountant()                  # NEW\n\n    for _ in range(MAX_ITERATIONS):\n        # NEW — measure before each turn.\n        snapshot = accountant.snapshot(transcript, tools=registry.schemas())\n        if on_snapshot is not None:\n            on_snapshot(snapshot)\n        if snapshot.state == \"red\":\n            # Chapter 8 drops the compactor in here.\n            # For now: observe only.\n            pass\n\n        # Unchanged from Ch 5: partial-text rescue + cancel handling around\n        # _one_turn. Don't drop this when merging — it's how Ctrl-C still\n        # captures streamed tokens into the transcript cleanly.\n        partial_text: list[str] = []\n        try:\n            response = await _one_turn(\n                provider, registry, transcript, partial_text, on_event,\n            )\n        except asyncio.CancelledError:\n            if partial_text:\n                transcript.append(Message.assistant_text(\n                    \"\".join(partial_text) + \" [interrupted]\"\n                ))\n            raise\n\n        if response.is_final:\n            transcript.append(Message.from_assistant_response(response))\n            return response.text or \"\"\n\n        transcript.append(Message.from_assistant_response(response))\n        for ref in response.tool_calls:\n            result = registry.dispatch(ref.name, ref.args, ref.id)\n            transcript.append(Message.tool_result(result))\n\n    raise RuntimeError(f\"agent did not finish in {MAX_ITERATIONS} iterations\")\n",[132,2309,2310,2315,2326,2330,2354,2373,2377,2381,2394,2406,2418,2429,2449,2468,2501,2531,2561,2594,2617,2627,2642,2661,2687,2706,2710,2730,2735,2772,2788,2799,2821,2826,2831,2836,2840,2845,2850,2855,2875,2882,2897,2923,2928,2942,2951,2971,2996,3001,3006,3010,3024,3049,3064,3068,3090,3108,3152,3176,3180],{"__ignoreMap":465},[189,2311,2312],{"class":470,"line":471},[189,2313,2314],{"class":507},"# src\u002Fharness\u002Fagent.py (updated)\n",[189,2316,2317,2319,2321,2323],{"class":470,"line":511},[189,2318,515],{"class":514},[189,2320,570],{"class":525},[189,2322,539],{"class":514},[189,2324,2325],{"class":525}," Callable\n",[189,2327,2328],{"class":470,"line":529},[189,2329,533],{"emptyLinePlaceholder":532},[189,2331,2332,2334,2337,2340,2342,2345,2347,2349,2351],{"class":470,"line":536},[189,2333,515],{"class":514},[189,2335,2336],{"class":558}," .",[189,2338,2339],{"class":525},"context",[189,2341,881],{"class":558},[189,2343,2344],{"class":525},"accountant ",[189,2346,539],{"class":514},[189,2348,1328],{"class":525},[189,2350,559],{"class":558},[189,2352,2353],{"class":525}," ContextSnapshot\n",[189,2355,2356,2358,2360,2363,2365,2368,2370],{"class":470,"line":545},[189,2357,515],{"class":514},[189,2359,2336],{"class":558},[189,2361,2362],{"class":525},"providers",[189,2364,881],{"class":558},[189,2366,2367],{"class":525},"events ",[189,2369,539],{"class":514},[189,2371,2372],{"class":525}," StreamEvent\n",[189,2374,2375],{"class":470,"line":565},[189,2376,533],{"emptyLinePlaceholder":532},[189,2378,2379],{"class":470,"line":578},[189,2380,533],{"emptyLinePlaceholder":532},[189,2382,2383,2386,2389,2392],{"class":470,"line":583},[189,2384,2385],{"class":753},"async",[189,2387,2388],{"class":753}," def",[189,2390,2391],{"class":746}," arun",[189,2393,1477],{"class":558},[189,2395,2396,2399,2401,2404],{"class":470,"line":591},[189,2397,2398],{"class":1366},"    provider",[189,2400,769],{"class":558},[189,2402,2403],{"class":525}," Provider",[189,2405,648],{"class":558},[189,2407,2408,2411,2413,2416],{"class":470,"line":596},[189,2409,2410],{"class":1366},"    registry",[189,2412,769],{"class":558},[189,2414,2415],{"class":525}," ToolRegistry",[189,2417,648],{"class":558},[189,2419,2420,2423,2425,2427],{"class":470,"line":612},[189,2421,2422],{"class":1366},"    user_message",[189,2424,769],{"class":558},[189,2426,1372],{"class":772},[189,2428,648],{"class":558},[189,2430,2431,2434,2436,2439,2441,2443,2445,2447],{"class":470,"line":651},[189,2432,2433],{"class":1366},"    transcript",[189,2435,769],{"class":558},[189,2437,2438],{"class":525}," Transcript ",[189,2440,1395],{"class":673},[189,2442,1399],{"class":1398},[189,2444,776],{"class":673},[189,2446,1399],{"class":1398},[189,2448,648],{"class":558},[189,2450,2451,2454,2456,2458,2460,2462,2464,2466],{"class":470,"line":657},[189,2452,2453],{"class":1366},"    system",[189,2455,769],{"class":558},[189,2457,1372],{"class":772},[189,2459,1516],{"class":673},[189,2461,1399],{"class":1398},[189,2463,776],{"class":673},[189,2465,1399],{"class":1398},[189,2467,648],{"class":558},[189,2469,2470,2473,2475,2478,2481,2484,2487,2489,2491,2493,2495,2497,2499],{"class":470,"line":662},[189,2471,2472],{"class":1366},"    on_event",[189,2474,769],{"class":558},[189,2476,2477],{"class":525}," Callable",[189,2479,2480],{"class":558},"[[",[189,2482,2483],{"class":525},"StreamEvent",[189,2485,2486],{"class":558},"],",[189,2488,1399],{"class":1398},[189,2490,945],{"class":558},[189,2492,1516],{"class":673},[189,2494,1399],{"class":1398},[189,2496,776],{"class":673},[189,2498,1399],{"class":1398},[189,2500,648],{"class":558},[189,2502,2503,2506,2508,2510,2512,2515,2517,2519,2521,2523,2525,2527,2529],{"class":470,"line":667},[189,2504,2505],{"class":1366},"    on_tool_call",[189,2507,769],{"class":558},[189,2509,2477],{"class":525},[189,2511,2480],{"class":558},[189,2513,2514],{"class":525},"ToolCall",[189,2516,2486],{"class":558},[189,2518,1399],{"class":1398},[189,2520,945],{"class":558},[189,2522,1516],{"class":673},[189,2524,1399],{"class":1398},[189,2526,776],{"class":673},[189,2528,1399],{"class":1398},[189,2530,648],{"class":558},[189,2532,2533,2536,2538,2540,2542,2545,2547,2549,2551,2553,2555,2557,2559],{"class":470,"line":729},[189,2534,2535],{"class":1366},"    on_tool_result",[189,2537,769],{"class":558},[189,2539,2477],{"class":525},[189,2541,2480],{"class":558},[189,2543,2544],{"class":525},"ToolResult",[189,2546,2486],{"class":558},[189,2548,1399],{"class":1398},[189,2550,945],{"class":558},[189,2552,1516],{"class":673},[189,2554,1399],{"class":1398},[189,2556,776],{"class":673},[189,2558,1399],{"class":1398},[189,2560,648],{"class":558},[189,2562,2563,2566,2568,2570,2572,2575,2577,2579,2581,2583,2585,2587,2589,2591],{"class":470,"line":734},[189,2564,2565],{"class":1366},"    on_snapshot",[189,2567,769],{"class":558},[189,2569,2477],{"class":525},[189,2571,2480],{"class":558},[189,2573,2574],{"class":525},"ContextSnapshot",[189,2576,2486],{"class":558},[189,2578,1399],{"class":1398},[189,2580,945],{"class":558},[189,2582,1516],{"class":673},[189,2584,1399],{"class":1398},[189,2586,776],{"class":673},[189,2588,1399],{"class":1398},[189,2590,559],{"class":558},[189,2592,2593],{"class":507},"   # NEW\n",[189,2595,2596,2599,2601,2604,2606,2608,2610,2612,2614],{"class":470,"line":739},[189,2597,2598],{"class":1366},"    accountant",[189,2600,769],{"class":558},[189,2602,2603],{"class":525}," ContextAccountant ",[189,2605,1395],{"class":673},[189,2607,1399],{"class":1398},[189,2609,776],{"class":673},[189,2611,1399],{"class":1398},[189,2613,559],{"class":558},[189,2615,2616],{"class":507},"                    # NEW\n",[189,2618,2619,2621,2623,2625],{"class":470,"line":750},[189,2620,862],{"class":558},[189,2622,865],{"class":558},[189,2624,1372],{"class":772},[189,2626,760],{"class":558},[189,2628,2629,2632,2635,2638,2640],{"class":470,"line":763},[189,2630,2631],{"class":514},"    if",[189,2633,2634],{"class":525}," transcript ",[189,2636,2637],{"class":673},"is",[189,2639,1399],{"class":1398},[189,2641,760],{"class":558},[189,2643,2644,2647,2649,2651,2653,2655,2657,2659],{"class":470,"line":783},[189,2645,2646],{"class":525},"        transcript ",[189,2648,674],{"class":673},[189,2650,645],{"class":950},[189,2652,855],{"class":558},[189,2654,686],{"class":956},[189,2656,674],{"class":673},[189,2658,686],{"class":950},[189,2660,654],{"class":558},[189,2662,2663,2665,2667,2670,2672,2675,2677,2680,2682,2685],{"class":470,"line":801},[189,2664,2433],{"class":525},[189,2666,881],{"class":558},[189,2668,2669],{"class":950},"append",[189,2671,855],{"class":558},[189,2673,2674],{"class":950},"Message",[189,2676,881],{"class":558},[189,2678,2679],{"class":950},"user_text",[189,2681,855],{"class":558},[189,2683,2684],{"class":950},"user_message",[189,2686,1895],{"class":558},[189,2688,2689,2692,2694,2697,2699,2701,2703],{"class":470,"line":817},[189,2690,2691],{"class":525},"    accountant ",[189,2693,674],{"class":673},[189,2695,2696],{"class":525}," accountant ",[189,2698,1456],{"class":673},[189,2700,1328],{"class":950},[189,2702,1065],{"class":558},[189,2704,2705],{"class":507},"                  # NEW\n",[189,2707,2708],{"class":470,"line":832},[189,2709,533],{"emptyLinePlaceholder":532},[189,2711,2712,2715,2718,2720,2723,2725,2728],{"class":470,"line":837},[189,2713,2714],{"class":514},"    for",[189,2716,2717],{"class":525}," _ ",[189,2719,1050],{"class":514},[189,2721,2722],{"class":1030}," range",[189,2724,855],{"class":558},[189,2726,2727],{"class":1030},"MAX_ITERATIONS",[189,2729,2061],{"class":558},[189,2731,2732],{"class":470,"line":846},[189,2733,2734],{"class":507},"        # NEW — measure before each turn.\n",[189,2736,2737,2740,2742,2745,2747,2750,2752,2754,2756,2759,2761,2764,2766,2769],{"class":470,"line":872},[189,2738,2739],{"class":525},"        snapshot ",[189,2741,674],{"class":673},[189,2743,2744],{"class":525}," accountant",[189,2746,881],{"class":558},[189,2748,2749],{"class":950},"snapshot",[189,2751,855],{"class":558},[189,2753,1612],{"class":950},[189,2755,559],{"class":558},[189,2757,2758],{"class":956}," tools",[189,2760,674],{"class":673},[189,2762,2763],{"class":950},"registry",[189,2765,881],{"class":558},[189,2767,2768],{"class":950},"schemas",[189,2770,2771],{"class":558},"())\n",[189,2773,2774,2776,2779,2781,2784,2786],{"class":470,"line":898},[189,2775,1234],{"class":514},[189,2777,2778],{"class":525}," on_snapshot ",[189,2780,2637],{"class":673},[189,2782,2783],{"class":673}," not",[189,2785,1399],{"class":1398},[189,2787,760],{"class":558},[189,2789,2790,2793,2795,2797],{"class":470,"line":903},[189,2791,2792],{"class":950},"            on_snapshot",[189,2794,855],{"class":558},[189,2796,2749],{"class":950},[189,2798,654],{"class":558},[189,2800,2801,2803,2805,2807,2810,2813,2815,2817,2819],{"class":470,"line":908},[189,2802,1234],{"class":514},[189,2804,1474],{"class":525},[189,2806,881],{"class":558},[189,2808,2809],{"class":884},"state",[189,2811,2812],{"class":673}," ==",[189,2814,693],{"class":482},[189,2816,1208],{"class":478},[189,2818,683],{"class":482},[189,2820,760],{"class":558},[189,2822,2823],{"class":470,"line":915},[189,2824,2825],{"class":507},"            # Chapter 8 drops the compactor in here.\n",[189,2827,2828],{"class":470,"line":925},[189,2829,2830],{"class":507},"            # For now: observe only.\n",[189,2832,2833],{"class":470,"line":967},[189,2834,2835],{"class":514},"            pass\n",[189,2837,2838],{"class":470,"line":993},[189,2839,533],{"emptyLinePlaceholder":532},[189,2841,2842],{"class":470,"line":998},[189,2843,2844],{"class":507},"        # Unchanged from Ch 5: partial-text rescue + cancel handling around\n",[189,2846,2847],{"class":470,"line":1005},[189,2848,2849],{"class":507},"        # _one_turn. Don't drop this when merging — it's how Ctrl-C still\n",[189,2851,2852],{"class":470,"line":1025},[189,2853,2854],{"class":507},"        # captures streamed tokens into the transcript cleanly.\n",[189,2856,2857,2860,2862,2864,2866,2868,2870,2872],{"class":470,"line":1085},[189,2858,2859],{"class":525},"        partial_text",[189,2861,769],{"class":558},[189,2863,1507],{"class":525},[189,2865,680],{"class":558},[189,2867,1539],{"class":772},[189,2869,945],{"class":558},[189,2871,776],{"class":673},[189,2873,2874],{"class":558}," []\n",[189,2876,2877,2880],{"class":470,"line":1090},[189,2878,2879],{"class":514},"        try",[189,2881,760],{"class":558},[189,2883,2884,2887,2889,2892,2895],{"class":470,"line":1097},[189,2885,2886],{"class":525},"            response ",[189,2888,674],{"class":673},[189,2890,2891],{"class":514}," await",[189,2893,2894],{"class":950}," _one_turn",[189,2896,1477],{"class":558},[189,2898,2899,2902,2904,2907,2909,2911,2913,2916,2918,2921],{"class":470,"line":1117},[189,2900,2901],{"class":950},"                provider",[189,2903,559],{"class":558},[189,2905,2906],{"class":950}," registry",[189,2908,559],{"class":558},[189,2910,1722],{"class":950},[189,2912,559],{"class":558},[189,2914,2915],{"class":950}," partial_text",[189,2917,559],{"class":558},[189,2919,2920],{"class":950}," on_event",[189,2922,648],{"class":558},[189,2924,2925],{"class":470,"line":1156},[189,2926,2927],{"class":558},"            )\n",[189,2929,2930,2933,2936,2938,2940],{"class":470,"line":1161},[189,2931,2932],{"class":514},"        except",[189,2934,2935],{"class":525}," asyncio",[189,2937,881],{"class":558},[189,2939,2303],{"class":884},[189,2941,760],{"class":558},[189,2943,2944,2947,2949],{"class":470,"line":1168},[189,2945,2946],{"class":514},"            if",[189,2948,2915],{"class":525},[189,2950,760],{"class":558},[189,2952,2953,2956,2958,2960,2962,2964,2966,2969],{"class":470,"line":1216},[189,2954,2955],{"class":525},"                transcript",[189,2957,881],{"class":558},[189,2959,2669],{"class":950},[189,2961,855],{"class":558},[189,2963,2674],{"class":950},[189,2965,881],{"class":558},[189,2967,2968],{"class":950},"assistant_text",[189,2970,1477],{"class":558},[189,2972,2973,2976,2978,2981,2983,2985,2987,2989,2991,2994],{"class":470,"line":1231},[189,2974,2975],{"class":482},"                    \"\"",[189,2977,881],{"class":558},[189,2979,2980],{"class":950},"join",[189,2982,855],{"class":558},[189,2984,2299],{"class":950},[189,2986,862],{"class":558},[189,2988,2128],{"class":673},[189,2990,693],{"class":482},[189,2992,2993],{"class":478}," [interrupted]",[189,2995,1266],{"class":482},[189,2997,2998],{"class":470,"line":1256},[189,2999,3000],{"class":558},"                ))\n",[189,3002,3003],{"class":470,"line":1269},[189,3004,3005],{"class":514},"            raise\n",[189,3007,3008],{"class":470,"line":1291},[189,3009,533],{"emptyLinePlaceholder":532},[189,3011,3012,3014,3017,3019,3022],{"class":470,"line":1302},[189,3013,1234],{"class":514},[189,3015,3016],{"class":525}," response",[189,3018,881],{"class":558},[189,3020,3021],{"class":884},"is_final",[189,3023,760],{"class":558},[189,3025,3026,3029,3031,3033,3035,3037,3039,3042,3044,3047],{"class":470,"line":1313},[189,3027,3028],{"class":525},"            transcript",[189,3030,881],{"class":558},[189,3032,2669],{"class":950},[189,3034,855],{"class":558},[189,3036,2674],{"class":950},[189,3038,881],{"class":558},[189,3040,3041],{"class":950},"from_assistant_response",[189,3043,855],{"class":558},[189,3045,3046],{"class":950},"response",[189,3048,1895],{"class":558},[189,3050,3051,3053,3055,3057,3059,3061],{"class":470,"line":1318},[189,3052,1259],{"class":514},[189,3054,3016],{"class":525},[189,3056,881],{"class":558},[189,3058,2054],{"class":884},[189,3060,1619],{"class":673},[189,3062,3063],{"class":482}," \"\"\n",[189,3065,3066],{"class":470,"line":1323},[189,3067,533],{"emptyLinePlaceholder":532},[189,3069,3070,3072,3074,3076,3078,3080,3082,3084,3086,3088],{"class":470,"line":1333},[189,3071,1490],{"class":525},[189,3073,881],{"class":558},[189,3075,2669],{"class":950},[189,3077,855],{"class":558},[189,3079,2674],{"class":950},[189,3081,881],{"class":558},[189,3083,3041],{"class":950},[189,3085,855],{"class":558},[189,3087,3046],{"class":950},[189,3089,1895],{"class":558},[189,3091,3092,3094,3097,3099,3101,3103,3106],{"class":470,"line":1347},[189,3093,1952],{"class":514},[189,3095,3096],{"class":525}," ref ",[189,3098,1050],{"class":514},[189,3100,3016],{"class":525},[189,3102,881],{"class":558},[189,3104,3105],{"class":884},"tool_calls",[189,3107,760],{"class":558},[189,3109,3110,3113,3115,3117,3119,3122,3124,3127,3129,3131,3133,3136,3138,3141,3143,3145,3147,3150],{"class":470,"line":1352},[189,3111,3112],{"class":525},"            result ",[189,3114,674],{"class":673},[189,3116,2906],{"class":525},[189,3118,881],{"class":558},[189,3120,3121],{"class":950},"dispatch",[189,3123,855],{"class":558},[189,3125,3126],{"class":950},"ref",[189,3128,881],{"class":558},[189,3130,2091],{"class":884},[189,3132,559],{"class":558},[189,3134,3135],{"class":950}," ref",[189,3137,881],{"class":558},[189,3139,3140],{"class":884},"args",[189,3142,559],{"class":558},[189,3144,3135],{"class":950},[189,3146,881],{"class":558},[189,3148,3149],{"class":884},"id",[189,3151,654],{"class":558},[189,3153,3154,3156,3158,3160,3162,3164,3166,3169,3171,3174],{"class":470,"line":1385},[189,3155,3028],{"class":525},[189,3157,881],{"class":558},[189,3159,2669],{"class":950},[189,3161,855],{"class":558},[189,3163,2674],{"class":950},[189,3165,881],{"class":558},[189,3167,3168],{"class":950},"tool_result",[189,3170,855],{"class":558},[189,3172,3173],{"class":950},"result",[189,3175,1895],{"class":558},[189,3177,3178],{"class":470,"line":1414},[189,3179,533],{"emptyLinePlaceholder":532},[189,3181,3182,3185,3188,3190,3193,3196,3199,3201,3204,3207],{"class":470,"line":1442},[189,3183,3184],{"class":514},"    raise",[189,3186,3187],{"class":772}," RuntimeError",[189,3189,855],{"class":558},[189,3191,3192],{"class":753},"f",[189,3194,3195],{"class":478},"\"agent did not finish in ",[189,3197,3198],{"class":779},"{",[189,3200,2727],{"class":1030},[189,3202,3203],{"class":779},"}",[189,3205,3206],{"class":478}," iterations\"",[189,3208,654],{"class":558},[112,3210,3211],{},"Three observations.",[112,3213,3214,3221,3222,3224,3225,3227,3228,3230,3231,3233,3234,3237,3238,3241,3242,3244],{},[287,3215,3216,3217,3220],{},"The three lines marked ",[132,3218,3219],{},"# NEW"," are all this chapter adds."," Everything else — the ",[132,3223,2299],{}," \u002F ",[132,3226,2303],{}," rescue, the ",[132,3229,3041],{}," commit that preserves ",[132,3232,342],{},", the tool dispatch \u002F result append — carries forward from Chapter 5 unchanged. If you're diffing your copy against §5.6, only ",[132,3235,3236],{},"snapshot = ...",", the ",[132,3239,3240],{},"on_snapshot"," callback, and the empty ",[132,3243,1208],{},"-state branch should be new.",[112,3246,3247,3250],{},[287,3248,3249],{},"The red-state hook is empty on purpose."," Chapter 8 drops in the compactor. Leaving the hook here now means Chapter 8's patch is about three lines of code.",[112,3252,3253,3258],{},[287,3254,3255,3257],{},[132,3256,3240],{}," callback per turn."," This is how you'd wire a CLI or TUI to display live context usage (\"67% \u002F yellow\"). In production harnesses, the same hook feeds your observability pipeline (Chapter 18).",[256,3260],{},[259,3262,3264],{"id":3263},"_76-making-it-visible","7.6 Making It Visible",[112,3266,3267],{},"If you can't see your context filling up, you can't reason about when to compact. A small text visualizer turns the abstract percentages into something you glance at:",[460,3269,3271],{"className":498,"code":3270,"language":500,"meta":465,"style":465},"# examples\u002Fch07_context_usage.py\nimport asyncio\n\nfrom harness.agent import arun\nfrom harness.context.accountant import ContextAccountant\nfrom harness.providers.anthropic import AnthropicProvider\nfrom harness.tools.registry import ToolRegistry\nfrom harness.tools.std import bash, calc, read_file\n\n\ndef display(snap) -> None:\n    bar_width = 40\n    u = snap.utilization\n    filled = int(u * bar_width)\n    empty = bar_width - filled\n    bar = \"█\" * filled + \"░\" * empty\n    state_color = {\"green\": \"\\033[92m\", \"yellow\": \"\\033[93m\", \"red\": \"\\033[91m\"}\n    color = state_color[snap.state]\n    reset = \"\\033[0m\"\n\n    print(f\"\\n{color}[{bar}] {u*100:.0f}% ({snap.state}){reset}\")\n    for k, v in snap.totals.items():\n        if k == \"headroom\":\n            continue\n        print(f\"  {k:10s} {v:>8,d}\")\n    print(f\"  {'usable':10s} {snap.budget.usable:>8,d}\")\n\n\nasync def main():\n    provider = AnthropicProvider()\n    registry = ToolRegistry(tools=[calc, read_file, bash])\n    accountant = ContextAccountant()\n\n    await arun(\n        provider=provider,\n        registry=registry,\n        user_message=(\n            \"Read the file \u002Fetc\u002Fhostname, the file \u002Fetc\u002Fos-release, \"\n            \"the file \u002Fproc\u002Fcpuinfo, and summarize the machine.\"\n        ),\n        on_snapshot=display,\n        accountant=accountant,\n    )\n\n\nasyncio.run(main())\n",[132,3272,3273,3278,3285,3289,3306,3325,3345,3365,3395,3399,3403,3424,3434,3448,3470,3486,3521,3590,3610,3626,3630,3704,3729,3746,3751,3787,3830,3834,3838,3849,3861,3893,3903,3907,3916,3928,3939,3948,3957,3966,3971,3983,3995,4000,4004,4008],{"__ignoreMap":465},[189,3274,3275],{"class":470,"line":471},[189,3276,3277],{"class":507},"# examples\u002Fch07_context_usage.py\n",[189,3279,3280,3282],{"class":470,"line":511},[189,3281,539],{"class":514},[189,3283,3284],{"class":525}," asyncio\n",[189,3286,3287],{"class":470,"line":529},[189,3288,533],{"emptyLinePlaceholder":532},[189,3290,3291,3293,3296,3298,3301,3303],{"class":470,"line":536},[189,3292,515],{"class":514},[189,3294,3295],{"class":525}," harness",[189,3297,881],{"class":558},[189,3299,3300],{"class":525},"agent ",[189,3302,539],{"class":514},[189,3304,3305],{"class":525}," arun\n",[189,3307,3308,3310,3312,3314,3316,3318,3320,3322],{"class":470,"line":545},[189,3309,515],{"class":514},[189,3311,3295],{"class":525},[189,3313,881],{"class":558},[189,3315,2339],{"class":525},[189,3317,881],{"class":558},[189,3319,2344],{"class":525},[189,3321,539],{"class":514},[189,3323,3324],{"class":525}," ContextAccountant\n",[189,3326,3327,3329,3331,3333,3335,3337,3340,3342],{"class":470,"line":565},[189,3328,515],{"class":514},[189,3330,3295],{"class":525},[189,3332,881],{"class":558},[189,3334,2362],{"class":525},[189,3336,881],{"class":558},[189,3338,3339],{"class":525},"anthropic ",[189,3341,539],{"class":514},[189,3343,3344],{"class":525}," AnthropicProvider\n",[189,3346,3347,3349,3351,3353,3355,3357,3360,3362],{"class":470,"line":578},[189,3348,515],{"class":514},[189,3350,3295],{"class":525},[189,3352,881],{"class":558},[189,3354,696],{"class":525},[189,3356,881],{"class":558},[189,3358,3359],{"class":525},"registry ",[189,3361,539],{"class":514},[189,3363,3364],{"class":525}," ToolRegistry\n",[189,3366,3367,3369,3371,3373,3375,3377,3380,3382,3385,3387,3390,3392],{"class":470,"line":583},[189,3368,515],{"class":514},[189,3370,3295],{"class":525},[189,3372,881],{"class":558},[189,3374,696],{"class":525},[189,3376,881],{"class":558},[189,3378,3379],{"class":525},"std ",[189,3381,539],{"class":514},[189,3383,3384],{"class":525}," bash",[189,3386,559],{"class":558},[189,3388,3389],{"class":525}," calc",[189,3391,559],{"class":558},[189,3393,3394],{"class":525}," read_file\n",[189,3396,3397],{"class":470,"line":591},[189,3398,533],{"emptyLinePlaceholder":532},[189,3400,3401],{"class":470,"line":596},[189,3402,533],{"emptyLinePlaceholder":532},[189,3404,3405,3408,3411,3413,3416,3418,3420,3422],{"class":470,"line":612},[189,3406,3407],{"class":753},"def",[189,3409,3410],{"class":746}," display",[189,3412,855],{"class":558},[189,3414,3415],{"class":1366},"snap",[189,3417,862],{"class":558},[189,3419,865],{"class":558},[189,3421,1399],{"class":1398},[189,3423,760],{"class":558},[189,3425,3426,3429,3431],{"class":470,"line":651},[189,3427,3428],{"class":525},"    bar_width ",[189,3430,674],{"class":673},[189,3432,3433],{"class":779}," 40\n",[189,3435,3436,3439,3441,3444,3446],{"class":470,"line":657},[189,3437,3438],{"class":525},"    u ",[189,3440,674],{"class":673},[189,3442,3443],{"class":525}," snap",[189,3445,881],{"class":558},[189,3447,1228],{"class":884},[189,3449,3450,3453,3455,3457,3459,3462,3465,3468],{"class":470,"line":662},[189,3451,3452],{"class":525},"    filled ",[189,3454,674],{"class":673},[189,3456,773],{"class":772},[189,3458,855],{"class":558},[189,3460,3461],{"class":950},"u ",[189,3463,3464],{"class":673},"*",[189,3466,3467],{"class":950}," bar_width",[189,3469,654],{"class":558},[189,3471,3472,3475,3477,3480,3483],{"class":470,"line":667},[189,3473,3474],{"class":525},"    empty ",[189,3476,674],{"class":673},[189,3478,3479],{"class":525}," bar_width ",[189,3481,3482],{"class":673},"-",[189,3484,3485],{"class":525}," filled\n",[189,3487,3488,3491,3493,3495,3498,3500,3503,3506,3509,3511,3514,3516,3518],{"class":470,"line":729},[189,3489,3490],{"class":525},"    bar ",[189,3492,674],{"class":673},[189,3494,693],{"class":482},[189,3496,3497],{"class":478},"█",[189,3499,683],{"class":482},[189,3501,3502],{"class":673}," *",[189,3504,3505],{"class":525}," filled ",[189,3507,3508],{"class":673},"+",[189,3510,693],{"class":482},[189,3512,3513],{"class":478},"░",[189,3515,683],{"class":482},[189,3517,3502],{"class":673},[189,3519,3520],{"class":525}," empty\n",[189,3522,3523,3526,3528,3531,3533,3535,3537,3539,3541,3544,3547,3549,3551,3553,3555,3557,3559,3561,3563,3566,3568,3570,3572,3574,3576,3578,3580,3582,3585,3587],{"class":470,"line":734},[189,3524,3525],{"class":525},"    state_color ",[189,3527,674],{"class":673},[189,3529,3530],{"class":558}," {",[189,3532,683],{"class":482},[189,3534,1190],{"class":478},[189,3536,683],{"class":482},[189,3538,769],{"class":558},[189,3540,693],{"class":482},[189,3542,3543],{"class":518},"\\033",[189,3545,3546],{"class":478},"[92m",[189,3548,683],{"class":482},[189,3550,559],{"class":558},[189,3552,693],{"class":482},[189,3554,1199],{"class":478},[189,3556,683],{"class":482},[189,3558,769],{"class":558},[189,3560,693],{"class":482},[189,3562,3543],{"class":518},[189,3564,3565],{"class":478},"[93m",[189,3567,683],{"class":482},[189,3569,559],{"class":558},[189,3571,693],{"class":482},[189,3573,1208],{"class":478},[189,3575,683],{"class":482},[189,3577,769],{"class":558},[189,3579,693],{"class":482},[189,3581,3543],{"class":518},[189,3583,3584],{"class":478},"[91m",[189,3586,683],{"class":482},[189,3588,3589],{"class":558},"}\n",[189,3591,3592,3595,3597,3600,3602,3604,3606,3608],{"class":470,"line":739},[189,3593,3594],{"class":525},"    color ",[189,3596,674],{"class":673},[189,3598,3599],{"class":525}," state_color",[189,3601,680],{"class":558},[189,3603,3415],{"class":525},[189,3605,881],{"class":558},[189,3607,2809],{"class":884},[189,3609,726],{"class":558},[189,3611,3612,3615,3617,3619,3621,3624],{"class":470,"line":750},[189,3613,3614],{"class":525},"    reset ",[189,3616,674],{"class":673},[189,3618,693],{"class":482},[189,3620,3543],{"class":518},[189,3622,3623],{"class":478},"[0m",[189,3625,1266],{"class":482},[189,3627,3628],{"class":470,"line":763},[189,3629,533],{"emptyLinePlaceholder":532},[189,3631,3632,3635,3637,3639,3641,3644,3646,3649,3651,3653,3655,3658,3660,3663,3665,3668,3670,3673,3676,3678,3681,3683,3685,3687,3689,3691,3693,3695,3698,3700,3702],{"class":470,"line":783},[189,3633,3634],{"class":1030},"    print",[189,3636,855],{"class":558},[189,3638,3192],{"class":753},[189,3640,683],{"class":478},[189,3642,3643],{"class":518},"\\n",[189,3645,3198],{"class":779},[189,3647,3648],{"class":950},"color",[189,3650,3203],{"class":779},[189,3652,680],{"class":478},[189,3654,3198],{"class":779},[189,3656,3657],{"class":950},"bar",[189,3659,3203],{"class":779},[189,3661,3662],{"class":478},"] ",[189,3664,3198],{"class":779},[189,3666,3667],{"class":950},"u",[189,3669,3464],{"class":673},[189,3671,3672],{"class":779},"100",[189,3674,3675],{"class":753},":.0f",[189,3677,3203],{"class":779},[189,3679,3680],{"class":478},"% (",[189,3682,3198],{"class":779},[189,3684,3415],{"class":950},[189,3686,881],{"class":558},[189,3688,2809],{"class":884},[189,3690,3203],{"class":779},[189,3692,862],{"class":478},[189,3694,3198],{"class":779},[189,3696,3697],{"class":950},"reset",[189,3699,3203],{"class":779},[189,3701,683],{"class":478},[189,3703,654],{"class":558},[189,3705,3706,3708,3710,3712,3714,3716,3718,3720,3722,3724,3726],{"class":470,"line":801},[189,3707,2714],{"class":514},[189,3709,1042],{"class":525},[189,3711,559],{"class":558},[189,3713,1047],{"class":525},[189,3715,1050],{"class":514},[189,3717,3443],{"class":525},[189,3719,881],{"class":558},[189,3721,1057],{"class":884},[189,3723,881],{"class":558},[189,3725,1062],{"class":950},[189,3727,3728],{"class":558},"():\n",[189,3730,3731,3733,3735,3738,3740,3742,3744],{"class":470,"line":817},[189,3732,1234],{"class":514},[189,3734,1071],{"class":525},[189,3736,3737],{"class":673},"==",[189,3739,693],{"class":482},[189,3741,182],{"class":478},[189,3743,683],{"class":482},[189,3745,760],{"class":558},[189,3747,3748],{"class":470,"line":832},[189,3749,3750],{"class":514},"            continue\n",[189,3752,3753,3756,3758,3760,3763,3765,3768,3771,3773,3775,3778,3781,3783,3785],{"class":470,"line":837},[189,3754,3755],{"class":1030},"        print",[189,3757,855],{"class":558},[189,3759,3192],{"class":753},[189,3761,3762],{"class":478},"\"  ",[189,3764,3198],{"class":779},[189,3766,3767],{"class":950},"k",[189,3769,3770],{"class":753},":10s",[189,3772,3203],{"class":779},[189,3774,3530],{"class":779},[189,3776,3777],{"class":950},"v",[189,3779,3780],{"class":753},":>8,d",[189,3782,3203],{"class":779},[189,3784,683],{"class":478},[189,3786,654],{"class":558},[189,3788,3789,3791,3793,3795,3797,3799,3802,3804,3806,3808,3810,3812,3814,3816,3818,3820,3822,3824,3826,3828],{"class":470,"line":846},[189,3790,3634],{"class":1030},[189,3792,855],{"class":558},[189,3794,3192],{"class":753},[189,3796,3762],{"class":478},[189,3798,3198],{"class":779},[189,3800,3801],{"class":482},"'",[189,3803,1146],{"class":478},[189,3805,3801],{"class":482},[189,3807,3770],{"class":753},[189,3809,3203],{"class":779},[189,3811,3530],{"class":779},[189,3813,3415],{"class":950},[189,3815,881],{"class":558},[189,3817,1141],{"class":884},[189,3819,881],{"class":558},[189,3821,1146],{"class":884},[189,3823,3780],{"class":753},[189,3825,3203],{"class":779},[189,3827,683],{"class":478},[189,3829,654],{"class":558},[189,3831,3832],{"class":470,"line":872},[189,3833,533],{"emptyLinePlaceholder":532},[189,3835,3836],{"class":470,"line":898},[189,3837,533],{"emptyLinePlaceholder":532},[189,3839,3840,3842,3844,3847],{"class":470,"line":903},[189,3841,2385],{"class":753},[189,3843,2388],{"class":753},[189,3845,3846],{"class":746}," main",[189,3848,3728],{"class":558},[189,3850,3851,3854,3856,3859],{"class":470,"line":908},[189,3852,3853],{"class":525},"    provider ",[189,3855,674],{"class":673},[189,3857,3858],{"class":950}," AnthropicProvider",[189,3860,1461],{"class":558},[189,3862,3863,3866,3868,3870,3872,3874,3876,3878,3881,3883,3886,3888,3890],{"class":470,"line":915},[189,3864,3865],{"class":525},"    registry ",[189,3867,674],{"class":673},[189,3869,2415],{"class":950},[189,3871,855],{"class":558},[189,3873,696],{"class":956},[189,3875,674],{"class":673},[189,3877,680],{"class":558},[189,3879,3880],{"class":950},"calc",[189,3882,559],{"class":558},[189,3884,3885],{"class":950}," read_file",[189,3887,559],{"class":558},[189,3889,3384],{"class":950},[189,3891,3892],{"class":558},"])\n",[189,3894,3895,3897,3899,3901],{"class":470,"line":925},[189,3896,2691],{"class":525},[189,3898,674],{"class":673},[189,3900,1328],{"class":950},[189,3902,1461],{"class":558},[189,3904,3905],{"class":470,"line":967},[189,3906,533],{"emptyLinePlaceholder":532},[189,3908,3909,3912,3914],{"class":470,"line":993},[189,3910,3911],{"class":514},"    await",[189,3913,2391],{"class":950},[189,3915,1477],{"class":558},[189,3917,3918,3921,3923,3926],{"class":470,"line":998},[189,3919,3920],{"class":956},"        provider",[189,3922,674],{"class":673},[189,3924,3925],{"class":950},"provider",[189,3927,648],{"class":558},[189,3929,3930,3933,3935,3937],{"class":470,"line":1005},[189,3931,3932],{"class":956},"        registry",[189,3934,674],{"class":673},[189,3936,2763],{"class":950},[189,3938,648],{"class":558},[189,3940,3941,3944,3946],{"class":470,"line":1025},[189,3942,3943],{"class":956},"        user_message",[189,3945,674],{"class":673},[189,3947,1477],{"class":558},[189,3949,3950,3952,3955],{"class":470,"line":1085},[189,3951,1594],{"class":482},[189,3953,3954],{"class":478},"Read the file \u002Fetc\u002Fhostname, the file \u002Fetc\u002Fos-release, ",[189,3956,1266],{"class":482},[189,3958,3959,3961,3964],{"class":470,"line":1090},[189,3960,1594],{"class":482},[189,3962,3963],{"class":478},"the file \u002Fproc\u002Fcpuinfo, and summarize the machine.",[189,3965,1266],{"class":482},[189,3967,3968],{"class":470,"line":1097},[189,3969,3970],{"class":558},"        ),\n",[189,3972,3973,3976,3978,3981],{"class":470,"line":1117},[189,3974,3975],{"class":956},"        on_snapshot",[189,3977,674],{"class":673},[189,3979,3980],{"class":950},"display",[189,3982,648],{"class":558},[189,3984,3985,3988,3990,3993],{"class":470,"line":1156},[189,3986,3987],{"class":956},"        accountant",[189,3989,674],{"class":673},[189,3991,3992],{"class":950},"accountant",[189,3994,648],{"class":558},[189,3996,3997],{"class":470,"line":1161},[189,3998,3999],{"class":558},"    )\n",[189,4001,4002],{"class":470,"line":1168},[189,4003,533],{"emptyLinePlaceholder":532},[189,4005,4006],{"class":470,"line":1216},[189,4007,533],{"emptyLinePlaceholder":532},[189,4009,4010,4013,4015,4018,4020,4023],{"class":470,"line":1231},[189,4011,4012],{"class":525},"asyncio",[189,4014,881],{"class":558},[189,4016,4017],{"class":950},"run",[189,4019,855],{"class":558},[189,4021,4022],{"class":950},"main",[189,4024,2771],{"class":558},[112,4026,4027,4028,4031],{},"Run it. You'll see the context usage grow turn-by-turn. The jumps come from tool outputs — ",[132,4029,4030],{},"\u002Fproc\u002Fcpuinfo"," on a typical machine is ~20KB ≈ 5000 tokens, one tool result that shifts your utilization several percentage points. Do this with a prompt that reads three large files and you'll watch the bar walk toward yellow in real time. That's the point. What was invisible is now something you watch.",[256,4033],{},[259,4035,4037],{"id":4036},"_77-observations-worth-keeping","7.7 Observations Worth Keeping",[112,4039,4040],{},"Three patterns show up reliably once you start watching the accountant.",[112,4042,4043,4046,4047,4050],{},[287,4044,4045],{},"Tool results dominate."," In most agentic workloads, by turn ten, tool results are 70–90% of the transcript. System prompts and tool schemas are rounding errors; the history is mostly what the tools returned. That's why Chapter 11 is devoted to tool ",[115,4048,4049],{},"output"," design — smaller, structured outputs are the single highest-leverage context intervention.",[112,4052,4053,4056],{},[287,4054,4055],{},"User messages are tiny."," The human at the other end writes a paragraph per turn, maybe. The model reads kilobytes. This asymmetry is one reason why the naive \"just make the context window bigger\" intuition fails: the user isn't the one filling it.",[112,4058,4059,4062],{},[287,4060,4061],{},"Assistant reasoning is the third bulge."," When you run an agent that thinks out loud — with extended thinking, or ReAct-style verbose reasoning — the assistant's own text can approach the size of tool results. The decision to log the reasoning (useful for debugging, expensive for context) is one you make consciously once you're accounting.",[256,4064],{},[259,4066,4068],{"id":4067},"_78-what-about-cache-discounts","7.8 What About Cache Discounts?",[112,4070,4071],{},"Both Anthropic and OpenAI support prompt caching: a long stable prefix (system prompt + tool schemas) can be marked for caching, and subsequent calls that share that prefix are charged at ~10% of the input rate (Anthropic's cache reads) or similar (OpenAI's implicit caching).",[112,4073,4074,4075,4078],{},"A cached prefix takes the same space in the context window — caching is a billing optimization, not a window optimization. Our accountant counts the raw tokens regardless of cache state. If you want to track cache-effective cost separately, Chapter 20 introduces a ",[132,4076,4077],{},"CostAccountant"," that pairs with this one.",[256,4080],{},[259,4082,4084],{"id":4083},"_79-commit","7.9 Commit",[460,4086,4088],{"className":462,"code":4087,"language":464,"meta":465,"style":465},"git add -A && git commit -m \"ch07: ContextAccountant and per-component token accounting\"\ngit tag ch07-accounting\n",[132,4089,4090,4120],{"__ignoreMap":465},[189,4091,4092,4095,4097,4101,4104,4107,4110,4113,4115,4118],{"class":470,"line":471},[189,4093,4094],{"class":474},"git",[189,4096,479],{"class":478},[189,4098,4100],{"class":4099},"stzsN"," -A",[189,4102,4103],{"class":558}," &&",[189,4105,4106],{"class":474}," git",[189,4108,4109],{"class":478}," commit",[189,4111,4112],{"class":4099}," -m",[189,4114,693],{"class":482},[189,4116,4117],{"class":478},"ch07: ContextAccountant and per-component token accounting",[189,4119,1266],{"class":482},[189,4121,4122,4124,4127],{"class":470,"line":511},[189,4123,4094],{"class":474},[189,4125,4126],{"class":478}," tag",[189,4128,4129],{"class":478}," ch07-accounting\n",[259,4131,4133],{"id":4132},"_710-try-it-yourself","7.10 Try It Yourself",[4135,4136,4137,4143,4156],"ol",{},[358,4138,4139,4142],{},[287,4140,4141],{},"Measure the naive loop."," Run the Chapter 2 calculator example through the accountant. How much of the budget did a simple arithmetic task consume? Compare to a prompt that reads three medium-sized files. Where did the budget go?",[358,4144,4145,4148,4149,4152,4153,4155],{},[287,4146,4147],{},"Calibrate against ground truth."," After each provider call, compare the accountant's estimate to ",[132,4150,4151],{},"response.input_tokens",". How far off is the local ",[132,4154,406],{}," estimate for your primary provider? Write a small report and keep it — you'll want it in Chapter 20 when cost accounting gets serious.",[358,4157,4158,4161],{},[287,4159,4160],{},"Find your red line."," Design a prompt that forces the agent to pull in enough context to push past 80% utilization. Run it. Does the model's behavior change as utilization climbs through yellow into red? You now have a bench test for Chapter 8's compaction.",[256,4163],{},[4165,4166,4167,4170],"what-you-understand",{},[112,4168,4169],{},"Your harness sees its context window. Every turn produces a snapshot with per-component token counts and a traffic-light state — green, yellow, red. The naive intuition that the context window is a fixed pile of bytes has been replaced with an operational view: a budget you spend, broken down by where you spent it.",[112,4171,4172],{},"What's still missing: the reaction. When the accountant returns red, the loop currently notes it and proceeds. The transcript keeps growing. Chapter 8 adds the compactor that fires on red state — observation masking first, LLM summarization if still over — and finally closes Break 5 from Chapter 2. After Chapter 8, your loop can run through dozens of turns without degrading.",[4174,4175,4176],"style",{},"html pre.shiki code .sbgvK, html code.shiki .sbgvK{--shiki-light:#E2931D;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .s_sjI, html code.shiki .s_sjI{--shiki-light:#91B859;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .sjJ54, html code.shiki .sjJ54{--shiki-light:#39ADB5;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .sutJx, html code.shiki .sutJx{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#6A737D;--shiki-default-font-style:inherit;--shiki-dark:#6A737D;--shiki-dark-font-style:inherit}html pre.shiki code .sVHd0, html code.shiki .sVHd0{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#D73A49;--shiki-default-font-style:inherit;--shiki-dark:#F97583;--shiki-dark-font-style:inherit}html pre.shiki code .s_hVV, html code.shiki .s_hVV{--shiki-light:#90A4AE;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .stp6e, html code.shiki .stp6e{--shiki-light:#39ADB5;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sGLFI, html code.shiki .sGLFI{--shiki-light:#6182B8;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sbsja, html code.shiki .sbsja{--shiki-light:#9C3EDA;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sZMiF, html code.shiki .sZMiF{--shiki-light:#E2931D;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .smCYv, html code.shiki .smCYv{--shiki-light:#E53935;--shiki-light-font-style:italic;--shiki-default:#24292E;--shiki-default-font-style:inherit;--shiki-dark:#E1E4E8;--shiki-dark-font-style:inherit}html pre.shiki code .skxfh, html code.shiki .skxfh{--shiki-light:#E53935;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .slqww, html code.shiki .slqww{--shiki-light:#6182B8;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .s99_P, html code.shiki .s99_P{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#E36209;--shiki-default-font-style:inherit;--shiki-dark:#FFAB70;--shiki-dark-font-style:inherit}html pre.shiki code .sptTA, html code.shiki .sptTA{--shiki-light:#6182B8;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .s2W-s, html code.shiki .s2W-s{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#032F62;--shiki-default-font-style:inherit;--shiki-dark:#9ECBFF;--shiki-dark-font-style:inherit}html pre.shiki code .sithA, html code.shiki .sithA{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#032F62;--shiki-default-font-style:inherit;--shiki-dark:#9ECBFF;--shiki-dark-font-style:inherit}html pre.shiki code .sFwrP, html code.shiki .sFwrP{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#24292E;--shiki-default-font-style:inherit;--shiki-dark:#E1E4E8;--shiki-dark-font-style:inherit}html pre.shiki code .s39Yj, html code.shiki .s39Yj{--shiki-light:#39ADB5;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .stzsN, html code.shiki .stzsN{--shiki-light:#91B859;--shiki-default:#005CC5;--shiki-dark:#79B8FF}",{"title":465,"searchDepth":511,"depth":511,"links":4178},[4179,4180,4181,4182,4183,4184,4185,4186,4187,4188],{"id":261,"depth":511,"text":262},{"id":295,"depth":511,"text":296},{"id":389,"depth":511,"text":390},{"id":494,"depth":511,"text":495},{"id":2289,"depth":511,"text":2290},{"id":3263,"depth":511,"text":3264},{"id":4036,"depth":511,"text":4037},{"id":4067,"depth":511,"text":4068},{"id":4083,"depth":511,"text":4084},{"id":4132,"depth":511,"text":4133},"md",{},null,{"title":38,"description":117},"Fn1JYmG6CgFy0bSPA7DGlbORopS8BJWVCG6ALCCpN5Q",[4195,4197],{"title":34,"path":35,"stem":36,"description":4196,"children":-1},"Previously: streaming, interruption, retries. The loop survives network failures and closes cleanly on Ctrl-C. But a misnamed argument still fails inside the tool function, and the registry still can't tell when the model is spinning.",{"title":42,"path":43,"stem":44,"description":4198,"children":-1},"Previously: the accountant sees the context window. Red state is detected but not acted upon. The transcript keeps growing until the provider refuses to accept it.",1776848984224]