VIII · Safety & ControlMature★★

Stop Hook

also known as Termination Predicate, Halt Condition, Stop Condition, Done Predicate, Exit Condition, Loop Termination Rule

Define an explicit programmatic predicate that decides when the agent's loop should terminate.

This pattern helps complete certain larger patterns —

  • specialisesStep Budget★★Cap the number of tool calls or loop iterations the agent is allowed within a single request.
  • used-byChat ChainDecompose a long, multi-disciplinary task into ordered phases; within each phase, run a paired-role chat between two agents until the phase artefact is signed off; pass the artefact to the next phase.

Context

A team is operating an agent loop where the agent repeatedly thinks, acts, observes, and decides whether to keep going. The loop needs an explicit stop condition that does not rely on the model itself declaring 'done', because in practice the model's own sense of completion is unreliable — it either stops too early on hard tasks or refuses to stop on easy ones.

Problem

When termination is left implicit, with the loop ending only when the model says it is finished, the agent stalls in two opposite ways. On uncertain tasks the model will not commit to 'done' and keeps generating one more step indefinitely; on stuck tasks the model will keep trying variations of the same broken approach. Both burn budget and produce poor results. The team needs an explicit programmatic predicate — a stop hook — that decides termination from outside the model, based on observable signals such as goal completion, step count, repeated outputs, or detected errors.

Forces

  • Predicate complexity trades correctness for performance.
  • Stop too early loses work; stop too late wastes calls.
  • Coverage: which conditions warrant a stop?

Example

An agent's loop terminates when 'the model says it is done', which fails when the model is uncertain or stuck and the loop runs to budget. The team adds an explicit stop-hook predicate that runs after each step and returns continue, stop-success, or stop-failure based on target reached, step budget, error class, or stagnation detection. Termination becomes a programmatic decision rather than a wish, and unbounded loops become impossible by construction.

Diagram

Solution

Therefore:

Implement a stop hook function that runs after each step. It returns one of: continue, stop-success, stop-failure. Conditions include: target reached, step budget hit, error encountered, stagnation detected (no progress in last N steps).

What this pattern forbids. The loop terminates exactly when the stop hook says so; no other code path may exit the loop.

And the patterns that stand alongside it, or against it —

  • alternative-toUnbounded LoopAnti-pattern: run the agent loop without a step budget and let model self-termination decide.
  • alternative-toInfinite DebateAnti-pattern: launch multi-agent debate without a termination rule and watch the agents loop forever.
  • complementsKill SwitchProvide an out-of-band control plane to halt running agent instances without redeploy.

Neighbourhood

Click any neighbour to follow the language. Scroll to zoom, drag to pan.

References

Provenance