Picture this: the thinking loop
Every turn is the same six moves. ReAct is this loop written as Thought → Action → Observation.
1 Observeread the question
→
2 Thinkwhat is missing?
→
3 Planpick a next step
→
4 Actcall a tool or reply
→
5 See resultobservation
→
6 Learnupdate the plan
The notebook forces this shape with a PromptTemplate. There is no real Search tool yet — the model only writes the words.
Before you click Practice — Module 2 notebook
The Agent Loop you just saw (observe → think → act) is written as a ReAct prompt. The model is not calling a real search tool yet — it is learning the shape of Thought / Action / Observation / Final Answer.
Install→API key→ChatGroq→PromptTemplate | llm
Run install → key → create llm → ReAct chain. Read the printed Thought lines; that is the “thinking” this module is about.
Practice task 1
Install packages into this notebook’s Python
Run the first code cell. It uses sys.executable -m pip so packages go into Kodin’s Jupyter kernel, not a random Windows pip.
# Install into THIS notebook kernel (not Windows pip).
import sys
import subprocess
print("Installing into:", sys.executable)
subprocess.check_call([
sys.executable, "-m", "pip", "install", "-qU",
"langgraph", "langchain-groq", "langchain-core",
])
print("Done. Run the next cells.")
Expected: Prints the kernel path, then “Done.”
Why this cell: A plain !pip install on Windows often installs into a different Python, then the next cell fails with ModuleNotFoundError: langgraph. If import still fails: Kernel → Restart Kernel, then Run All.
Practice task 2
API key + MODEL + groq_text (same as Module 1)
Every practice notebook repeats this cell. Run it before any Groq call. Key stays in os.environ["GROQ_API_KEY"] for this kernel session.
import os
MODEL = "openai/gpt-oss-20b"
def groq_text(msg) -> str:
# GPT-OSS may return text, a list of parts, or reasoning_content
...
if not os.getenv("GROQ_API_KEY"):
os.environ["GROQ_API_KEY"] = input("Enter GROQ_API_KEY: ").strip()
print("API key ready. Model:", MODEL)
Expected: “API key ready. Model: openai/gpt-oss-20b”
Why this cell: Without this cell, ChatGroq has no key. Without groq_text, later prints can look empty even when the model answered.
Practice task 3
Create the LLM (brain only)
Same ChatGroq as Module 1. You will pipe a prompt template into this object.
from langchain_groq import ChatGroq
from langchain_core.prompts import PromptTemplate
llm = ChatGroq(model=MODEL, temperature=0)
print("Model ready:", MODEL)
Expected: “Model ready: openai/gpt-oss-20b”
Why this cell: Keeping the LLM in its own cell means if the next cell fails, you do not re-install packages. temperature=0 makes the ReAct format more consistent.
Practice task 4
ReAct prompt chain: Thought → Action → Final Answer
The template is a script the model must follow. {tools}, {tool_names}, and {input} are filled when you call chain.invoke(...). The | operator connects prompt → LLM.
react_prompt = PromptTemplate.from_template("""
Answer the following question as best you can. You have access to the following tools:
{tools}
Use the following format:
Question: ...
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: I now know the final answer
Final Answer: ...
Question: {input}
Thought:""")
chain = react_prompt | llm
response = chain.invoke({
"tools": "Search: useful for current events",
"tool_names": "Search",
"input": "Who is the current CEO of Microsoft?"
})
print(groq_text(response) or response)
PromptTemplate.from_template — reusable prompt with holes to fill.
chain = react_prompt | llm — LangChain LCEL: fill prompt, then call the model.
- Look for
Thought: in the output — that is the agent “thinking out loud.”
Expected: Printed text with Thought / Action / Final Answer (CEO of Microsoft).
Why this cell: This is prompted ReAct, not a real agent loop. There is no Python function named Search. The model writes “Action: Search” as text. Module 3 binds real tools; Module 5 runs the actual ReAct executor.