Safety & Control

Stop Hook

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

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.

Solution

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).

When to use

  • Agent loops need an explicit termination predicate beyond model self-declaration.
  • Conditions like budget hit, error, or stagnation can be detected programmatically.
  • Costs of an unbounded loop are unacceptable.

Open the full interactive page

Diagram, neighbourhood map, code examples, related patterns and full provenance.

Related