A stateless tool loop over LlmCall.
See docs/design.md for the contract.
Summary
Functions
Renders a tool's return value without a byte budget.
Renders a tool's return value as the text the model sees.
Continues a run that paused.
Continues a run from a completed tool turn.
Runs one tool loop over request.
Types
@type outcome() :: {:ok, AgentCall.Result.t()} | {:exhausted, AgentCall.Result.t()} | {:pending, AgentCall.Pending.t()} | {:error, AgentCall.Error.t()}
What a run or a resumed run can return.
{:ok, result} means the model asked for no more tools; {:exhausted, result} means the loop stopped before the model finished; {:pending, pending} means the run paused for approval or input, discriminated by
Pending.kind; {:error, error} means the inputs or a dependency refused the
run.
Functions
Renders a tool's return value without a byte budget.
Equivalent to default_format(value, :infinity).
@spec default_format(term(), non_neg_integer() | :infinity) :: String.t()
Renders a tool's return value as the text the model sees.
A binary is rendered verbatim; everything else is inspected without printable or collection limits, because a truncated map is a different bug than a large one.
max_bytes bounds the rendering instead of building a string that would be
immediately truncated. A collection is rendered with at most max_bytes
entries, and a rendering that elided any is necessarily over the budget —
every entry renders to at least one byte — so a value that fits is never
elided. The bound is approximate in the other direction: inspect/2 counts
entries, not bytes, so one large entry can overrun it, and the returned text
is cut to max_bytes on a code-point boundary.
:infinity keeps the unbounded rendering.
@spec resume(LlmCall.Client.t(), AgentCall.Pending.t(), keyword()) :: outcome()
Continues a run that paused.
pending is the value run/3 returned as {:pending, pending}. It takes
exactly one decision for the pause: approval: (:granted or :denied) for
a kind: :approval pause, or input: ({:answer, term} or {:cancel, term})
for a kind: :input pause. A decision that does not match the pause kind is a
:configuration error before anything runs.
An approval applies to the head of pending.remaining, the call the pause was
about. That call is re-dispatched with the decision, the rest of the turn runs
under tool_opts: unchanged, and the whole turn's results go back to the
model in one user turn, in call order. A denial is an is_error result the
model reads, not an abort.
A tool-target input pause never ran its head call: {:answer, value} is
rendered with format_result: and becomes that call's result, and
{:cancel, reason} becomes an is_error result naming the cancellation. The
rest of the turn then runs as above.
A turn-target input pause happened before a model request: {:answer, value}
is rendered with format_result: and capped by max_result_bytes:, appended
as exactly one user message, and the same model turn continues at
pending.steps + 1, running the context and prompt policies again.
{:cancel, reason} is {:exhausted, result} with
halt_reason: {:input, reason} and sends no request; response and
stop_reason are nil, and the transcript is the one the pause carried.
For a tool-target pause, before any call runs, the pause is checked against
the head call: pending.approval by the head's name and parsed arguments (the
approval request stores no call id), and pending.input.call by the head's
call id, name and arguments. A mismatch is a :configuration error.
A resumed tool-target pause already passed the step-budget guards, so it
finishes even if max_steps: is now lower. A turn-target pause's answer
starts the model turn it paused before, so the budget is checked against the
steps already taken before that request. When that check stops the resumed
run, {:exhausted, result} carries response: nil and stop_reason: nil,
because the paused turn's response is not stored in Pending;
halt_reason: :budget, the counters and the transcript describe the ending.
A context: policy supplied to resume/3 runs before the next model request,
not before the re-dispatched head call; a policy stop there has the same nil
response and stop_reason, with halt_reason: {:context, reason}.
Because of that ordering, the policy cannot change the tool set the
re-dispatched batch runs under: it may narrow from the next turn onward, but
pending.remaining is dispatched with the set the Pending carried. To
dispatch under a different set, narrow before the pause.
A resumed run keeps the Pending's run_id and accepts the run options of
run/3 except run_id:. A re-supplied max_concurrency: is a wave size and
controls the resumed batch (default 1): a parallel pause resumed without it
runs serially. As in run/3, a value greater than 1 runs the tool-phase
prompt: and format_result: in worker tasks. A re-supplied cancel_when:
is checked at each new turn entry and before a new tool batch; it is not
checked before the paused batch this function re-dispatches, because that
batch runs under the explicit approval/input decision and was already
admitted by the original run.
Options are those of run/3 plus the required decision; on_event: observes
the resumed run, which starts with :run_started(resumed?: true).
@spec resume_checkpoint(LlmCall.Client.t(), AgentCall.Checkpoint.t(), keyword()) :: outcome()
Continues a run from a completed tool turn.
checkpoint is the value a :checkpoint event carried after a completed
tool turn. It accepts the run options of run/3 except run_id: and takes no
approval/input decision: a checkpoint never paused. The checkpoint's
run_id is kept, and the resumed run starts at checkpoint.steps + 1.
The completed tools are not re-run. The next request uses
checkpoint.transcript, and steps, tool_calls, usage and last_usage
carry forward. The checkpoint is validated before any event or request: a
malformed shape, request, transcript, counter or tool map is a
:configuration error. A supplied run_id: or a decision is also a
:configuration error.
The budget is checked against checkpoint.steps. When the next turn would be
beyond max_steps:, the run returns {:exhausted, result} with
response: nil, stop_reason: nil, halt_reason: :budget, the checkpoint's
transcript and counters, and sends no request.
context:, prompt:, cancel_when:, max_steps:, stream:, tool_opts:,
max_result_bytes:, format_result:, max_concurrency:, codec:, headers:
and on_event: are re-supplied; none is stored in the checkpoint. A pause
after a resume is a normal %AgentCall.Pending{}, answered with resume/3;
the next completed tool turn emits a new :checkpoint. A streamed completed
turn checkpoints; a stream failure discards its partial content, emits no
checkpoint and returns the existing :llm error, so the last checkpoint is
the retry point.
@spec run(LlmCall.Client.t(), LlmCall.Request.t(), keyword()) :: outcome()
Runs one tool loop over request.
request.tools is the single source of truth: the tools the model may call
are the tools this loop can run. Options are those of design §9.1, §23.1 and
§23.2.
context: may also return {:cont, messages, tools} to replace the tool
entries for that turn and later ones, which is how a run grants tools
progressively; Pending and Checkpoint carry the narrowed set.
cancel_when: is checked at the two safe boundaries. max_concurrency: runs
a turn's calls in ordered waves of at most that many (default 1); a later
wave waits for the previous one, and a pause stops later waves. With a value
greater than 1, a tool-phase prompt: and format_result: run in worker
tasks, so a policy or renderer that relies on process-local state is the
caller's responsibility; on_event: stays in the loop's process. run_id:
is the run's correlation id, generated when omitted.
The request's exact fields, its nested messages/tools shapes and every
Message/content part are checked before the first event; an improper list, a
non-Message entry, a fake nested struct or another malformed request is a
:configuration error. This is a live-request check, so it does not require
the request to be portable; checkpoint records still do.