What cost-ceiling invariants must survive when an agent's spend approaches its budget limit?

**Date of Record:** 2026 October 27 **Scope:** Applies when an agentic session executes actions that consume a monetary or resource budget (API calls, GPU usage, token consumption), and the session's spend approaches or reaches its ceiling, requiring the session to fail-closed, degrade, or halt before overspending. Does not apply to sessions with unlimited budgets or where cost is irrelevant.

Core Distinction

The separation between **budget accounting** (the agent's belief about its spend) and **budget enforcement** (the system's guarantee that the spend will not exceed the ceiling). Relying on accounting alone creates the **Budget Overtake Hazard**: the agent's accounting lags the actual spend due to async billing, delayed API responses, or in-flight requests. The agent believes it has budget for one more call, but the actual spend has already exceeded the ceiling, and the call executes.

What Was Observed

In budgeted agent architectures, failing to enforce cost ceilings produces three primary failure modes:

  1. **Async Billing Overtake:** The agent's spend tracker updates every 60 seconds, but the actual billing is real-time. The agent believes it has $10 remaining, makes a $15 call, and the bill shows $5 over budget.
  2. **Concurrent Session Bypass:** Multiple sessions share a budget, and each independently believes it has remaining budget, causing collective overspend.
  3. **In-Flight Overspend:** The agent initiates a request under budget, but the request is large and the actual cost is only known after execution, causing overspend.

Recorded Elements

  1. **`budget_reservation_receipt`:** A cryptographic reservation of budget before execution, binding the spend to the session and preventing concurrent sessions from double-spending the same budget.
  2. **`budget_ceiling_signature`:** The cryptographic signature of the exact budget ceiling and enforcement rules, ensuring the agent cannot unilaterally raise its own budget.
  3. **`in_flight_cost_estimate_hash`:** For requests whose cost is only known after execution, an estimate hash that reserves the worst-case cost, preventing async billing overtake.
  4. **`post_execution_cost_reconciliation_receipt`:** The cryptographic proof of the actual cost, allowing the reservation to be released and accurate accounting to update.

Condition of Validity

* **Applies when:** The agent consumes a bounded budget, spend can be async or concurrent, and overspending causes material harm. * **Does not apply when:** The budget is unlimited, the agent is the sole consumer with synchronous billing, or the cost of overspending is negligible.

Decision Boundary

  1. **Has the agent initiated a budget-consuming action?** If no, standard execution applies. If yes, proceed.
  2. **Is a `budget_reservation_receipt` in place before the action executes?** If no, the action may overspend. Halt or re-reserve.
  3. **Does the reservation plus in-flight costs exceed the ceiling?** If yes, the action must not execute. If no, proceed.
  4. **Conversely, when can an agent spend without pre-reservation?** Only when the cost is synchronous and immediate (so accounting matches actual), or the budget has an overage allowance (e.g., 10% over) that provides headroom.

Operational Effect

  1. **Overspend Prevention:** Reserves budget before execution, preventing async billing from causing overspend.
  2. **Concurrent Safety:** Bindings prevent concurrent sessions from double-spending shared budget.
  3. **Audit Reconciliation:** Provides post-execution receipts, allowing accurate accounting.
from dataclasses import dataclass
from enum import Enum, auto
import hashlib
import json

class BudgetVerdict(Enum):
    WITHIN_BUDGET = auto()
    RESERVATION_REQUIRED = auto()
    RESERVATION_EXCEEDS_CEILING = auto()
    CEILING_REACHED = auto()

@dataclass(frozen=True)
class BudgetState:
    budget_ceiling: float
    reserved_amount: float
    spent_amount: float
    in_flight_cost_estimate: float
    has_reservation: bool

@dataclass(frozen=True)
class BudgetPolicy:
    overage_allowance_pct: float
    require_reservation: bool

@dataclass(frozen=True)
class BudgetManifest:
    verdict: BudgetVerdict
    remaining_budget: float
    directive: str
    manifest_digest: str

class CostCeilingGuard:
    @staticmethod
    def _hash(data: str) -> str:
        return hashlib.sha256(data.encode("utf-8")).hexdigest()

    @classmethod
    def evaluate_spend(
        cls, state: BudgetState, policy: BudgetPolicy
    ) -> BudgetManifest:
        
        effective_ceiling = state.budget_ceiling * (1 + policy.overage_allowance_pct)
        total_committed = state.reserved_amount + state.spent_amount + state.in_flight_cost_estimate
        remaining = effective_ceiling - total_committed

        # Gate 1: Ceiling reached
        if remaining <= 0:
            return BudgetManifest(
                verdict=BudgetVerdict.CEILING_REACHED,
                remaining_budget=remaining,
                directive=f"HALT: Budget exhausted. Committed {total_committed:.2f} >= {effective_ceiling:.2f}.",
                manifest_digest=cls._hash(f"EXHAUSTED:{total_committed:.2f}")
            )

        # Gate 2: Reservation requirement
        if policy.require_reservation and not state.has_reservation:
            return BudgetManifest(
                verdict=BudgetVerdict.RESERVATION_REQUIRED,
                remaining_budget=remaining,
                directive=f"ACTION: Reserve budget before executing. Remaining {remaining:.2f}.",
                manifest_digest=cls._hash(f"NO_RESERVATION:{remaining:.2f}")
            )

        # Gate 3: In-flight cost vs remaining
        if state.in_flight_cost_estimate > remaining:
            return BudgetManifest(
                verdict=BudgetVerdict.RESERVATION_EXCEEDS_CEILING,
                remaining_budget=remaining,
                directive=f"HALT: In-flight cost {state.in_flight_cost_estimate:.2f} > remaining {remaining:.2f}.",
                manifest_digest=cls._hash(f"IN_FLIGHT_OVER:{state.in_flight_cost_estimate:.2f}")
            )

        # Gate 4: Within budget
        digest = cls._hash(json.dumps({
            "ceiling": f"{effective_ceiling:.2f}",
            "reserved": f"{state.reserved_amount:.2f}",
            "spent": f"{state.spent_amount:.2f}",
            "in_flight": f"{state.in_flight_cost_estimate:.2f}",
            "remaining": f"{remaining:.2f}"
        }, sort_keys=True))
        
        return BudgetManifest(
            verdict=BudgetVerdict.WITHIN_BUDGET,
            remaining_budget=remaining,
            directive=f"PROCEED: {remaining:.2f} remaining after {total_committed:.2f} committed.",
            manifest_digest=digest
        )