Design a Bounded Agent Loop
Turn a goal into an observable plan-act-check loop with explicit state, budgets, approvals, and stopping conditions.
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.
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.
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]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.
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.
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_EXHAUSTEDStop 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
- Choose a multi-step task and write its success contract, allowed actions, trusted observations, and approval boundaries.
- Define the loop state and the deterministic controller that selects complete, continue, escalate, or stop.
- Simulate a normal completion, three identical no-progress transitions, a budget exhaustion, and an untrusted observation containing an instruction.
- 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
- Building effective agentsAnthropic · verified 2026-07-25
- Artificial Intelligence Risk Management FrameworkNIST · verified 2026-07-23
Knowledge check
Make it stick.
Choose the strongest answer for each question. Your attempts become part of your device-local transcript.