LangChain.Chains.LLMChain.Modes.WhileNeedsResponse (LangChain v0.14.2)

Copy Markdown View Source

Execution mode that loops while the chain needs a response.

After each LLM call, if the response contains tool calls, this mode:

  1. Executes the pending tool calls
  2. Applies any LangChain.MessageExpansion those tools asked for
  3. Calls the LLM again with the tool results
  4. Repeats until needs_response is 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 every LLMChain.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)