physics.context
physics.context
Context engineering: the equations behind what one agent request carries.
Pure arithmetic on serialized token counts (added 2026-10-20). A request is budgeted by role against the window, laid out in zones ordered by change rate so a serving engine can reuse its prefix, compacted between two watermarks, and edited when a write supersedes an observation. One attention head’s softmax gives the first-order cost of every admitted token. The solvers in engine/solvers/context.py compose these.
Functions
| Name | Description |
|---|---|
| calc_compaction_cycle | Return turns between two context-compaction watermarks. |
| calc_equivalent_key_count | Return an equal-normalizer count at a reference logit. |
| calc_observation_budget | Return the token budget remaining for observations. |
| calc_softmax_target_weight | Return a target key’s softmax weight against grouped competitors. |
| calc_turns_that_fit | Return the number of complete observation turns that fit. |
| calc_zoned_prefill_tokens | Count uncached input tokens in a root-trunk-leaf trajectory. |
calc_compaction_cycle
physics.context.calc_compaction_cycle(trigger, target, tokens_per_turn)Return turns between two context-compaction watermarks.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| trigger | int | Token count at which compaction fires; greater than target. | required |
| target | int | Token count immediately after compaction; nonnegative. | required |
| tokens_per_turn | int | Fixed tokens appended per turn; positive. | required |
Returns
| Name | Type | Description |
|---|---|---|
| int | Turns until the first request reaches or exceeds the trigger. |
Notes
Equation: n_cycle = ceil((trigger - target) / tokens_per_turn).
Assumptions: Compaction resets exactly to target and observations append at a fixed rate.
Excludes: Compaction latency, information loss, and variable-size observations.
References
Derivation: Solve target + n*tokens_per_turn >= trigger for the smallest integer n.
Examples
>>> calc_compaction_cycle(100, 40, 20) == 3
Truecalc_equivalent_key_count
physics.context.calc_equivalent_key_count(count, logit, reference_logit)Return an equal-normalizer count at a reference logit.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| count | int | Number of original keys; nonnegative. | required |
| logit | float | Dimensionless score shared by the original keys. | required |
| reference_logit | float | Dimensionless score assigned to equivalent keys. | required |
Returns
| Name | Type | Description |
|---|---|---|
| float | Equivalent key count; fractional counts represent normalizer mass. |
Notes
Equation: n_equivalent = count * exp(logit - reference_logit).
Assumptions: Logits are fixed and their exponential difference is representable.
Excludes: Integer rounding and changes in attention scores induced by the input.
References
Derivation: Solve n_equivalent exp(reference_logit) = count exp(logit) for the equivalent count.
Examples
>>> calc_equivalent_key_count(4, 2.0, 2.0) == 4
Truecalc_observation_budget
physics.context.calc_observation_budget(window, output_reserve, fixed_tokens)Return the token budget remaining for observations.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| window | int | Total context-window capacity in tokens; nonnegative. | required |
| output_reserve | int | Tokens reserved for the generated reply; nonnegative. | required |
| fixed_tokens | int | Serialized system, tool, and persistent-state tokens; nonnegative. | required |
Returns
| Name | Type | Description |
|---|---|---|
| int | Available observation tokens; a negative result denotes overcommitment. |
Notes
Equation: S_obs = window - output_reserve - fixed_tokens.
Assumptions: All three counts refer to the same tokenizer and request.
Excludes: Tokenizer estimation, KV capacity, and truncation; negative budgets are not clamped.
References
Derivation: Subtract disjoint reservations from a finite token capacity.
Examples
>>> calc_observation_budget(100, 20, 30) == 100 - 20 - 30
Truecalc_softmax_target_weight
physics.context.calc_softmax_target_weight(target_logit, competitors)Return a target key’s softmax weight against grouped competitors.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| target_logit | float | Dimensionless target score; its exponential must be representable. | required |
| competitors | iterable of tuple | Pairs of nonnegative key counts and dimensionless logits. | required |
Returns
| Name | Type | Description |
|---|---|---|
| float | Normalized target weight, between zero and one for valid inputs. |
Notes
Equation: a = exp(z_t) / (exp(z_t) + sum_j n_j exp(z_j)).
Assumptions: Competitor keys in each group share a fixed logit; there is one target key, and logits are shifted by the maximum score to prevent overflow.
Excludes: Learned changes to logits and multihead aggregation.
Adding keys at fixed logits increases the normalizer and reduces target weight.
References
Derivation: Expand the softmax denominator by repeated equal-logit terms; each group contributes n_j exp(z_j).
Examples
>>> calc_softmax_target_weight(0.0, [(1, 0.0)]) == 0.5
Truecalc_turns_that_fit
physics.context.calc_turns_that_fit(observation_budget, tokens_per_turn)Return the number of complete observation turns that fit.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| observation_budget | int | Available observation capacity in tokens; nonnegative. | required |
| tokens_per_turn | int | Fixed serialized tokens appended per turn; positive. | required |
Returns
| Name | Type | Description |
|---|---|---|
| int | Number of whole turns admitted by the token budget. |
Notes
Equation: turns = floor(observation_budget / tokens_per_turn).
Assumptions: Every turn contributes the same token count and existing state stays fixed.
Excludes: Partial turns, variable-length observations, and context compaction.
References
Derivation: Integer division counts complete equal-sized groups within a capacity.
Examples
>>> calc_turns_that_fit(100, 30) == 100 // 30
Truecalc_zoned_prefill_tokens
physics.context.calc_zoned_prefill_tokens(
turns,
root,
trunk,
leaf,
trunk_rewrites,
)Count uncached input tokens in a root-trunk-leaf trajectory.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| turns | int | Number of calls in the trajectory; at least one. | required |
| root | int | Invariant leading-prefix size in tokens; nonnegative. | required |
| trunk | int | Less frequently changed middle-prefix size in tokens; nonnegative. | required |
| leaf | int | Per-call changing suffix size in tokens; nonnegative. | required |
| trunk_rewrites | int | Calls after the first that rewrite the trunk; from zero to turns - 1. | required |
Returns
| Name | Type | Description |
|---|---|---|
| int | Total tokens prefilling uncached input across all calls. |
Notes
Equation: total = root + trunk + leaf + R*(trunk + leaf) + (H - 1 - R)*leaf.
Assumptions: The first call is cold; matching root and trunk prefixes remain cached for subsequent calls.
Excludes: Eviction, cache lookup overhead, output tokens, and partial trunk-prefix reuse.
References
Derivation: Partition calls into one cold call, R trunk rewrites, and H - 1 - R leaf-only updates.
Examples
>>> calc_zoned_prefill_tokens(3, 10, 20, 5, 1) == (10 + 20 + 5) + (20 + 5) + 5
True