Hooks step into the native agent loop: before and after each tool call, and when the model would end the run.
A hook is a module implementing any of this behaviour's optional callbacks,
listed as module or {module, opts} by the agent's hooks/0 callback or
the session's :hooks option. Hooks run as a chain in list order, and each
callback receives the session's tool context with the hook's own opts.
| Callback | Returns | Chain |
|---|---|---|
before_tool/2 | :ok, {:rewrite, args}, or {:block, reason} | Rewritten arguments pass on; the first block stops it. |
after_tool/3 | :ok or {:replace, result} | The replaced result passes on. |
on_yield/2 | :ok or {:continue, message} | The first continue stops it. |
Each chain runs in its own process, linked below the run's worker. Aborting
the run stops it, so a hook may wait on a person with receive, bounded by
its own after, and needs no abort handling. A hook that crashes or returns
anything else fails what it was deciding: the tool call gets an error
result, or the answer stands. The crash is logged.
Hooks hold no state of their own. Keep state that outlives a call, such as pending approvals or budgets, in a supervised process the hook talks to. See the Hooks guide for examples.
Summary
Callbacks
Runs after a tool call, including calls a hook blocked and calls to unknown
tools, with the arguments the call ran with and its result as {:ok, value}
or {:error, reason}. Return {:replace, result} to change the result the
model and later hooks see. A replaced {:ok, value} must be a string or
encode as JSON, like a tool's result; otherwise the hook fails.
Runs before a tool call executes, with the call as
%{id: id, name: name, args: args}.
Runs when the model answers without tool calls, which would end the run,
with the assistant Condukt.Message.
Callbacks
@callback after_tool( call :: map(), result :: {:ok, term()} | {:error, term()}, context :: map() ) :: :ok | {:replace, {:ok, term()} | {:error, term()}}
Runs after a tool call, including calls a hook blocked and calls to unknown
tools, with the arguments the call ran with and its result as {:ok, value}
or {:error, reason}. Return {:replace, result} to change the result the
model and later hooks see. A replaced {:ok, value} must be a string or
encode as JSON, like a tool's result; otherwise the hook fails.
@callback before_tool(call :: map(), context :: map()) :: :ok | {:rewrite, map()} | {:block, String.t()}
Runs before a tool call executes, with the call as
%{id: id, name: name, args: args}.
Return {:rewrite, args} to run the call with other arguments, which later
hooks see, or {:block, reason} to skip the call and give the model an error
result with reason instead.
Runs when the model answers without tool calls, which would end the run,
with the assistant Condukt.Message.
Return {:continue, message} to add message as a user message and send the
model back to work in the same run. Continued turns count against the run's
:max_turns, and on the run's last turn this callback is not called.