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: Foundation · Understand the concepts. 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.

Foundation · Understand the concepts

Understand the principle · Proposal, stopping and business completion

Objectives of this level: Derive the next state from recorded facts, and distinguish model turns, accepted tool calls and business effects before designing budgets.

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.

Work from a fixed input

The fictional after-sales task is main-return: user Alice at tenant acme, order o-100, quantity 2, ordinary item delivered 8 days ago. The current rule allows an unused ordinary item to enter the return-application process within 30 days. The task contract is: read the authorized order and current policy, save a quantity-1 proposal, obtain human approval, then confirm one merchant application receipt. Creating an application does not mean issuing a refund.

Reason from these four observations, in order:

  1. One model response proposes get_order; the accepted read returns the order. State is queued, turn_count=1, tool_count=1, with no receipt. The model phase is unfinished.
  2. After retrieval and proposal, a final structured answer says decision="eligible". State is awaiting_approval, turn_count=4, tool_count=3, with no receipt. Eligibility and a proposal do not establish an executed application.
  3. A write response is lost. The task stores outcome_unknown and merchant_transport_unknown. The transport failure does not establish whether the merchant committed.
  4. Looking up the same operation_key returns a receipt bound to this task and proposal. State can become completed; independent merchant audit must show one application for this task.

Why the runtime owns the transition

The model proposes calls and text; the runtime checks tool names, parameters, authorization, budgets and evidence, and persists facts. It cannot infer human approval from model prose. A terminal model response stops the model phase; the business state follows the receipt contract. A cancellation stops future dispatch but cannot erase an already committed application. The project therefore also uses completed_after_cancel when cancellation discovers a committed receipt.

Derive the budget before dispatch

A separate batch example has MAX_TOOLS=6, four accepted tools already recorded and five new candidate calls. Remaining capacity is two. This project preflights the whole batch, so it rejects all five with tool_budget_exhausted; zero new tools run. Executing two is a different contract and would require explicit unexecuted-call results.

The project persists MAX_TURNS=8, MAX_TOOLS=6 and a 64,000-byte serialized-request guard. turn_count increments after a response is classified and accepted; tool_count increments when a tool result is committed. They are not all HTTP attempts or provider tokens. A failed provider call, a rejected batch or a business execution request cannot be accounted for by these two counters alone. Bounds on failed attempts, elapsed task time, cost and parallel work require additional design; do not claim this demo implements them.

Read the run-loop reference as a framework concept; verify the actual project behavior in Agent.run, Agent.tool and Agent.execute.

code example

A changed validator for a research task

For a research task, predefine source coverage, readable citations and how unresolved claims are reported. Completion can mean a reviewed evidence bundle rather than a merchant receipt. The validator changes with the contract; reaching a search-count limit alone does not establish coverage.

Acceptance task for this level

For the four fixed observations, write a state-transition note: current fact, permitted next operation, forbidden inference, and evidence required to call the application completed. Separately calculate the five-call batch outcome.

Acceptance self-checks

Reading this browser’s record…

Hide the answer and check your understanding

The model has stopped and the proposal is eligible, but the receipt is null. Is the business task completed?

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.