AI Hints
Home›LangGraph Tutorial›Agents by Hand
LangGraph · Lesson 34 of 120

A ReAct Agent

The loop by hand.

⏱ 2 min read▶ Video at 35:42🎓 Free
Watch this lesson in the LangGraph courseWatch on YouTube ↗Full LangGraph course

A ReAct agent reasons, then acts, then looks at the result, and repeats. We have every piece. A model with tools, a tool node, and tools condition. Let's wire them into a loop.

A ReAct Agent
From the video: A ReAct Agent
  • Reason, act, observe
  • We have every piece
  • Wire the loop

The agent graph

The agent node calls the model with tools. The tools node runs the calls. Tools condition routes from the agent, to tools, or to the end. And an edge from tools back to the agent closes the loop. Run it. The agent checks stock and price, then answers in a sentence.

react_agent.pypython
from langchain.chat_models import init_chat_model
from langgraph.graph import MessagesState, StateGraph, START
from langgraph.prebuilt import ToolNode, tools_condition
from shop_tools import check_stock, get_price

tools = [check_stock, get_price]
model = init_chat_model("google_genai:gemini-flash-lite-latest", temperature=0).bind_tools(tools)

def agent(s: MessagesState):
    return {"messages": [model.invoke(s["messages"])]}

b = StateGraph(MessagesState)
b.add_node(agent); b.add_node("tools", ToolNode(tools))
b.add_edge(START, "agent")
b.add_conditional_edges("agent", tools_condition)
b.add_edge("tools", "agent")
graph = b.compile()
out = graph.invoke({"messages": [{"role": "user", "content": "Is Dune in stock, and what does it cost?"}]})
print(out["messages"][-1].text)
Output
Yes, *Dune* is in stock with 3 copies available, and it costs £9.99.
Also uses shop_tools.py
from langchain.tools import tool

BOOKS = {
    "The Hobbit": {"stock": 7, "price": 8.99, "author": "J.R.R. Tolkien"},
    "Dune": {"stock": 3, "price": 9.99, "author": "Frank Herbert"},
    "Circe": {"stock": 0, "price": 10.99, "author": "Madeline Miller"},
}

@tool
def check_stock(title: str) -> int:
    """Copies of a book in stock at Riverside Books."""
    return BOOKS.get(title, {}).get("stock", 0)

@tool
def get_price(title: str) -> float:
    """Price of a book in pounds."""
    return BOOKS.get(title, {}).get("price", 0.0)

@tool
def get_author(title: str) -> str:
    """Author of a book."""
    return BOOKS.get(title, {}).get("author", "unknown")

One run, step by step

Here's what happened. The agent asked for two tools. The tool node ran them. Back at the agent, the model read the results, and wrote a final answer. Tools condition saw no more tool calls, and ended the run.

  1. Agent: ask tools
  2. Tools run
  3. Agent: answer
  4. No calls left
  5. END
One run, step by step
From the video: One run, step by step

Summary

A ReAct agent is a model node, a tool node, tools condition, and an edge back. That's the loop that powers most agents. Next, watching each step as it happens.

Key takeaways
  • agent ↔ tools
  • tools_condition
  • Edge back closes the loop
Next up: Watching the Agent — step by step