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

ReAct using LangGraph - Reasoning Runnable

Build the reasoning runnable that converts state into either AgentAction or AgentFinish with strict output contracts.

Core Theory

The reasoning runnable is the policy brain. It consumes current graph state and emits one of two structured outcomes:

  • AgentAction: tool name + validated tool input
  • AgentFinish: final answer payload and finish metadata

Contract-first design: do not parse free-form prose to infer actions. Use structured output parsing and strict type checks before writing to agent_outcome.

Prompt inputs typically include: user objective, prior intermediate_steps, tool descriptions, and policy constraints. Keep these fields normalized so route behavior remains stable.

Failure modes:

  • Runnable emits malformed tool args.
  • Runnable keeps emitting actions when evidence is already sufficient.
  • Runnable hallucinates unavailable tools.

Mitigations: parser retry on malformed outputs, fallback model for parser failures, hard tool-name validation against registry, and confidence-driven finish policy.

Production guidance: version prompts and parser schemas together; changes to either can alter route behavior and must be regression-tested.

Deepening Notes

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

  • so in this section let's go ahead and build out the the chain or the runnable that is required to build out the reason node which is very similar to what we did in the reflection r
  • to be using the create react agent the Tav tool and you know we have the llm as well so what is the next thing that we need so we can just go back to the first section and then let
  • so we have the get system time tool basically I'm going to replicate the same example that we saw we don't want to reinvent the wheel because we're just learning right so I'm just
  • me tool so let me come back put it over here perfect and finally we are going to use this particular create react agent method and let's look at what are the things that it needs s
  • the react we are going to need a few you know we are going to think about what are the states that we need so we'll look at that in the next section

Interview-Ready Deepening

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

  • Build the reasoning runnable that converts state into either AgentAction or AgentFinish with strict output contracts.
  • Use structured output parsing and strict type checks before writing to agent_outcome .
  • Mitigations: parser retry on malformed outputs, fallback model for parser failures, hard tool-name validation against registry, and confidence-driven finish policy.
  • Production guidance: version prompts and parser schemas together; changes to either can alter route behavior and must be regression-tested.
  • AgentAction : tool name + validated tool input
  • AgentFinish : final answer payload and finish metadata
  • Runnable keeps emitting actions when evidence is already sufficient.
  • It consumes current graph state and emits one of two structured outcomes:

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

Typed outcome walkthrough: 1) Runnable receives input + prior steps and emits AgentAction(fetch_order). 2) Tool result is appended to state.intermediate_steps. 3) Runnable re-evaluates with fresh context and emits AgentFinish. 4) Router terminates cleanly. Invalid-action branch: - Runnable outputs unknown tool name. - Validator rejects action before execution. - Graph routes to fallback/human path instead of attempting unsafe tool call.

🧠 Beginner-Friendly Examples

Guided Starter Example

Typed outcome walkthrough: 1) Runnable receives input + prior steps and emits AgentAction(fetch_order). 2) Tool result is appended to state.intermediate_steps. 3) Runnable re-evaluates with fresh context and emits AgentFinish. 4) Router terminates cleanly. Invalid-action branch: - Runnable outputs unknown tool name. - Validator rejects action before execution. - Graph routes to fallback/human path instead of attempting unsafe tool call.

Source-grounded Practical Scenario

Build the reasoning runnable that converts state into either AgentAction or AgentFinish with strict output contracts.

Source-grounded Practical Scenario

Use structured output parsing and strict type checks before writing to agent_outcome .

🧭 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 - Reasoning Runnable.
  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

Reasoning runnable details and prompt/tool setup.

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. Focus on create_react_agent and tool list wiring.

🎯 Interview Prep

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

  • Q1[beginner] Why should a reasoning runnable output typed actions instead of plain text instructions?
    Typed actions are required because downstream execution must be deterministic and schema-validated. Free-form text instructions are ambiguous and unsafe for automated tool invocation.
  • Q2[beginner] How do you validate AgentAction payloads before execution?
    Validate AgentAction by checking allowed tool name, argument schema, and policy constraints before any call. Reject invalid actions into a safe fallback route rather than guessing.
  • Q3[intermediate] What is your fallback strategy when parsing fails repeatedly?
    For repeated parse failures, use bounded parser retries, optional backup model, and then fail-safe completion/escalation. Never allow unbounded parser recovery loops.
  • Q4[expert] How would you detect runnable prompt drift after a model upgrade?
    Detect prompt drift by running fixed regression prompts, comparing route/action distributions across versions, and diffing traces for loop depth, wrong-tool frequency, and completion quality.
  • Q5[expert] How would you explain this in a production interview with tradeoffs?
    Frame it as contract engineering: typed outputs, validator gates, schema versioning, and regression evals on route decisions.
πŸ† 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...