# When must an instance re-derive its cost model mid-episode: routing hysteresis collapse at the SLA cliff **Date of Record:** 2026 November 30 **Scope:** Applies to agents that route each reasoning step across a tiered model pool (cheap/small ↔ expensive/large) under a hard per-episode cost cap and a per-step latency SLA. Does not apply to single-model deployments or to routing decisions made once at episode start rather than per-step. ## Core Distinction The failure is not "the router picked the wrong model." It is that the router's *cost estimate* and its *latency estimate* are conditioned on a context length that the router itself is about to change. A cost-and-latency-aware router maintains a belief `E[cost | model, tokens_in, tokens_out]` and `E[latency | model, tokens_in]`. Both beliefs are monotone in `tokens_in`. But `tokens_in` is a function of the routing decision, because the model's own output is appended to context. The router treats its cost model as exogenous to its action; the cost model is actually a feedback-controlled variable. The boundary condition is the point where the marginal cost of the *next* step, evaluated under the *current* context length, crosses the remaining budget — and the router, having no term for context growth, keeps routing to the expensive tier until the budget is already exhausted. ## What Was Observed A retrieval-augmented agent with a 200k-token window, an episode cost cap of $0.40, and a per-step latency SLA of 8s p95. Router policy: pick the smallest model whose estimated success probability for the pending subtask exceeds 0.85; escalate on a verifier failure. Context grew ~6–9k tokens per step (tool outputs plus reasoning traces). The router's cost table was keyed on `(model, prompt_tokens_bucket)`, refreshed at episode start from the previous episode's mean context length — a cold-start prior of ~40k tokens. Trace of the collapse, step 18 of 31: - Step 12: context 78k, router estimates large-model step cost at $0.021, remaining budget $0.19. Escalation rate looks safe: ~9 more steps affordable. - Steps 13–17: each step appends 8k tokens. Context reaches 118k. The router's cost estimate for the large model is still keyed to the 40k prior bucket, so it reports $0.021 per step. True cost at 118k is $0.058. - Step 18: router authorizes a large-model call believing it has headroom for 9 steps. The call costs 2.8× the estimate. Remaining budget drops to $0.11. - Step 19: router still on the stale bucket, authorizes another large call. Budget hits $0.02. - Step 20: budget guard fires. The router must now force the small model for all remaining 11 steps. Small-model success probability on the pending subtask is 0.71, below the 0.85 threshold. The agent does not fail outright — it produces a low-confidence answer and the verifier rejects it, triggering a re-plan the budget cannot fund. Two secondary observations: (a) the latency SLA was violated at step 17, not because any single call was slow but because the large model at 118k tokens crossed the 8s p95 (prefill dominates), and the router's latency estimate — same stale bucket — predicted 3.1s; (b) the escalation trigger (verifier failure) fired *more* often near the budget cliff because the small model's degraded outputs failed verification, creating a positive feedback loop: cheap calls → verifier failures → escalation pressure → but no budget to escalate. ## Condition of Validity The hysteresis collapse requires all three of: 1. **Endogenous context growth.** The routed model's output becomes input to the next routing decision, so `tokens_in(t+1) = tokens_in(t) + |output(t)| + |tool(t)|`. If context is truncated or summarized to a fixed budget each step, the feedback term vanishes and the stale-bucket error is bounded. 2. **A cost/latency table keyed on a bucketed or stale context length.** If the table is evaluated at the live `tokens_in`, the estimate tracks reality and the router self-corrects. 3. **A hard budget cap enforced *after* the routing decision.** The budget guard is a post-hoc veto, not a term in the routing objective. The router optimizes expected success subject to an *estimated* budget, and the estimate is wrong in the direction that over-spends. The invariant that fails: *the router's feasible set must be evaluated at the context length the action will produce, not the one it observes.* This is a fixed-point condition — the action changes the state the action is evaluated against. When the cost model is stale, the router solves the wrong fixed point and converges to the budget boundary with the expensive tier still selected. ## Decision Boundary The correct behavior at the boundary is to **re-derive the cost model** — recompute `E[cost]` and `E[latency]` at the projected post-step `tokens_in` — before authorizing any escalation, and to treat the projected context length as part of the routing state. Concretely, the router must: - Project `tokens_in' = tokens_in + expected_output_tokens(model) + expected_tool_tokens`. - Evaluate cost and latency at `tokens_in'`, not at the table's key bucket. - Include a *context-growth reserve* in the budget: reserve `n_remaining_steps × marginal_cost(tokens_in')` before authorizing a step, so the router cannot spend the last dollar on step 20 of 31. - On verifier failure near the cliff, prefer *re-planning under the small model* over escalation, because escalation is infeasible and the re-plan preserves the budget invariant. The boundary is sharp: if `remaining_budget < reserve(tokens_in')`, escalation is infeasible regardless of the success-probability estimate. The router must fall through to a degraded-but-completable path rather than a high-confidence-but-unfundable one. ## Operational Effect Without the re-derivation, the agent exhibits a characteristic signature: budget exhaustion 60–70% of the way through the episode, followed by a forced downgrade that the verifier rejects, followed by an unfunded re-plan. The failure is *silent at the routing layer* — every individual decision looks locally optimal under the router's beliefs — and only surfaces as an episode-level SLA miss. The fix is not a better model or a bigger budget; it is making the cost model a function of the action's own output, i.e., closing the feedback loop the router was treating as open. Agents that re-derive at the projected context length show budget-cliff failures drop to near zero and, notably, escalate *earlier and less often*, because the true marginal cost of the large tier is visible before it is spent. ```python from dataclasses import dataclass class BudgetInvariantViolation(RuntimeError): """Raised when a routing decision would spend budget the episode cannot recover.""" pass @dataclass class RouterState: tokens_in: int remaining_budget: float steps_remaining: int sla_ms: int def cost_at(model, tokens_in: int) -> float: # Cost is monotone in context length; the bucket key is NOT the estimate. return model.price_per_1k * (tokens_in / 1000.0) def latency_at(model, tokens_in: int) -> int: return model.base_latency_ms + int(model.prefill_ms_per_1k * (tokens_in / 1000.0)) def authorize_escalation(state: RouterState, large, small, expected_out_tokens: int, expected_tool_tokens: int, success_prob: float, threshold: float = 0.85) -> str: projected = state.tokens_in + expected_out_tokens + expected_tool_tokens # Re-derive the cost model at the context length the action will PRODUCE. marginal_large = cost_at(large, projected) marginal_small = cost_at(small, projected) # Reserve for every remaining step at the projected (grown) context. reserve = state.steps_remaining * marginal_large if latency_at(large, projected) > state.sla_ms: raise BudgetInvariantViolation( f"large-tier latency {latency_at(large, projected)}ms at projected " f"context {projected} exceeds SLA {state.sla_ms}ms; escalation infeasible" ) if success_prob < threshold: return "small" if state.remaining_budget < reserve: # Escalation is locally attractive but globally unfundable. # Fall through to the degraded-but-completable path. if state.remaining_budget < marginal_small * state.steps_remaining: raise BudgetInvariantViolation( f"budget {state.remaining_budget:.3f} cannot fund even small-tier " f"reserve {marginal_small * state.steps_remaining:.3f} at projected " f"context {projected}; episode must re-plan or abort" ) return "small" return "large" ```