English 繁體中文 简体中文

How does an AI agent research a question (ReAct, version 3, web search in a loop)?

Howard Pong Yuen Lam · AI Notes · November 9, 2026 · 5 min read

The previous note asked a model with no tools: “When did Mark launch Muse?” It thought carefully and answered, honestly, “I don't know.” This note asks the same question and adds one tool: a web search, provided by Tavily. The agent can now repeat the think → act → observe loop — up to five rounds — and stop only when it decides it knows the answer.

The script

This is ReAct03.py as it stands:

# Same ReAct idea as react01.py, but this task needs MORE THAN ONE action,
# so we repeat Think -> Act -> Observe in a loop until the model says "finish".
# Step 1: THINK  -> ask the model to reason about what to do next
# Step 2: ACT    -> ask for ONE action: search[...] or finish[...]
# Step 3: OBSERVE-> run the Tavily web search (a real tool), add the result to the prompt, loop again
import os
import re
from datetime import datetime
from tavily import TavilyClient
from openai import OpenAI
from dotenv import load_dotenv
load_dotenv()

client = OpenAI(
    api_key=os.environ.get('DEEPSEEK_API_KEY'),
    base_url="https://api.deepseek.com")
tavily = TavilyClient(api_key=os.environ.get('TAVILY_API_KEY'))

def ask(prompt, stop=None):
    """Send one prompt to the model and return its text reply."""
    response = client.chat.completions.create(
        model="deepseek-flash",
        messages=[{"role": "user", "content": prompt}],
        stop=stop,   # stop writing when this text appears
    )
    return response.choices[0].message.content

# The only tool (action) our agent can use
def search_tavily(query):
    """Return the top web results from Tavily (title, url, short content)."""
    results = tavily.search(query=query, max_results=3)["results"]
    if not results:
        return f"No web results found for '{query}'."
    return "\n".join(f"[{r['title']}] ({r['url']}) {r['content'][:400]}"
                     for r in results)

task = ("When did Mark launch Muse")

prompt = f"""Task: {task}
You can use ONE tool: search[query] searches the web (Tavily) and returns the top results.
Today is {datetime.now().strftime('%Y-%m-%d')}.
Work in steps. Each step has a Thought, an Action, and an Observation."""

for step in range(1, 6):          # at most 5 rounds, so it cannot loop forever
    # ---- Step 1: THINK (Reasoning) ----
    thought = ask(prompt + f"\n\nThought {step}: (reason briefly about what to do next; do not act yet)",
                  stop=["Action"])   # stop the model inventing its own Action/Observation
    print(f"=== Round {step} - Thought ===")
    print(thought)

    # ---- Step 2: ACT — append the reasoning trace, then ask for an action ----
    prompt = prompt + f"\n\nThought {step}: {thought}"
    action = ask(prompt + "\n\nWhat action should be taken based on that? "
                 "Reply with ONLY: search[query] or finish[answer]").strip()
    print(f"=== Round {step} - Action chosen by the model ===")
    print(action)

    if action.startswith("finish"):
        answer = re.search(r"finish\[(.*)\]", action, re.S)
        print("=== Final Answer ===")
        print(answer.group(1) if answer else action)
        break

    # ---- Step 3: OBSERVE — our Python code (not the model) runs the tool ----
    query = re.search(r"search\[(.*)\]", action, re.S)
    observation = search_tavily(query.group(1)) if query else "Invalid action."
    print(f"=== Round {step} - Observation from Tavily web search ===")
    print(observation)
    prompt = prompt + f"\nAction {step}: {action}\nObservation {step}: {observation}"
else:
    print("=== Stopped: no answer after 5 rounds ===")

What each part does

One real run

Four rounds. Watch how round 2 notices that two sources disagree, and how the agent keeps searching until the dates line up:

=== Round 1 - Thought ===
Thought 1: I need to find out when Mark (likely Mark Zuckerberg, given "Muse") launched something called Muse. I should search the web for this, but I need to be careful—"Muse" could refer to different things (e.g., a Meta product, an AI model, or a project by another Mark). I'll start with a targeted search query combining "Mark" and "Muse launch" to get relevant results.
=== Round 1 - Action chosen by the model ===
search[Mark Muse launch date]
=== Round 1 - Observation from Tavily web search ===
[Meta Muse in the UK, Europe and Australia: Where It Stands in October 2026] (https://medium.com/@mateohq/meta-muse-in-the-uk-europe-and-australia-where-it-stands-in-october-2026-3342eb499e81) ...Status: not available, no date announced...
[Meta launches Muse for Small Business as Zuckerberg pushes beyond consumer AI market] (https://www.cnbc.com/2026/09/29/meta-launches-muse-for-small-business-zuckerberg-pushes-enterprise-ai.html) ...Published Tue, Sep 29 2026...
[Everything new coming to Meta's AI agent Muse | TechCrunch] (https://techcrunch.com/2026/09/23/everything-new-coming-to-metas-ai-agent-muse) Muse, which launched in early September, interacts across a user's devices and digital accounts...
=== Round 2 - Thought ===
Thought 2: The search results give conflicting or unclear timing: one CNBC article says Meta launched Muse for Small Business on September 29, 2026, while a TechCrunch article says Muse "launched in early September." I need to pin down the original launch date of Muse itself, not just the small-business version. I should search more specifically...
=== Round 2 - Action chosen by the model ===
search[Meta launches Muse AI agent date early September 2026]
=== Round 2 - Observation from Tavily web search ===
[Meta Muse Launches: AI Agent That Shops, Books and Negotiates] (https://www.how2shout.com/ai/meta-muse-personal-ai-agent-launch.html) Meta launched Muse on Tuesday (September 8, 2026), a personal AI agent that books travel, sends email, shops, and negotiates on your behalf...
[Instagram] (https://www.instagram.com/p/Dd_SU5yEbfg) Meta announced Muse on September 8, 2026, describing it as a personal AI agent that can take action—not just answer questions...
[Meta Muse is a secure, personal AI... - Tipp City RoadDog] (https://www.facebook.com/TippCityRoadDog/posts/...) ...Launched in September 2026, it shifts the paradigm from a traditional chatbot...
=== Round 3 - Thought ===
Thought 3: I now have converging evidence that Meta's Muse AI agent launched on September 8, 2026, with a small-business version following on September 29, 2026. The question asks "When did Mark launch Muse" — this almost certainly refers to Mark Zuckerberg and Meta's Muse. The best answer is the original launch date: September 8, 2026. I should do one more search to confirm...
=== Round 3 - Action chosen by the model ===
search[Meta Muse official launch September 8 2026 Zuckerberg announcement]
=== Round 3 - Observation from Tavily web search ===
[Zuckerberg Is Back (And It's Not the Model) - Meta Muse Launch] (https://www.youtube.com/watch?v=-_ovL8w1TMM) On 8 September, Meta launched Muse: a personal AI agent with its own computer and its own wallet...
[Meta Enterprise Platform: What Zuckerberg Announced | CellCog] (https://cellcog.ai/blog/meta-enterprise-platform) On September 28, 2026 ... Mark Zuckerberg announced Meta Enterprise Platform...
[Meta launches Muse for Small Business ... CNBC] (https://www.cnbc.com/2026/09/29/meta-launches-muse-for-small-business-zuckerberg-pushes-enterprise-ai.html) ...
=== Round 4 - Thought ===
Thought 4: The evidence is now consistent and converging. Meta (Mark Zuckerberg's company) launched the Muse personal AI agent on September 8, 2026, with the US-only rollout on iOS, Android, and muse.ai. A small-business/enterprise version followed later in September (announced September 28–29, 2026). The question "When did Mark launch Muse" is best answered with the original consumer launch date. I have enough to finalize.
=== Round 4 - Action chosen by the model ===
finish[Mark Zuckerberg's Meta launched Muse on September 8, 2026. It was a US-only personal AI agent rollout across iOS, Android, and muse.ai. A small-business/enterprise version followed around September 28–29, 2026.]
=== Final Answer ===
Mark Zuckerberg's Meta launched Muse on September 8, 2026. It was a US-only personal AI agent rollout across iOS, Android, and muse.ai. A small-business/enterprise version followed around September 28–29, 2026.

Extracts shortened (…) for readability; the full run prints the first 400 characters of each result.

Compare this with the no-tools answer from the previous note. The question was identical; what changed is that reasoning now has something to work with. The interesting reasoning happens in round 2: the model reads three results, spots that CNBC and TechCrunch disagree about the date, and writes a sharper query to settle it. That judgment call is the part no tool can do for it.

Running it on your own computer

Download the demo program (Folder-AI-ReAct03.zip). The zip holds three files: ReAct03.py (the script above), pyproject.toml (the package list) and README.md.

  1. Open the folder in Visual Studio Code.
  2. Create a file named .env in the folder with two lines:
    DEEPSEEK_API_KEY=your-deepseek-key
    TAVILY_API_KEY=your-tavily-key
    Get the first from the DeepSeek console and the second from Tavily; both have free starting credit.
  3. In the Terminal, run:
    uv sync
    uv run ReAct03.py
    uv sync is needed only the first time; it reads pyproject.toml and installs openai, tavily-python and python-dotenv into the project.

Why this run matters

Further reading