Skip to content
Concept-Lab
← LangGraphπŸ•ΈοΈ 22 / 42
LangGraph

ReAct using LangGraph - Building Nodes

Implement reason and act nodes with strict contracts, deterministic state updates, and safe tool invocation.

Core Theory

Node implementation is where architecture becomes executable behavior. In this step you implement two concrete nodes:

  • reason node: invoke reasoning runnable and write agent_outcome.
  • act node: execute tool based on agent_outcome and append (action, observation) to intermediate_steps.

Reason node requirements: pass exact expected state keys, validate runnable output type, and never write tool outputs directly.

Act node requirements: resolve tool from registry, validate input schema, enforce timeout, catch tool errors, and convert output into stable string/JSON representation for state.

Critical invariant: act node should only run when current outcome is AgentAction; finish outcomes must bypass action execution.

Failure handling strategy:

  • Tool not found -> append structured error observation and route to reason for fallback decision.
  • Tool timeout -> append timeout marker + increment retry counter.
  • Serialization error -> safe-stringify observation and attach parse status.

Testing priority: node-level tests for state updates and error branches, then integration tests for full loop routing.

Deepening Notes

Source-backed reinforcement: these points are extracted from the LangGraph source note to sharpen architecture and flow intuition.

  • n node as well as the ACT node right here so let's actually let me take you through it step by step so right here you can see that we are going to use the react agent runnable that
  • ing to use the react agent runnable that we wrote in the previous section and all that we are going to do is we are going to invoke on it using the agent State okay so all the valu
  • ing the agent State okay so all the values all the these things that we see right here these are reserved keywords and this you know runnable is going to know exactly what to do wi
  • ere we have the agent outcome that is going to be an agent action or the finish so as soon as this node is executed it is either going to have agent action or agent finish only dur
  • there's always going to be action or finish all right all right so let's come back to the graph so now let's let's assume that it is going to be agent action now it is going to com

Interview-Ready Deepening

Source-backed reinforcement: these points add detail beyond short-duration UI hints and emphasize production tradeoffs.

  • Implement reason and act nodes with strict contracts, deterministic state updates, and safe tool invocation.
  • Reason node requirements: pass exact expected state keys, validate runnable output type, and never write tool outputs directly.
  • Act node requirements: resolve tool from registry, validate input schema, enforce timeout, catch tool errors, and convert output into stable string/JSON representation for state.
  • Testing priority: node-level tests for state updates and error branches, then integration tests for full loop routing.
  • Fine-grained state graphs improve control, but poor state contracts can create brittle routing behavior.
  • Tool-heavy loops improve grounding, but latency and failure surfaces rise with each external dependency.
  • Node implementation is where architecture becomes executable behavior.
  • Critical invariant: act node should only run when current outcome is AgentAction ; finish outcomes must bypass action execution.

Tradeoffs You Should Be Able to Explain

  • More agent autonomy increases adaptability but also increases non-determinism and debugging effort.
  • Tool-heavy loops improve grounding, but latency and failure surfaces rise with each external dependency.
  • Fine-grained state graphs improve control, but poor state contracts can create brittle routing behavior.

First-time learner note: Think in state transitions, not giant prompts. Keep node responsibilities small and route logic deterministic so each step is easy to reason about.

Production note: Bound autonomy with loop limits, tool policies, and checkpoints. Capture route decisions and state snapshots for replay and incident analysis.

🧾 Comprehensive Coverage

Exhaustive coverage points to ensure complete topic understanding without missing core concepts.

Loading interactive module...

πŸ’‘ Concrete Example

Reason/act node contract example: 1) reason node writes AgentAction(fetch_order_status). 2) act node validates args and executes tool with timeout. 3) act node appends structured observation tuple. 4) reason node emits final response based on observation. Timeout branch: - act node writes error observation (type=timeout, attempt=1). - next reason step decides retry or escalate based on policy state.

🧠 Beginner-Friendly Examples

Guided Starter Example

Reason/act node contract example: 1) reason node writes AgentAction(fetch_order_status). 2) act node validates args and executes tool with timeout. 3) act node appends structured observation tuple. 4) reason node emits final response based on observation. Timeout branch: - act node writes error observation (type=timeout, attempt=1). - next reason step decides retry or escalate based on policy state.

Source-grounded Practical Scenario

Implement reason and act nodes with strict contracts, deterministic state updates, and safe tool invocation.

Source-grounded Practical Scenario

Reason node requirements: pass exact expected state keys, validate runnable output type, and never write tool outputs directly.

🧭 Architecture Flow

Loading interactive module...

🎬 Interactive Visualization

Loading interactive module...

πŸ›  Interactive Tool

Loading interactive module...

πŸ§ͺ Interactive Sessions

  1. Concept Drill: Manipulate key parameters and observe behavior shifts for ReAct using LangGraph - Building Nodes.
  2. Failure Mode Lab: Trigger an edge case and explain remediation decisions.
  3. Architecture Reorder Exercise: Reorder 5 flow steps into the correct production sequence.

πŸ’» Code Walkthrough

Node-level implementation for reasoning and acting.

content/github_code/langgraph/6_react_agent/agent_reason_runnable.py

Reason step using ReAct prompt and tool-enabled runnable.

Open highlighted code β†’

content/github_code/langgraph/6_react_agent/react_state.py

Typed state contract for agent outcome + intermediate steps.

Open highlighted code β†’
  1. Trace how tool output is appended into intermediate_steps.

🎯 Interview Prep

Questions an interviewer is likely to ask about this topic. Think through your answer before reading the senior angle.

  • Q1[beginner] What exact state mutation should each ReAct node be responsible for?
    Reason node should only update decision fields (for example agent_outcome), while act node should only append observations and execution metadata. Clear ownership prevents silent state corruption.
  • Q2[beginner] How do you enforce the invariant that act node only runs on AgentAction?
    Enforce act-node preconditions by checking outcome type before execution and routing non-action outcomes directly to finish/end path.
  • Q3[intermediate] How should act node represent tool failures in state?
    Represent tool failures as structured observations (error_type, message, retryable, latency) so the next reasoning step can choose retry, fallback, or escalation deterministically.
  • Q4[expert] What test cases are mandatory before shipping these nodes?
    Mandatory tests: success path, malformed action path, unknown tool path, timeout path, retry path, and finish-without-act path.
  • Q5[expert] How would you explain this in a production interview with tradeoffs?
    Interviewers look for ownership boundaries: reason writes decisions, act writes observations, route function decides control flow.
πŸ† Senior answer angle β€” click to reveal
Use the tier progression: beginner correctness -> intermediate tradeoffs -> expert production constraints and incident readiness.

πŸ“š Revision Flash Cards

Test yourself before moving on. Flip each card to check your understanding β€” great for quick revision before an interview.

Loading interactive module...