Skip to content
Agent Field Notes
Course contentsExpand a topic to choose a lesson.

SYSTEMATIC LEARNING / FOUR-LEVEL COURSE

Control and completion criteria in agent loops

Starting from the tool invocation loop, design termination conditions, budgets, cancellations, and verifiable completion statuses.

Learning objectives: Model output proposes the next step; the runtime owns execution and terminal state. Stopping only establishes that the loop ended. Success additionally requires evidence that the agreed business conditions hold. Budget exhaustion, cancellation, and unknown outcomes therefore need distinct labels rather than one generic 'completed' state.

Content checked: 2026-10-03 · Each level has independent explanations, tasks and inspections

Choose a starting point based on your familiarity with this topic. Current level: Design · Explain the trade-offs. After completing the task, continue to the next level. Reading and self-checks alone do not establish mastery.

On this level

Review the prerequisites

Suitable for: Backend developers learning Agent applications.

Control and completion criteria in agent loops
Model output proposes the next step; the runtime owns execution and terminal state. Stopping only establishes that the loop ended. Success additionally requires evidence that the agreed business conditions hold. Budget exhaustion, cancellation, and unknown outcomes therefore need distinct labels rather than one generic 'completed' state.

How does the mechanism work?

Step-by-step explanation
  1. One model round generates five tool calls, how to budget?

    One round can contain multiple actions, so account for the abstract loop in the resource ledger.

  2. When the user cancels, the remote write operation has been successful. How do you end it?

    Cancellation signals affect execution; business effects that already occurred require separate follow-through.

  3. Is a correct answer successful if the model did not run the required tests?

    The terminal state must be determined by acceptance conditions, rather than how credible the text sounds.

Design · Explain the trade-offs

Design tradeoffs · From one worker to three parallel branches

Objectives of this level: Change the execution constraints and specify unimplemented controls, while preserving evidence-based business states.

Visual guide · Model stop and business finish

Model stop and business finish

Only a bound merchant receipt completes the application. Approval, a final model answer and a timeout each establish different facts. Select the figure to open it at full size.

Change the constraints

The existing project executes tool calls sequentially, uses persisted leases/fences and separates proposal, approval and merchant execution. It has no general parallel scheduler, cumulative failed-attempt budget, billed-cost ceiling or absolute task deadline. Its serialized-request byte guard is not a tokenizer. Design a future variant with three concurrent branches, a shared limit of six started tool attempts (including failures), at most eight started model requests, a 90-second active-work budget and a human-review window of 24 hours. These numbers are exercise inputs, not measured production limits.

Derive the decisions

  1. Three workers seeing a remaining quota of two cannot each dispatch two calls. Reserve shared attempt quota before dispatch, record the reservation owner and settle the result. Specify what happens after a crash between reservation and actual request; uncertain attempts cannot simply refund quota.
  2. Model requests that fail before returning still consume the started-attempt budget. The current turn_count does not count them. Distinguish started, failed and committed response records rather than redefining an existing counter silently.
  3. Define active work separately from 24-hour human waiting. The next execution after approval still revalidates identity, proposal and policy. An expired lease or deadline blocks new work but does not invalidate a committed merchant receipt.
  4. For a repeated normalized action and unchanged failure, decide whether to stop after two repetitions or gather new diagnostic evidence. Balance legitimate repeated polling against pointless retries. Unknown side effects require same-key receipt reconciliation, not an unbounded new write.
  5. Record explicit outcomes for unstarted calls, partial branches and unknown external operations. A partial answer must not be promoted to completed application state.

Deliver a design note, not a claim that these features exist. For each item list the trusted state, atomic update, failure window and test that would reject an unsafe implementation. Prefer a constrained deterministic workflow when the approved action and order are already known; an extra model decision cannot supply missing authorization.

code example

Scheduled tasks need a different completion artifact

For a daily report, distinguish a new dated output from retrying yesterday’s delivery. A snapshot/versioned report can establish completion; repeating a delivery needs its stable action identity. Neither schedule frequency nor the model saying “sent” proves the external delivery effect.

Acceptance task for this level

Design the three-branch variant under the six-attempt / eight-request / 90-second / 24-hour constraints. Include one quota race timeline, one model-failure timeline and one lost-write-response timeline; identify the required changes beyond current Agent.run.

Acceptance self-checks

Reading this browser’s record…

Hide the answer and check your understanding

Can we meet the changed requirement by setting MAX_TOOLS=6 and MAX_TURNS=8 in the current code?

Further explanations and practice

When encountering unfamiliar principles, first read the implementation, continuous questioning and migration cases, and then independently explain the premise and boundaries. Answers and notes are saved to the original account record.

All linked explanations and exercises (1 )

Sources and verification scope

The principles are based on public information; the numbers, cases and tasks are the teaching design of this website. Offline experiments verify the range noted on this page, and the learning effect still needs to be judged through independent tasks and feedback.