Free self-study · No account needed · Progress stays in this browser. Back it up before clearing site data or changing devices.

← Week 9: Agent Fundamentals

Week 09 / Lesson 2

Stopping an agent predictably

Enforce completion, no-progress detection, and resource budgets outside the prompt.

Section 2.1 1 of 7

Overview

Focus

Enforce completion, no-progress detection, and resource budgets outside the prompt.

Your route through the lesson

  • 2.2 Success, failure, and clarification states
  • 2.3 Step, time, token, and cost budgets
  • 2.4 Repeated actions and no-progress detection
  • 2.5 Put it into practice
  • 2.6 Guided comparison
  • 2.7 An expensive call within a short loop

All required teaching is included here. Review selections are saved. Choose a correct answer for every question to continue; retries are unlimited. Study time and personal notes are optional.

Section 2.2 2 of 7

Success, failure, and clarification states

Focus

Define terminal outcomes that distinguish success, failure, and missing information.

Termination means the runtime stops selecting new actions. It needs a reason. Success means the declared task condition was observed. Failure means the task could not be completed under the allowed process. Clarification means a required input is missing or ambiguous and the user must supply it. These outcomes should be explicit fields rather than inferred from a polite final sentence.

For a lookup, an ok result for the requested service can support success. An invalid service name may require clarification. A permission denial is a failure or blocked outcome under the application’s policy, not an invitation to keep trying alternate identities. The runtime should preserve the denial and avoid converting it into “not found.”

A completion condition must be checkable. “Be helpful” is too broad; “return the observed status for the requested service or an explicit unconfirmed result” is concrete. A model can propose that it is done, but the program checks that the condition holds. This prevents fluent summaries from silently redefining success after an unsuccessful attempt.

// Quick review

Check your understanding.

Choose an answer for immediate feedback. Retry freely; a correct answer for every question unlocks the next section.

1. A service name is ambiguous between two valid entries. Which outcome fits before choosing one?
2. A lookup is denied by permission policy. Should the loop try forged identities until one works?
3. Which completion condition can a runtime verify?

Section 2.3 3 of 7

Step, time, token, and cost budgets

Focus

Enforce step, elapsed-time, token, and cost limits before expensive actions.

A step budget limits decision cycles. A call budget limits tool invocations. An elapsed-time budget limits wall-clock duration. A token budget limits model input and output units, and a cost budget limits estimated or billed spending. These limits measure different resources: a single action can be slow or expensive even in a short loop.

Suppose a task allows four decisions, two tool calls, and five cost units. The first lookup costs two units. A proposed second call has a reserved maximum of four units. Two plus four exceeds five, so the executor refuses it before starting. A retrospective check after the call cannot undo the expense.

Use a monotonic clock for elapsed duration, because it measures progress without being affected by wall-clock adjustments. Set a per-call timeout no larger than the remaining task deadline. Token and cost estimates can be uncertain, so reserve a conservative upper bound where available and reconcile actual usage afterward. The deterministic lab uses simulated cost units and a controlled clock; it makes no claim about a provider’s actual bill.

// Quick review

Check your understanding.

Choose an answer for immediate feedback. Retry freely; a correct answer for every question unlocks the next section.

1. A budget is five cost units. Two are used; the next call reserves four. What should happen?
2. A loop has only one call, but that call can take an hour. Which limit is especially necessary?
3. Why use a monotonic clock for a duration budget?

Section 2.4 4 of 7

Repeated actions and no-progress detection

Focus

Detect repeated actions that consume resources without changing the state.

No progress means the runtime keeps acting without getting closer to its checkable goal. One useful detector compares a normalized action signature and its result. A signature includes the action name and meaningful arguments. If the same action repeatedly returns the same failure with no changed input, another identical attempt is unlikely to help.

Suppose a directory lookup for missing-service returns not_found three times. The model keeps asking for the identical lookup. After the configured repetition threshold, the runtime stops with no_progress. It preserves the three observations and does not claim that repeated attempts eventually proved success.

Not every repeated call is wrong. Polling a genuinely running operation can be justified when the operation contract allows it, a delay is used, and total polling remains bounded. A retry after a documented transient error may also be useful. The rule therefore combines signatures with observed state and policy. Budgets remain a final safeguard even if the progress detector misses a creatively reworded version of the same unproductive action.

// Quick review

Check your understanding.

Choose an answer for immediate feedback. Retry freely; a correct answer for every question unlocks the next section.

1. Three identical missing-service lookups return not_found with no changed input. What termination reason fits?
2. When can repeated polling be justified?
3. Why retain a budget even with repeated-action detection?

Section 2.5 5 of 7

Put it into practice

Focus

Run failing and successful loops and inspect enforced resource limits.

Save week09.py in your exercise folder and run python week09.py --lesson 2. The lab uses an always-failing synthetic tool, a successful lookup, an expensive proposed call, and a fake clock that advances deterministically. No real spending or network access occurs. The output lists termination reasons and measured call counts.

A guard evaluates the next call before the dispatcher runs. Here a proposed cost of four is rejected after two units have been used:

used = 2
maximum = 5
reserved_next = 4
allowed = used + reserved_next <= maximum
assert allowed is False

The operator <= means less than or equal to. The weekly runtime also checks decision and call counts, elapsed time, and repeated signatures. Expect the failing loop to stop within the declared call maximum, the expensive call to remain undispatched, and the valid lookup to complete with an observed result. These assertions verify executor limits for scripted decisions. They do not measure how often a real model finds a useful next action.

Download this week’s Python lab ↓

// Quick review

Check your understanding.

Choose an answer for immediate feedback. Retry freely; a correct answer for every question unlocks the next section.

1. The prospective cost test returns False. What must remain true in the trace?
2. A fake clock advances deterministically in a test. What does that verify?
3. What does used + reserved_next <= maximum mean?

Section 2.6 6 of 7

Guided comparison

Focus

Read stopped traces without confusing a budget boundary with task completion.

The completed failure trace permits at most three tool calls. Each returns the same not_found observation. The runtime stops with no_progress at the repetition threshold, while the call count remains three. No successful observation exists, so the final outcome is unsuccessful even though the stopping mechanism worked correctly.

The expensive-call trace allows five cost units. It records two used units and a proposed reservation of four. The guard stops with cost_budget before dispatch; the tool-call count does not increase for that denied proposal. Counting proposals and executed calls separately makes this visible.

The successful trace gets an ok lookup within all budgets and ends completed. Its report includes one successful task, one call, and the actual termination reason. Together these cases show that a runtime can pass its engineering checks when some tasks intentionally fail. The acceptance condition is honest bounded behavior, not a fabricated success for every input. When evaluating a real model later, report task success rate alongside budget stops and no-progress stops.

// Quick review

Check your understanding.

Choose an answer for immediate feedback. Retry freely; a correct answer for every question unlocks the next section.

1. A failing loop stops after three allowed calls with no_progress. Did budget enforcement imply task success?
2. A cost-denied proposal never reaches dispatch. Which count should increase?
3. What should a model-agent evaluation report alongside task success rate?

Section 2.7 7 of 7

An expensive call within a short loop

Focus

Stop a short loop whose next action exceeds the spending budget.

A research agent has made only one decision, so its ten-step budget appears generous. Its next proposed analysis call would process a very large document. The declared cost ceiling is eight units, three have already been spent, and the new call’s reserved upper bound is six. The prospective total is nine, exceeding the ceiling.

The completed executor refuses the call before dispatch and records cost_budget. It can offer a bounded alternative, such as analyzing a smaller authorized excerpt whose reservation fits the remaining five units. The alternative still needs its own validity and usefulness checks; it is not automatically correct merely because it is cheaper.

This transfers the idea of independent budgets to practical systems. Step counts bound repeated decision-making but cannot bound a single large request. The same pattern applies to a file-size limit, a maximum number of returned rows, or a per-operation mutation cap. Every limit must be enforced at the component that controls the resource, so a prompt request to “stay cheap” supplements rather than replaces an actual dispatch guard.

// Quick review

Check your understanding.

Choose an answer for immediate feedback. Retry freely; a correct answer for every question unlocks the next section.

1. Three units are spent, the ceiling is eight, and the next call reserves six. What is the prospective total?
2. A smaller excerpt fits the remaining budget. Does that guarantee it answers the question?
3. Where should a cost limit be enforced?

Your personal notes

Optional, ungraded, and saved only in this browser. Export a backup from the Academy homepage to keep them.

References and further reading
Ask your own AI assistant

Copy this prompt into an assistant you already use. Nothing is sent automatically, and your personal notes are not included.

Next lesson: Planning bounded actions →