engine.solvers.agents
engine.solvers.agents
Agent solvers: reliability, cost, and context over a whole trajectory.
Domain implementations behind mlsysim.solvers (the public import path, derived from engine.solvers.__init__); kept per-domain so the logic stays reviewable. The unit of account is the delegated task, not the model call: a task succeeds only if every unchecked turn does, costs every attempt it takes until one is accepted, and re-reads its own history on every turn.
Classes
| Name | Description |
|---|---|
| AcceptedTaskCostModel | Cost per accepted task, C_eff = E[C_attempt] / p_acc, and two levers on it. |
| AppendOnlyContextModel | Context read by the last call, and input resent in total, by an append-only trajectory. |
| HorizonCompoundingModel | Unchecked task success over a horizon of independent steps. |
AcceptedTaskCostModel
engine.solvers.agents.AcceptedTaskCostModel()Cost per accepted task, C_eff = E[C_attempt] / p_acc, and two levers on it.
An attempt costs model tokens plus everything else (sandbox, tests, review); attempts repeat until one is accepted. A price cut removes a fraction of one component; an acceptance gain divides the whole attempt. The break-even acceptance is the acceptance the price cut is worth. Charging only the accepted attempt understates cost by 1 / p_acc. Growing the model cost by a fraction g raises the attempt to C' = (1 + g) C_model + C_other; cost per accepted task holds only if acceptance rises to p_acc C' / C.
Methods
| Name | Description |
|---|---|
| solve | Account for repeated attempts until a task is accepted. |
solve
engine.solvers.agents.AcceptedTaskCostModel.solve(
model_cost,
other_cost,
acceptance,
token_price_cut=None,
improved_acceptance=None,
other_cost_cut=None,
acceptance_gain=None,
independent_attempts=None,
model_cost_growth=None,
)Account for repeated attempts until a task is accepted.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| model_cost | Quantity | Nonnegative model cost per attempt, convertible to USD. | required |
| other_cost | Quantity | Nonnegative non-model cost per attempt in USD; total attempt cost must be positive. | required |
| acceptance | float | Acceptance probability per attempt in (0, 1]. | required |
| token_price_cut | float | Fraction of model cost removed, in [0, 1]; None omits this comparison. | None |
| improved_acceptance | float | Alternative acceptance probability in (0, 1]; None omits it. | None |
| other_cost_cut | float | Fraction of non-model cost removed, in [0, 1]; None omits it. | None |
| acceptance_gain | float | Additive acceptance gain; acceptance plus gain must be in (0, 1]. | None |
| independent_attempts | int | Positive count for the probability of at least one accepted attempt. | None |
| model_cost_growth | float | Fractional increase in model cost; must be at least -1. None omits it. | None |
Returns
| Name | Type | Description |
|---|---|---|
| AcceptedTaskCostResult | Costs as USD-valued scalars, dimensionless cost shares and acceptance thresholds, and optional comparison fields. |
Notes
Equation: Effective cost is (model_cost + other_cost) / acceptance. A model price cut c changes the numerator to model_cost * (1 - c) + other_cost.
Assumptions: Attempts share fixed expected cost and acceptance. Repetition continues until acceptance; pricing comparisons hold other inputs fixed.
Excludes: Correlated retries, a finite retry budget, queueing, and an estimate of how spending changes acceptance. A derived break-even probability can exceed one and therefore be unattainable.
References
Derivation: The geometric expected attempt count is 1 / acceptance. Multiplying by attempt cost gives effective cost; equalizing two effective costs gives their break-even acceptance.
Examples
>>> from mlsysim import Q_
>>> from mlsysim.solvers import AcceptedTaskCostModel
>>> result = AcceptedTaskCostModel().solve(Q_(1, "USD"), Q_(1, "USD"), 0.5)
>>> result.cost_per_accepted > result.attempt_cost
TrueAppendOnlyContextModel
engine.solvers.agents.AppendOnlyContextModel()Context read by the last call, and input resent in total, by an append-only trajectory.
Each turn appends its output and observation to the prompt and resends the whole history, so the last of H calls reads s0 + delta (H - 1) tokens while the run sends H s0 + delta H (H - 1) / 2: the window can hold the history while the resend grows quadratically with the horizon.
Methods
| Name | Description |
|---|---|
| solve | Account for history retained and resent across a task. |
solve
engine.solvers.agents.AppendOnlyContextModel.solve(
initial_tokens,
tokens_per_turn,
turns,
context_window,
)Account for history retained and resent across a task.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| initial_tokens | Quantity | Nonnegative first-prompt size in tokens; converted to an integer count. | required |
| tokens_per_turn | Quantity | Nonnegative appended size per turn in tokens; converted to an integer count. | required |
| turns | int | Positive number of calls; the first uses only initial_tokens. | required |
| context_window | Quantity | Positive context capacity in tokens, used for a reported occupancy ratio. | required |
Returns
| Name | Type | Description |
|---|---|---|
| AppendOnlyContextResult | Last-request and cumulative input sizes in tokens, a dimensionless window fraction, and a cumulative-to-last-request ratio. |
Notes
Equation: For H calls, initial size S and increment D, the last request is S + D*(H-1) and cumulative input is H*S + D*H*(H-1)/2.
Assumptions: Every new call includes the full previous history; token counts are fixed. The last request must be nonempty for the reread ratio.
Excludes: Prefix-cache billing, truncation, summarization, tokenizer boundary effects, and enforcement of the context capacity.
References
Derivation: Summing the arithmetic sequence S, S+D, …, S+(H-1)*D gives cumulative input. calc_append_only_context owns this identity.
Examples
>>> from mlsysim import Q_
>>> from mlsysim.solvers import AppendOnlyContextModel
>>> result = AppendOnlyContextModel().solve(Q_(10, "token"), Q_(2, "token"), 3, Q_(100, "token"))
>>> result.cumulative_input_tokens > result.last_call_tokens
TrueHorizonCompoundingModel
engine.solvers.agents.HorizonCompoundingModel()Unchecked task success over a horizon of independent steps.
Every step is right with probability p, nothing checks a step before the next builds on it, and nothing recovers, so a task of H turns succeeds with probability p^H. Holding target at H turns needs a per-step error of 1 - target^(1/H); for small error the product eps H approaches -ln(target) at any horizon. The first horizon whose success falls below target is floor(ln target / ln p) + 1.
Methods
| Name | Description |
|---|---|
| solve | Estimate unchecked success across independent task steps. |
solve
engine.solvers.agents.HorizonCompoundingModel.solve(
step_success,
horizons,
target_success,
target_horizon,
)Estimate unchecked success across independent task steps.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| step_success | float | Per-step success probability, dimensionless and in the open interval (0, 1). | required |
| horizons | Sequence[int] | Positive step counts at which to report task success. | required |
| target_success | float | Desired task success probability in the open interval (0, 1). | required |
| target_horizon | int | Positive number of steps over which the target must hold. | required |
Returns
| Name | Type | Description |
|---|---|---|
| HorizonCompoundingResult | Dimensionless success probabilities and error ratios, with the first integer horizon that falls below the target. |
Notes
Equation: For per-step success p and horizon H, success is p**H. The required per-step error is 1 - target_success**(1 / target_horizon).
Assumptions: Every step has the same independent success probability; failure of any unchecked step makes the whole task fail.
Excludes: Error correlation, recovery, checking, and a prediction of model accuracy.
References
Derivation: Multiplication of independent event probabilities gives p**H; solving that expression for p gives the required error. The implementation uses calc_unchecked_success and calc_step_error_for_target in physics.agents.
Examples
>>> from mlsysim.solvers import HorizonCompoundingModel
>>> result = HorizonCompoundingModel().solve(0.9, [1, 2], 0.8, 2)
>>> result.success[2] < result.success[1]
True