Execution mode that loops while the chain needs a response.
After each LLM call, if the response contains tool calls, this mode:
- Executes the pending tool calls
- Applies any
LangChain.MessageExpansionthose tools asked for - Calls the LLM again with the tool results
- Repeats until
needs_responseis false (no more tool calls)
The LLM always gets the last word after tool execution.
An assistant message that is narration only (see
LangChain.Message.narration?/1) also leaves needs_response true, so the
LLM is called again to finish its turn.
Step 2 costs nothing for a tool that asks for no expansion, which is every tool that has not opted in. It is included so that a tool carrying one is honoured under the standard mode rather than silently ignored.
Options
:max_runs- Maximum LLM calls in one run before returning%LangChainError{type: "exceeded_max_runs"}. Default: 25. The count starts at 0 on everyLLMChain.run/2, so a chain that is run again after a new message gets a fresh budget.
Usage
LLMChain.run(chain, mode: :while_needs_response)
# or
LLMChain.run(chain, mode: LangChain.Chains.LLMChain.Modes.WhileNeedsResponse)