Course navigation
AgentsLesson 4 of 9

ReAct Framework

Tool Calling used tool_calls JSON. ReAct is the text version — the model prints each step. You will still see it in older repos and blog posts.

Lines in the output

Agent Types already sketched these. On a real run they look like:

Thought — short plan before picking a tool.

Action + Action Input — tool name and args as plain text.

Observation — whatever your Python function returned.

Final Answer — user-facing reply when the loop stops.

The pattern comes from the ReAct paper. LangChain ships a copy of the prompt in the docs.

One lookup_html_tag call:

Thought: need the <a> entry
Action: lookup_html_tag
Action Input: a
Observation: <a>: Creates a hyperlink…
Final Answer: …
Plain text from the model. LangChain parses the lines and runs your function.

Prompt template

ReAct is mostly this block pasted into a PromptTemplate. Watch {agent_scratchpad} — LangChain drops previous Thought/Action/Observation lines there between passes.

Use the following format:

Question: the input question you must answer
Thought: you should always think about what to do
Action: the action to take, should be one of [{tool_names}]
Action Input: the input to the action
Observation: the result of the action
... (Thought/Action/Action Input/Observation can repeat)
Thought: I now know the final answer
Final Answer: the final answer to the original input question

Question: {input}
Thought:{agent_scratchpad}

For new work, stick with create_agent. ReAct is handy when you need the steps printed or you are reading code from 2023.

Tool Calling

create_agent
tool_calls JSON
hidden steps

ReAct

create_react_agent
Thought / Action /
Observation in output
fixed prompt template
Same lookup_html_tag tool. Different way of asking the model to pick it.

create_react_agent

Import from langchain_classic, not the main agents package. Pass ChatOpenAI, your tools, and the template. AgentExecutor calls lookup_html_tag when the model names it in Action.

from langchain_classic.agents import AgentExecutor, create_react_agent
from langchain_core.prompts import PromptTemplate
from langchain_openai import ChatOpenAI

prompt = PromptTemplate.from_template(REACT_PROMPT)
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)

agent = create_react_agent(llm, [lookup_html_tag], prompt)
executor = AgentExecutor(agent=agent, tools=[lookup_html_tag], verbose=True)

result = executor.invoke({"input": "What does the <a> tag do?"})
print(result["output"])

Run the demo

Venv from Project Setup. One extra package:

pip install langchain-classic

Download, then:

react_framework_demo.py

One <a> question, verbose on

OPENAI_API_KEY in .env. Needs langchain-classic below.
react_framework_demo.py
"""react_framework_demo.py"""
from langchain_classic.agents import create_react_agent
# lookup_html_tag from Tool Calling
executor = AgentExecutor(…, verbose=True)
python react_framework_demo.py
PowerShell — (.venv) active
(.venv) PS C:\projects\langchain-course> python react_framework_demo.py
Question: What does the <a> tag do?
> Entering new AgentExecutor chain...
Thought: I should look up the <a> tag.
Action: lookup_html_tag
Action Input: a
Observation: <a>: Creates a hyperlink. Use href for the URL.
Thought: I now know the final answer
Final Answer: The <a> tag creates a hyperlink.
> Finished chain.
Final: The <a> tag creates a hyperlink.
Turn on verbose=True to see each line in the shell.

create_react_agent in the reference.

What's Next

Next: Agent Execution Flow.