New Tool Radar

AI Agents & Automation

How to Build a First ReAct Agent With LangChain and Two Tools

Build a first ReAct agent with LangChain and two tools so the model decides which tool to call for each question.

Schematic with three steps: Define the two tools; Create the ReAct agent and the executor; Run the three test questions and read.

A LangChain agent differs from a chain in one way: the model decides at each step what action to take, calls a tool, looks at the result, and then decides what to do next. The ReAct pattern makes that loop visible: the agent reasons, acts, observes, and repeats. Build it with create_react_agent, two small tools defined with the tool decorator, and an AgentExecutor to run it, then watch the verbose output to see the loop working.

What you need

  • Python with langchain, langchain-openai and langchain-core installed and an API key
  • Two small tools the agent needs, such as a search and a calculator
  • Three test questions, one per tool and one needing neither

Step 1: Define the two tools

Write two small Python functions and mark each with the tool decorator, giving each a clear name and docstring. The docstring is what the model reads to decide when to call the tool, so describe the job and the input precisely.

Step 2: Create the ReAct agent and the executor

Pull the ReAct prompt template, create the agent with create_react_agent using your model, the tools and the prompt, then wrap it in an AgentExecutor with verbose mode on so you can see every thought, tool call and observation.

Step 3: Run the three test questions and read the verbose log

Run the three questions and read the verbose output: confirm the agent called the right tool for each question, used the tool result, and answered the third question directly without a tool. If it called the wrong tool, the docstring is the fix.

Worked example: synthetic ReAct run

The table below is a synthetic example. It shows the expected result of a ReAct agent; no agent was run.

Question Tool called Result
Search question Search tool Pass
Maths question Calculator tool Pass
General question None Pass

Expected result (UNTESTED). the agent calls the search tool for the search question, the calculator for the maths question, and no tool for the general question. This check was not run live; the table shows the expected result for the synthetic example.

Limits

  • An agent decides at each step, so a tool with a vague docstring will be called for the wrong questions.
  • Set a maximum iteration count on the executor, so a looping agent stops instead of running until the budget ends.
  • Verbose mode prints every thought, tool call and observation, so turn it off before the agent serves real users.

Sources

See all