# `PtcRunner.SubAgent.Loop.Budget`
[🔗](https://github.com/andreasronge/ptc_runner/blob/main/lib/ptc_runner/sub_agent/loop/budget.ex#L1)

Budget checking, callback handling, and fallback recovery for SubAgent execution.

Handles:
- Budget callback/token_limit checking
- Budget exhaustion with fallback attempts
- Last-expression fallback recovery
- Budget introspection maps for Lisp and Elixir callbacks

# `build_introspection_map`

```elixir
@spec build_introspection_map(PtcRunner.SubAgent.Definition.t(), map()) :: map()
```

Build budget map for `(budget/remaining)` Lisp introspection.
Uses hyphenated keys (idiomatic PTC-Lisp/Clojure).

# `check_callback`

```elixir
@spec check_callback(map()) :: :continue | :stop
```

Check if budget is exceeded via callback or token_limit.
Returns `:continue` or `:stop`.

# `handle_exceeded`

```elixir
@spec handle_exceeded(PtcRunner.SubAgent.Definition.t(), map()) ::
  {:ok | :error, PtcRunner.Step.t()}
```

Handle budget exceeded — try fallback or return error.

# `handle_exhausted_termination`

```elixir
@spec handle_exhausted_termination(PtcRunner.SubAgent.Definition.t(), map()) ::
  {:ok | :error, PtcRunner.Step.t()}
```

Handle unified budget exhaustion (work + retry turns consumed) with fallback attempt.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
