Lesson 44.3 · 3 of 4 · 5 min read · Reviewed 24 Nov 2026
PostToolUse: check or log a result
By the end, you can
- Use PostToolUse to validate, transform or log a tool's result.
- Use a Stop hook to keep an agent working until a check passes.
PostToolUsePostToolUse hook: A hook that runs after a tool call, often to log, validate or post-process the result. fires once the tool has returned. The side effect has already happened, so this hookHook: Code that runs automatically at a lifecycle event, such as before or after a tool call, to check, block or log it. is about checking and reacting.
Three jobs
- Validate. After an edit, run the linter or type checker. If it fails, return the errors as feedback so the model fixes them on the next turn.
- Transform. Run the formatter so every file the agent touches comes out clean, or redactRedaction: Removing or masking sensitive data, such as PII or secrets, before it is stored, logged or sent. secrets from a tool result before the model reads it.
- Log. Write who called what, with which input, and what came back. That record is the start of observabilityObservability: Being able to understand what a system is doing from the data it emits: traces, logs and metrics. (chapter 67).
Stop hooks: no more early ‘done’
Lesson 42.1 said the loop needs a check. A Stop hook enforces it. When the agentAgent: A system where a model decides its own next steps, calling tools in a loop until the task is done. tries to finish, the hook runs the tests. If they fail, it blocks the stop and tells the model why, and the agent carries on. Add a turn limit too, so a check that can never pass doesn’t loop forever.
Keep hooks fast and quiet
Hooks run on every matching event. A slow hook slows every turn, and a noisy one fills the context. Return only what the model needs to act on.
Key points
- PostToolUse runs after the tool, so it can't prevent the call, but it can check and react to the result.
- Common uses: run the formatter or tests after an edit, flag bad output, and write an audit log.
- A Stop hook can refuse to let the agent finish until a check passes, which turns 'done' into a tested claim.
Refuse an early stop until tests pass
Use a Stop hook to turn an agent's 'done' into a tested claim, with a turn cap as backstop.
You will have: A Stop hook that runs the tests, blocks the stop while any fail, and a printed run log.
Steps
- Run the starter: the agent stops on turn 1 with 3 failing tests.
- Make the Stop hook run the tests and block the stop with a reason while any fail.
- Run it and watch the reason feed back into the next turn.
- Set failing to 9 and confirm the turn cap ends the run and says what still fails.
Done when
- The stop is only allowed on turn 4, when failing tests reach 0.
- Each blocked turn prints how many tests still fail.
- With failing = 9 the run ends at MAX_TURNS with 'turn cap reached'.
Open in the Playground No API key needed.
Starter code (Python)
# The agent always claims it is done. A Stop hook checks the claim before letting it finish.
failing = 3 # failing tests in the simulated repo
MAX_TURNS = 6 # backstop in case the tests can never pass
def agent_turn(feedback):
"""Simulated agent: fixes one failing test per turn, but only when told which tests fail."""
global failing
if feedback and failing > 0:
failing -= 1
return "All done, tests pass."
def run_tests():
return {"passed": failing == 0, "failing": failing}
def stop_hook():
# TODO: run the tests. If any fail, return {"block": True, "reason": "<n> tests still fail"}
return {"block": False}
feedback = None
for turn in range(1, MAX_TURNS + 1):
claim = agent_turn(feedback)
verdict = stop_hook()
status = "blocked: " + verdict["reason"] if verdict["block"] else "stop allowed"
print(f"turn {turn}: agent says '{claim}' -> {status}")
if not verdict["block"]:
break
feedback = verdict["reason"]
else:
print("turn cap reached: report what still fails")
print("failing tests at the end:", run_tests()["failing"])
Show a solution
# The agent always claims it is done. A Stop hook checks the claim before letting it finish.
failing = 3 # failing tests in the simulated repo
MAX_TURNS = 6 # backstop in case the tests can never pass
def agent_turn(feedback):
"""Simulated agent: fixes one failing test per turn, but only when told which tests fail."""
global failing
if feedback and failing > 0:
failing -= 1
return "All done, tests pass."
def run_tests():
return {"passed": failing == 0, "failing": failing}
def stop_hook():
result = run_tests()
if not result["passed"]:
return {"block": True, "reason": f"{result['failing']} tests still fail"}
return {"block": False}
feedback = None
for turn in range(1, MAX_TURNS + 1):
claim = agent_turn(feedback)
verdict = stop_hook()
status = "blocked: " + verdict["reason"] if verdict["block"] else "stop allowed"
print(f"turn {turn}: agent says '{claim}' -> {status}")
if not verdict["block"]:
break
feedback = verdict["reason"]
else:
print("turn cap reached: report what still fails")
print("failing tests at the end:", run_tests()["failing"])