ContextConcepts

Budgets and prompts

Every request is assembled inside a token budget, in a deterministic order, and reports what it cost.

session.prompt() builds the next request as a value: ordered sections, tools, a token report, cache breakpoints and prefix digests. Same registry, same state, same arguments: same bytes, in both languages.

Tiers

Providers cache a byte prefix, so Context orders everything by how often it changes.

TierContentsChanges when
staticBase instructions, always skills, the catalog, ambient tools, load_skillYou deploy
sessionSkills loaded by a router, trigger or code, in load orderA skill loads
turnYour per-request extras: retrieved snippets, the date, idsEvery request

Skills the model loads arrive as load_skill results inside the conversation, so they never touch the prefix at all. Nothing time-like may go in static or session: pass it as a turn extra.

The budget

A bare number is input_tokens: the most the next request's input may contain. Per-section caps are optional:

input_tokens: 16000
sections: { catalog: 1500, skills: 6000 }

Everything Context produces is counted, wherever it ends up: system sections, tool schemas, and the instructions of every skill the model loaded (the session knows what it returned), including skills since evicted or unloaded: their instructions are still in the conversation (the sent section). Evicting a skill the model loaded frees only its tool schemas; unloading a skill loaded from code also frees its instructions. Pass the conversation as messages and the rest of it is counted too, without double counting the load_skill results inside it; budget.scope says which (context or request). Leave room for the output yourself.

When a request is over budget, Context gives way in a fixed order and records every decision in prompt.dropped: turn extras are truncated at a paragraph boundary, then dropped by priority; catalog entries degrade to their first sentence, then to the name. Skills are never dropped at prompt time, because a skill's instructions and its tools must agree. If what must be kept still does not fit, the prompt comes back with budget.ok false; call raise_for_budget() (Python) or throwIfOverBudget() (TypeScript) to get an exception instead.

Loads check the budget before anything changes. By default (on_over_budget, TypeScript onOverBudget, set to "evict") the lowest-priority, least-recently-loaded model, router and trigger loads are unloaded to make room and listed in evicted; if even that cannot make room, the model reads how many tokens the skill needs and that it should continue without it. With "reject" nothing is unloaded and the model reads that it should continue with what is loaded. Refusals never ask the model to unload anything: it has no tool for that.

Counting

The default counter, estimate, is a deterministic estimator with no dependencies, identical in both languages and calibrated to overcount slightly (median 1.17x against o200k on the calibration set; table in bench/CALIBRATION.md). Use a real counter when exact numbers matter: tiktoken:o200k_base, or a provider's counting endpoint (anthropic:<model>, openai:<model>), which runs once at load and caches by content hash so prompt() stays synchronous.

Testing prefix stability

prompt.shares_prefix_with(other) (TypeScript sharesPrefixWith) catches the timestamp someone put in the system prompt before it costs money, and the scripted runs in the testing helpers count cache invalidations per provider.

On this page