Behaviour for lifecycle hooks.
Hooks observe and transform data at each stage of client execution. All callbacks are optional.
Return Types
{:cont, value}- Continue with value{:halt, response}- Short-circuit with response{:error, reason}- Abort with error
Callbacks
on_call_start/3- Before LLM callon_call_end/3- After successful callon_call_error/3- On call failureon_stream_start/3,on_stream_chunk/3,on_stream_end/2- Stream lifecycleon_backend_request/2,on_backend_response/2- Backend lifecycleon_compaction_start/3,on_compaction_end/2- Compaction lifecycle
Example
defmodule MyApp.LoggingHooks do
@behaviour Puck.Hooks
require Logger
@impl true
def on_call_start(_client, content, _context) do
Logger.info("Call started")
{:cont, content}
end
@impl true
def on_call_end(_client, response, _context) do
Logger.info("Call completed")
{:cont, response}
end
endUsage
client = Puck.Client.new({Puck.Backends.ReqLLM, "anthropic:claude-sonnet-4-5"},
hooks: MyApp.LoggingHooks
)
# Multiple hooks execute in order
Puck.call(client, "Hello", context,
hooks: [MyApp.LoggingHooks, MyApp.MetricsHooks]
)
Summary
Functions
Invokes an observational hook callback (return value is ignored).
Invokes a transforming hook callback on the given hook module(s).
Merges client-level hooks with per-call hooks.
Types
@type chunk() :: map()
@type client() :: Puck.Client.t()
@type config() :: map()
@type context() :: Puck.Context.t()
@type messages() :: [map()]
@type response() :: Puck.Response.t()
Callbacks
Functions
Invokes an observational hook callback (return value is ignored).
Used for callbacks like on_call_error, on_stream_chunk, on_stream_end
where the return value doesn't affect the pipeline.
Invokes a transforming hook callback on the given hook module(s).
Returns the transformed value, a halt response, or an error. If a callback is not implemented, the initial value is passed through.
Returns
{:cont, value}- Continue with (possibly transformed) value{:halt, response}- Short-circuit with a response{:error, reason}- Abort with error
Merges client-level hooks with per-call hooks.
Per-call hooks come after client-level hooks (client hooks run first).