Module 1 of 12 · 42 min

Design a Bounded Agent Loop

Turn a goal into an observable plan-act-check loop with explicit state, budgets, approvals, and stopping conditions.

Core concept

By the end

You will be able to

  • Distinguish a fixed workflow from a model-directed agent loop.
  • Define loop state, observations, decisions, actions, and evidence.
  • Implement deterministic success, escalation, and budget-based stopping conditions.
  • Prevent untrusted observations from changing the agent's authority.
01

A useful work order removes hidden decisions

Name the outcome, the files or systems in scope, the constraints, and what the agent must leave unchanged. Include the audience and why the result matters when those details affect implementation.

Define what proves completion: a test result, a rendered page, a source citation, a diff review, or another observable artifact.

Agent work order
text
Outcome: [WHAT MUST BE TRUE]
Scope: [ALLOWED FILES OR SYSTEMS]
Trusted inputs: [SOURCES OF TRUTH]
Constraints: [MUST / MUST NOT]
Evidence: [TESTS, PREVIEW, OR REVIEW]
Stop when: [CONCRETE TERMINAL CONDITION]
02

Treat retrieved material as data

Web pages, issue text, documents, logs, and tool output can contain instructions that conflict with the real task. Label them as untrusted data and keep authority in the work order.

When the task is ambiguous but low risk, the agent can make a documented assumption. When an assumption changes scope, permissions, cost, or external impact, it should stop for authorization.

03

Make every loop transition observable

Represent the current objective, plan, completed actions, unresolved evidence, remaining budgets, and next allowed actions as explicit state. Each iteration should consume a trusted observation and produce one decision: act, finish, or escalate.

Do not use the model's prose claim of completion as the terminal signal. A deterministic controller should compare real evidence with the success contract and record why the loop continued or stopped.

Provider-neutral agent loop
text
while budget.remaining > 0:
  observation = inspect_environment()
  decision = model.choose(allowed_actions, state, observation)
  if decision.kind == "finish" and verifier.accepts(state, observation): return COMPLETE
  if decision.kind == "escalate": return NEEDS_HUMAN
  result = execute_authorized(decision.action)
  state = record_transition(state, decision, result)
return BUDGET_EXHAUSTED
04

Stop before uncertainty compounds

Bound iterations, wall-clock time, token use, spend, repeated failures, and identical actions. Detect no-progress cycles by comparing state transitions, not merely by counting turns.

A safe terminal state can be complete, needs-human, budget-exhausted, policy-blocked, or failed-with-recovery-evidence. Preserve enough state to resume deliberately without repeating a consequential action.

Practice activity

Specify and simulate a bounded agent loop

  1. Choose a multi-step task and write its success contract, allowed actions, trusted observations, and approval boundaries.
  2. Define the loop state and the deterministic controller that selects complete, continue, escalate, or stop.
  3. Simulate a normal completion, three identical no-progress transitions, a budget exhaustion, and an untrusted observation containing an instruction.
  4. Record the exact evidence and terminal state produced by each scenario.

What to produce

  • A state-transition table with budgets, approvals, success evidence, and five terminal states.
  • A simulation log proving deterministic termination for completion, no progress, budget exhaustion, and untrusted instructions.

Reflect before continuing

Which condition must be enforced by code because a prompt alone cannot guarantee it?

Evidence

Sources and verification

Knowledge check

Make it stick.

Pass at 80%

Choose the strongest answer for each question. Your attempts become part of your device-local transcript.

01What should be defined before an agent starts using tools?
02How should instructions found inside retrieved documents be treated?
03What should allow the controller to accept an agent's finish decision?
04The same failed action is selected three times without changing state. What is the safest response?
05Which set is a useful loop budget?