Skip to main content

Module react

Module react 

Source
Expand description

The prebuilt ReAct agent (LangGraph create_react_agent parity).

create_react_agent assembles the classic reasoning-acting loop as a two-node cyclic graph over a single messages channel (Reducer::AddMessages):

        ┌──────────────────────────────────────────┐
        │                                          │
        ▼                                          │
     [agent] ── last message has tool_calls? ──► [tools]
        │                                          │
        └─ no tool_calls ──► End ◄── static edge ──┘
  • agent — serializes the messages channel into ChatMessages, calls ChatModel::chat with the registry’s OpenAI-format tool schemas, and appends the assistant message (final answer or tool-call request) back onto messages.
  • tools — takes the tool_calls of the last assistant message, dispatches them in parallel through ToolExecutor::execute_batch, and appends one role: "tool" message per call.
  • Routing — a conditional edge on agent routes to tools when the last message carries tool calls, otherwise to Route::End; a static edge loops tools → agent so the model observes the tool results.

The caller drives the returned Graph with a crate::state::StateSpec declaring messages with Reducer::AddMessages and an initial state seeding the conversation (see examples/react_agent.rs).

Four flavors exist: create_react_agent (the agent node calls ChatModel::chat; no crate::executor::GraphEvent::Token events), create_react_agent_streaming (the agent node calls ChatModel::chat_stream and forwards deltas as crate::executor::GraphEvent::Tokens into the run’s event channel), and the Flight Recorder pair create_react_agent_with_recording / create_react_agent_replaying.

§Flight Recorder

create_react_agent_with_recording wires the run’s Journal into both nodes: every model call is journaled through crate::replay::RecordingChatModel and every tool call through crate::replay::RecordingTool, in the canonical crate::replay::model_call_request / crate::replay::tool_call_request payload shapes, parented per iteration to the invocation’s node-input event (the executor hands its id over via crate::journal::PARENT_EVENT_KEY). Attach the same journal to the run with crate::executor::RunConfig::with_journal. create_react_agent_replaying is the mirror image for exact replay: the same topology with crate::replay::ReplayingChatModel / crate::replay::ReplayingTool answering from the recorded journal — zero outbound calls, so the wrapped model and tools may be panic-on-call sentinels. See examples/react_record_replay.rs for the full record → replay loop.

Constants§

AGENT_NODE
The name of the model-calling node in the compiled graph.
MESSAGES_CHANNEL
The state channel the ReAct loop reads from and appends to. Declare it with Reducer::AddMessages in the run’s crate::state::StateSpec.
TOOLS_NODE
The name of the tool-dispatch node in the compiled graph.

Functions§

create_react_agent
Build a prebuilt ReAct agent graph over model and tools.
create_react_agent_replaying
Build a prebuilt ReAct agent graph that answers every model and tool call from a recorded journal instead of executing it (exact replay).
create_react_agent_streaming
Build a prebuilt ReAct agent graph whose agent node streams token deltas as GraphEvent::Tokens through token_tx (ChatModel::chat_stream under the hood; LangGraph’s messages stream mode).
create_react_agent_with_recording
Build a prebuilt ReAct agent graph that journals every model and tool call into journal (Flight Recorder, R0.5).