genai.chat component takes the
tools on its tools port, ends on a tool_call route when the model stops to call them, and runs again once the
results are in its history. Here the flow runs that loop with a logic.code component.
The Service Desk Agent in the workflow gallery shows the loop
running.
Declaring tools
Each tool needs a
name and a parameters object; the definition check names the index of one that lacks either.
Two checks hold a flow to the model:
- The capability.
toolsis a capability of the catalog. Agenai.chatthat holdstoolsneeds a model that does it:model: <id> does not do tools, which its consumers need. A model the catalog does not know passes, and the vendor decides. - A forced choice. Four catalog models refuse
required. Chat sends themautoinstead and warns:tool_choice "required" is not supported by <id>: sent "auto". The list is on the language model page.
The tool_call route
When the answer ends with finish tool_call, chat writes its output and ends on tool_call instead of success. It
applies neither output_role nor the role of output_metadata to that answer
(A tool loop keeps its roles).
- Parallel calls are several
tool_callblocks of one message, each with its ownid. - A flow without tools never takes the route.
- A flow that holds
toolsmust wiretool_call. The definition check refuses it otherwise:tool_call: the flow holds tools, so the tool_call route must be connected. A route left unwired would fail the run at the first call.
Results
The results go back as oneuser message of tool_result blocks, one per call, in order. Each block names the id of
the call it answers, and its content is blocks: text, json or a file. Anthropic is sent every result of a turn in
one user turn. On Gemini a result that answers no call in the history fails the call:
tool_result <id> answers no call in the history.
A message made only of tool_result blocks is stored on a thread as a reply, the role of the answer it answers,
unless the component that posts it gives it a role, and reads back as a user message.
The logic.code helpers
Both are bound in the code Sandbox. See Code Component.
id raises ValueError("tool result <n> has no id").
The loop
- Chat reads its history and calls the model. The answer holds calls and ends with
finishtool_call. - Chat writes the answer to its output and ends on
tool_call. When its output is wired to adata.thread, the answer is posted there as an item. - A
logic.codecomponent reads the answer, runs each call and builds the results message. Wired to the samedata.thread, its output is posted as one item, whose parent is the answer’s item. - Chat runs again and reads the call and its results with the rest of the history. A call’s results count right after it, ahead of anything posted to the thread while the call ran, so the model reads the call, its results, then the rest (Selecting a History).
success once an answer ends with any other finish.
What the loop keeps steady
- The start is fixed for the run. A chat’s first call in an execution fixes where its history starts. Its later calls start in the same place, so a post or a newer anchor that arrives in between does not move the history under the loop. See Selecting a History.
- Reasoning travels with the answer. A model’s reasoning blocks keep the vendor’s own record in
vendor_data. A call to the same vendor sends it back unchanged, in order and verbatim, so the vendor can continue its reasoning across the calls of the loop; a call to another vendor leaves areasoningblock out. See Data Types. A model on thegeminivendor that continues another vendor’s loop is sent a placeholder thought signature on the first call of each step of the current turn that has none (Reasoning). - The roles stay
reply. Whatever role the chat outputs, the loop’s calls arereplyitems, and so are its results unless the component running the tools gives them a role. When the chat’s role is notreply, a listener on it hears only the final answer (A tool loop keeps its roles).
Example: a service desk lookup
Agenai.chat for the Ostrava Pumps service desk holds one tool, lookup_pump, and its tool_call route goes to this
logic.code. The lookup table stands in for an internal system.
dict, so it becomes one json block of the result; the text of an error becomes one text block. When
the model asks for two serial numbers at once, tool_calls returns two entries and tool_result_message returns one
message with two tool_result blocks, in the order of the calls.
The results output is wired to the same data.thread as the chat’s output, so each round of the loop is on the thread
for the conversation widget to show.
