Skip to main content

engine.solvers.harness

engine.solvers.harness

Agent-harness solvers: what the runtime holds, shares, and admits (added 2026-10-20).

Domain implementations behind mlsysim.solvers. Each model is arithmetic on stated inputs: approval escrow sized by Little’s law, weighted deficit round-robin between request classes, bounded-liability admission of the next model call, and one trajectory metered against its budget vector. None models a particular serving engine, provider quota, or queueing burst.

Classes

Name Description
ApprovalEscrowModel What approval escrow holds for a long-running workload and an urgent one.
BoundedLiabilityAdmissionModel Bounded-liability admission of the next model call, against a check of settled spend.
DeficitRoundRobinModel Weighted deficit round-robin between backlogged request classes.
TrajectoryBudgetMeterModel One append-only trajectory metered against a budget vector of turns, tokens, time, and dollars.

ApprovalEscrowModel

engine.solvers.harness.ApprovalEscrowModel()

What approval escrow holds for a long-running workload and an urgent one.

Little’s law gives the trajectories waiting for a decision, L = lambda W. For the repair workload each keeps its workspace lease, so escrow binds on retained memory; for the remediation workload fewer than one waits, and the decision binds on time to mitigate, T = diagnose + decide + apply. Means only: bursts and batch review hold more for as long as they last.

Methods

Name Description
solve Estimate retained workspaces and remediation delay during approval.
solve
engine.solvers.harness.ApprovalEscrowModel.solve(
    repair_rate,
    repair_wait,
    lease,
    remediation_rate,
    diagnosis,
    remediation_wait,
    apply_verify,
)

Estimate retained workspaces and remediation delay during approval.

Parameters
Name Type Description Default
repair_rate Quantity Nonnegative repair arrivals per unit time. required
repair_wait Quantity Mean nonnegative approval waiting time per repair. required
lease Quantity Memory retained by each waiting repair, convertible to GiB. required
remediation_rate Quantity Nonnegative remediation arrivals per unit time. required
diagnosis Quantity Diagnosis duration, convertible to minutes. required
remediation_wait Quantity Mean remediation approval wait, convertible to minutes. required
apply_verify Quantity Application and verification duration, convertible to minutes. required
Returns
Name Type Description
ApprovalEscrowResult Mean counts held, retained memory in GiB, mitigation time in minutes, and dimensionless approval share of mitigation time.
Notes

Equation: held = repair_rate * repair_wait; retained = held * lease. Mitigation time is diagnosis + remediation_wait + apply_verify. Assumptions: stationary mean arrivals and waits, one lease per repair, and positive total mitigation duration. Excludes: bursts, batching of reviews, lease sharing, and queue transients.

References

Derivation: Little’s occupancy identity multiplies arrival rate by mean residence time. Retained memory multiplies occupancy by memory per resident. The mitigation phases are disjoint durations and therefore add.

Examples
>>> from mlsysim import Q_
>>> result = ApprovalEscrowModel().solve(Q_(2, '1/hour'), Q_(1, 'hour'),
...     Q_(3, 'GiB'), Q_(1, '1/hour'), Q_(1, 'minute'),
...     Q_(2, 'minute'), Q_(1, 'minute'))
>>> result.retained_memory.m_as('GiB') == 6
True

BoundedLiabilityAdmissionModel

engine.solvers.harness.BoundedLiabilityAdmissionModel()

Bounded-liability admission of the next model call, against a check of settled spend.

A spend check admits when the call’s liability fits allowance - settled. The reservation test admits only when settled spend, outstanding reservations, the call’s liability, and the protected finalization reserve together fit the allowance. Liability prices every input token at the undiscounted rate and the whole output allowance (calc_bounded_liability). Dollars throughout.

Methods

Name Description
solve Reserve the next call’s maximum token charge before admitting it.
solve
engine.solvers.harness.BoundedLiabilityAdmissionModel.solve(
    allowance,
    settled,
    reserved,
    finalization_reserve,
    input_tokens,
    output_allowance,
    input_price,
    output_price,
    probe,
)

Reserve the next call’s maximum token charge before admitting it.

Parameters
Name Type Description Default
allowance Quantity Positive total spending limit in USD. required
settled Quantity Nonnegative already billed spending in USD. required
reserved Quantity Nonnegative outstanding reservations in USD. required
finalization_reserve Quantity Nonnegative protected closing budget in USD. required
input_tokens int Nonnegative full prompt token count. required
output_allowance int Nonnegative maximum generated token count. required
input_price Quantity Positive uncached input price in USD/token. required
output_price Quantity Nonnegative output price in USD/token; total call liability must be positive. required
probe Quantity Nonnegative comparison spending amount in USD. required
Returns
Name Type Description
BoundedLiabilityAdmissionResult Dollar liabilities and headrooms as floats, admission booleans, token ceilings, and dimensionless spending shares.
Notes

Equation: liability = input_tokens * input_price + output_allowance * output_price. Headroom = allowance - settled - reserved - finalization_reserve. Admission requires liability <= headroom. Assumptions: prices are fixed, all input is undiscounted, and output cannot exceed its allowance. Negative headroom represents an exhausted budget. Excludes: tool fees, taxes, provider rounding, and reconciliation of actual bills.

References

Derivation: reserve each token’s maximum charge before spending. Subtract settled, outstanding, and protected amounts from the same budget once each.

Examples
>>> from mlsysim import Q_
>>> result = BoundedLiabilityAdmissionModel().solve(Q_(10, 'USD'),
...     Q_(2, 'USD'), Q_(3, 'USD'), Q_(1, 'USD'), 100, 100,
...     Q_(0.01, 'USD/token'), Q_(0.01, 'USD/token'), Q_(1, 'USD'))
>>> result.admitted and result.headroom == 4
True

DeficitRoundRobinModel

engine.solvers.harness.DeficitRoundRobinModel()

Weighted deficit round-robin between backlogged request classes.

Each round a class earns w q charge units and spends c per call (input tokens plus the full output allowance), carrying unspent credit forward. Weights fix the token-charge shares, w / sum(w); the call shares follow w q / c and so depend on call size. A direct simulation of rounds rounds checks the long-run formula.

Methods

Name Description
solve Compare token-charge shares and call shares under deficit round-robin.
solve
engine.solvers.harness.DeficitRoundRobinModel.solve(quantum, classes, rounds)

Compare token-charge shares and call shares under deficit round-robin.

Parameters
Name Type Description Default
quantum float Positive token-charge credit allocated per unit weight per round. required
classes Mapping[str, Mapping[str, float]] Backlogged classes, each with positive weight and nonnegative input and output token charges. Their sum must be positive. required
rounds int Positive number of simulated rounds, sufficient to serve a call. required
Returns
Name Type Description
DeficitRoundRobinResult Credit and token charges, calls per round, rounds per call, dimensionless limiting shares, and finite-run simulated shares.
Notes

Equation: calls_per_round = weight * quantum / (input + output). Charge share is weight / sum(weights); call share normalizes calls_per_round. Assumptions: permanently backlogged classes, fixed per-call charge, and credit carried between rounds without a cap. Excludes: arrival processes, variable output length, latency, and GPU capacity.

References

Derivation: each round adds weight times quantum credit and each completed call consumes its input-plus-output charge. Dividing credit by charge gives the long-run call rate; the loop retains integer-call residual credit.

Examples
>>> result = DeficitRoundRobinModel().solve(10,
...     {'short': {'weight': 1, 'input': 5, 'output': 5},
...      'long': {'weight': 1, 'input': 10, 'output': 10}}, 4)
>>> result.call_ratio == 2
True

TrajectoryBudgetMeterModel

engine.solvers.harness.TrajectoryBudgetMeterModel()

One append-only trajectory metered against a budget vector of turns, tokens, time, and dollars.

With prefix reuse every turn’s earlier prompt is billed at the cached rate and only the newly appended tokens (and the whole first prompt) at the input rate; without reuse every input token is billed uncached. The token meter counts cached input in full, so only the dollar meter sees a cache miss. Each used fraction is used / allowance; the leading meter is the largest.

Methods

Name Description
solve Compare a trajectory’s token bill with and without prefix reuse.
solve
engine.solvers.harness.TrajectoryBudgetMeterModel.solve(
    turns,
    input_tokens,
    uncached_tokens,
    output_tokens,
    input_price,
    cached_price,
    output_price,
    allowances,
    elapsed,
)

Compare a trajectory’s token bill with and without prefix reuse.

Parameters
Name Type Description Default
turns int Nonnegative completed turn count. required
input_tokens int Nonnegative aggregate input tokens, including cached tokens. required
uncached_tokens int Uncached subset of input_tokens, between zero and input_tokens. required
output_tokens int Nonnegative aggregate generated tokens. required
input_price Quantity Nonnegative uncached price in USD/token. required
cached_price Quantity Nonnegative cached-input price in USD/token. required
output_price Quantity Nonnegative generation price in USD/token; total reused bill must be positive. required
allowances Mapping[str, float] Positive limits with keys turns, tokens, time, and dollars. Time uses the same unit as elapsed; dollars are USD. required
elapsed float Nonnegative elapsed time in the unit selected for allowances[‘time’]. required
Returns
Name Type Description
TrajectoryBudgetMeterResult Token counts, dollar bills as floats, dimensionless used fractions, and names of the largest used fractions for both billing policies.
Notes

Equation: reused cost = uncached_tokens * input_price + (input_tokens - uncached_tokens) * cached_price + output_tokens * output_price. Each budget meter is used / allowance; its leading name maximizes that ratio. Assumptions: the supplied aggregate tokens reflect an append-only trajectory and prices are constant for the accounting period. Cached tokens count in full against the token meter. Excludes: provider rounding, tool bills, burst quotas, and automatic admission.

References

Derivation: partition input tokens into disjoint cached and uncached sets, price each set once, and add generated-token spending. Normalize each resource against its own allowance before comparing exhaustion fractions.

Examples
>>> from mlsysim import Q_
>>> result = TrajectoryBudgetMeterModel().solve(2, 100, 50, 20,
...     Q_(0.02, 'USD/token'), Q_(0.01, 'USD/token'), Q_(0.01, 'USD/token'),
...     {'turns': 10, 'tokens': 1000, 'time': 100, 'dollars': 10}, 5)
>>> result.cached_tokens == 50 and result.cost_with_reuse < result.cost_without_reuse
True
Back to top