advanced34 min studyLesson 70 in Engineering

Agent orchestration and resource budgets

Model a bounded agent as a state machine with explicit observations, permissions, verification, retries, and token-cost limits.

On this page

What you’ll understand

  • Distinguish a fixed workflow from a model-selected sequence of actions.
  • Calculate worst-case cost and the effect of repeated attempts under stated assumptions.
  • Design termination, checkpointing, and verification boundaries for multi-step tasks.

An agent system repeatedly observes a state, proposes an action, receives a result, and decides what to do next. Workflows fix more transitions in code; agents delegate more selection to models. Neither arrangement removes the need for explicit state and limits. Ask which decisions need model judgment, which can be deterministic, and what external evidence proves task completion.

Make the action loop inspectable

Represent the task with durable state: objective, accepted constraints, completed operations, unresolved questions, resource use, and authoritative artifact identifiers. Tool results are observations, not proof of success. ReAct explores interleaving reasoning and actions, but the application still owns capabilities and execution. A fixed retrieval-and-formatting workflow can be easier to test when its steps are known.

Each transition needs a precondition, allowed action set, validation, and a recorded outcome. Keep external side effects idempotent or reconciled through an operation ledger. If a process restarts, reload committed state rather than reconstructing it solely from a conversation summary. Parallel workers can accelerate independent tasks, but shared mutable state creates conflicts and duplicate actions. Assign ownership and merge rules, and verify child results before allowing their claims to determine a final answer.

st+1=F(st,at,ot+1),at∈A(st),t≤Ks_{t+1}=F(s_t,a_t,o_{t+1}),\qquad a_t\in A(s_t),\qquad t\leq K
State updates combine the prior state, authorized action, and observed result. A(s) is the application-permitted action set and K a hard step cap; the notation does not assume the observation is true or the action successful.↔ Scroll wide equations horizontally.

Budget the entire trajectory

A single model call has input and output tokens, while a trajectory adds tool charges, retries, growing context, and worker calls. Set hard limits for tokens, money, elapsed time, actions, and concurrent work. Reserve budget for verification and a useful final report instead of spending everything on exploration. A step cap is insufficient if one operation has unbounded output or duration. Bound each operation as well as the loop.

The expected cost of a retry policy depends on how often it continues and how much each attempt costs. Independence is often a poor assumption: the same misunderstood requirement can cause every attempt to fail. Repeated-sampling formulas require stated assumptions, and verifiers can themselves be wrong. Distinguish pass-at-k, which asks whether any attempt succeeded, from a deployed selector's ability to identify that attempt. More generations do not help if the system cannot recognize correct work.

C=∑t=1K(rinIt+routOt106+Ctool,t),P(at least one success)=1−(1−p)kC=\sum_{t=1}^{K}\left(\frac{r_{\mathrm{in}}I_t+r_{\mathrm{out}}O_t}{10^6}+C_{\mathrm{tool},t}\right),\qquad P(\text{at least one success})=1-(1-p)^k
The cost uses per-million token rates and explicitly counted calls. The success expression assumes k independent attempts with equal success probability p; correlated failures or an imperfect selector invalidate using it as a deployed success guarantee.↔ Scroll wide equations horizontally.

Terminate on verified outcomes or explicit limits

Define success through an external criterion: a test passes, a requested artifact exists and meets its contract, or an authoritative service confirms the operation. The model saying done is not that criterion. Define failure and partial completion too. If a deadline expires, preserve completed work and report remaining tasks with evidence. A precise partial outcome is useful when a required dependency remains unavailable.

Evaluate full trajectories on representative multi-step tasks, including tool outages, ambiguous instructions, and policy constraints. Tau-bench studies tool-agent-user interactions and motivates examining reliable task completion across repeated trials. Its domain-specific scores are not a guarantee of general agent capability. Review traces for needless loops, stale observations, unauthorized proposals, and missing verification. Add autonomy where measured benefits justify its cost, with narrow, auditable boundaries.

Set a worst-case task cost

Use fictional planning rates of 2 currency units per million input tokens and 8 per million output tokens. Each round is capped at 1,200 input and 400 output tokens plus a 0.01-unit tool call. A task permits at most four rounds; assume every round reaches its cap.

  1. One round's input cost is 1,200×2/1,000,000=0.0024 units. Its output cost is 400×8/1,000,000=0.0032 units.

  2. Add the tool cost: 0.0024+0.0032+0.01=0.0156 units per round. Four rounds cost at most 4×0.0156=0.0624 units under these stated caps.

  3. At 100,000 tasks per day, the capped trajectory spend is 6,240 units per day. This excludes fixed infrastructure and any separately budgeted evaluator. If the context grows beyond 1,200 tokens, the original bound no longer holds.

The result

A useful cap constrains actual serialized input and every paid call, including child workers. The rates are invented for arithmetic practice, not current provider prices.

Explore the model

Interactive lab

Interactive lab · queue

Retries consume shared capacity

Vary arrival λ and service μ in the fluid backlog max(0, 4+(λ−μ)t). Interpret arrival as total admitted work, including retries. The separate M/M/1 readout requires stable Poisson/exponential single-server assumptions; this lab does not simulate agents, branching workflows, or retry timing.

Retries consume shared capacityFluid backlog starts at 4 and follows max(0, 4 + (λ − μ)t). The separate M/M/1 residence-time formula assumes Poisson arrivals, exponential service and λ < μ. Current readings: Fluid backlog: 4; Net rate · λ − μ: −1 /s; M/M/1 mean residence: 1 s. All parameters are adjustable with the labeled controls below.02.557.51001.534.56Elapsed time · sFluid backlogλ < μ
Fluid backlogCurrent time
Fluid backlog
4
Net rate · λ − μ
−1 /s
M/M/1 mean residence
1 s
t = 0 s

Fluid backlog starts at 4 and follows max(0, 4 + (λ − μ)t). The separate M/M/1 residence-time formula assumes Poisson arrivals, exponential service and λ < μ. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.

Look forRepeated attempts are additional load, so a retry policy must fit both a per-task budget and the system's capacity.

Check your understanding

Three independent attempts each succeed with probability 0.5. What does 1−(1−0.5)³=0.875 establish?

Try it yourself

A task allows three rounds, each capped at 2,000 input and 500 output tokens, at the fictional rates 2 and 8 units per million respectively. Tools cost 0.02 units per round. Compute the maximum modeled task cost and name a condition that breaks the bound.

Take a moment to make a prediction or work it out on paper. Open a hint when you need one.

Give me a hint

Compute one complete round, then multiply by the maximum round count; include tool charges.

Compare with the solution

Input costs 0.004 units and output costs 0.004, so each round totals 0.028 with tools. Three rounds cost at most 0.084 units. An uncounted child model call, larger serialized context, an additional paid evaluator, or more tool calls than capped would break this accounting. Reserve those operations explicitly rather than assuming they are free.

Take it with you

  • A bounded agent is an explicit state machine with application-owned action permissions.
  • Budget all serialized tokens, tools, retries, workers, and verification.
  • Finish on external evidence of success or report a precise partial result at the limit.

Read further

Original sources and deeper explanations to follow at your own pace.

Optional progress, saved in this browser when available. Revisit any lesson at any time.

Lesson not marked complete.