The caller-owned cancellation policy types.
A cancel_when: option is a 1-arity function called at two safe boundaries:
before a model request (phase: :turn, after the step budget and context
policy, before the turn prompt) and after an assistant response but before any
tool of that turn starts (phase: :tools). It returns :cont to continue or
{:cancel, reason} to stop the run.
A cancellation is terminal: the run returns {:exhausted, result} with
halt_reason: {:cancelled, reason} and there is no resume/3 for it. A
:turn cancel sends no request; a :tools cancel runs no tool. A cancel set
while tools are already running is observed at the next turn, because the
library never kills a running call or fabricates a partial result.
A policy that raises, throws or exits, or returns anything but a decision, is a configuration error, not a crash of the run.
Summary
Types
What a cancel_when: function answers: continue the run, or stop it.
What a policy knows at a safe boundary.
Types
@type decision() :: :cont | {:cancel, term()}
What a cancel_when: function answers: continue the run, or stop it.
@type info() :: %{ phase: :turn | :tools, step: pos_integer(), max_steps: pos_integer(), usage: LlmCall.Usage.t() | nil, request: LlmCall.Request.t(), messages: [LlmCall.Message.t()], calls: [LlmCall.ToolCall.t()] | nil }
What a policy knows at a safe boundary.
step is the turn about to start (:turn) or the turn that produced the
calls (:tools); max_steps is the budget. usage is the last response's
own usage, nil before the first request. request and messages describe
the context the request would carry. calls is nil at a turn boundary and
the whole batch at a tools boundary.