# Stop Hook

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

- **Category**: Agentic AI
- **Subcategory**: Safety & Control
- **Canonical URL**: https://designpattern.fyi/patterns/stop_hook/

---

## Description
**Intent**: Define an explicit programmatic predicate that decides when the agent's loop should terminate.
**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.
**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).


## Use Cases
- 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.





## Trade-offs


### Advantages

- Explicit, testable termination logic.

- Independent from the model's self-assessment.




### Considerations & Drawbacks

- More code to maintain than 'while not done'.

- Predicate bugs cause hangs or premature stops.







---
**Reference**: [Original Source](https://www.agentpatternscatalog.org/patterns/stop-hook/)

