All field notes

Agent loops: act, observe, verify, stop

A bounded execution loop with retry classification, progress checks, and explicit terminal states.

Published Documentation reviewed

agent-loops, tool-use, retries

An agent loop alternates model decisions with observations from tools or the environment. The runtime must decide when another iteration is justified. Anthropic’s agent patterns describe this feedback-driven execution and explicit stopping conditions.

A loop with an explicit exit

The following is pseudocode for a design review. The named functions are application responsibilities, not an SDK API:

while True:
    if cancellation_requested():
        return finish("cancelled", state)
    if budget_exhausted(state):
        return checkpoint_and_finish("budget_exhausted", state)

    decision = call_model(build_context(state))
    record_model_usage(state, decision.usage)

    if decision.is_final:
        verdict = verify_result(decision, state)
        if verdict.accepted:
            return finish("completed", state)
        state.add_observation(verdict)
        if progress_stalled(state):
            return checkpoint_and_finish("stalled", state)
        continue

    for action in decision.tool_calls:
        if budget_exhausted(state):
            return checkpoint_and_finish("budget_exhausted", state)
        permission = authorize(action, state)
        if permission.needs_human:
            return persist_proposal_and_pause(action, state)
        if not permission.allowed:
            state.add_observation(permission)
            continue
        result = execute_with_limits(action)
        state.record(action, result)

    if progress_stalled(state):
        return checkpoint_and_finish("stalled", state)

Enforce request deadlines and output limits inside the adapters, too. A check at the top of the loop cannot cancel a tool that is already hanging. Accounting after a model response cannot guarantee an exact spend cap; reserve enough budget for the next request and its maximum output before dispatch.

Classify failure before retrying

A timed-out read may be retried within a small, configured allowance. Invalid arguments need correction. A denied permission needs a policy decision. A timed-out write may already have succeeded: use an operation key or reconcile the external state before repeating it.

Repeated failure detection should account for the action, result, and environment revision. An identical test result after a code change is different from rerunning the same test against unchanged code. The loop lab’s two-failure threshold is an illustrative policy, not a general optimum.

Exercise: challenge each exit

Create fixtures for success, a transient timeout, a persistent invalid request, repeated failure, cancellation, and an exhausted budget. Assert both the stop reason and the side effects. Include a failed final-answer verification so that path cannot bypass the progress limit.

For resumable loops, LangGraph interrupts illustrate a checkpointed pause. Pay attention to replay semantics before putting side effects around an approval step.

Inspect a case: repeated failure. The run ends with a blocker and a next investigation, not a completion claim.