Add hooks to a call
Build oneHooks at import. Build one CallHooks per call, and pass it to the agent as call=.
- Many calls may share one
Hooks. It holds only your functions. - A
CallHooksbinds to one call. Build a new one for each call. - A hook may be sync or async, except a
prompthook, which must be a plain function, and awrap_*hook, which must be async.
Add a line after every prompt
- The suffix goes after the agent’s prompt, every task step’s, the finish checks’ and the summaries’, after a blank line.
- With
model=, only prompts that model reads get it. - It refuses blank text, more than 512 characters, and a
{{...}}placeholder.
Change a model request
request.modelnames the model that answers, likegpt-5.6-luna. Return None to keep the request as it is.request.paramsare extra body fields for this request only.wrap_modelhooks run around the request, andafter_modelhooks hear the answer.
Refuse or change a tool call
Refuse(message)stops the call. The tool never runs, and the model reads the message.- Return a changed
ToolRunto change the arguments. They are checked against the schema again. wrap_toolhooks run around the tool, asasync (run, handler). Callawait handler(run)to run it.
Run code when a tool ends
- Return None to keep the result. Return something else to replace what the model reads.
- An
after_toolhook that raises is logged, and the model reads the tool’s own result.
Watch call events
- A pattern may hold
*, liketask.*. A pattern that matches no event raisesValueError. - Observers only watch. One that raises is only logged.
- The end of the call waits for
call.endedobservers, up toend_timeout_s.
Where the model hooks run
Theprompt, before_model, wrap_model and after_model hooks run only where the library sends the model request. The tool hooks and the event hooks do not depend on this.
When a call has model hooks and no request goes through the library, the adapter logs one warning that says what to change.
wrap_model reaches only a request that a ModelCall answers.
Run tool hooks on a plain agent
UnmuteAgent and UnmuteFlows pass the call’s hooks to every tool. On a plain agent, pass them yourself.
hooks=callruns the tool hooks, and fires thetool.*events for your tools.- Framework tools skip the tool hooks: end call, handoffs and
native_tools. So do prefetch lookups.
Where to go next
Hooks reference
Every point, event, rule, argument and error.
The call's life
CallHooks, tools on events, the failure line and the end.Tools
The tools your hooks run around.
Model call
The
ModelCall that wrap_model reaches.Hooks in agent.yaml
Where Unmute declares
prompt_suffix.