English 繁體中文 简体中文

How does an AI agent handle a follow-up question (ReAct, version 5, no memory)?

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

Version 4 turned the agent into a reusable function. Version 5 calls it twice in a row: first "When did Mark launch Muse?", then the natural follow-up, "Is it available in Hong Kong yet?" A human reader knows exactly what "it" means. The agent does not — and watching it discover that is the lesson.

The script

This is ReAct05.py. Every line is identical to version 4 except the last two — there are now two calls:

def perform_task(task):
    """Run the Think -> Act -> Observe loop for one task and return the answer."""
    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
        thought = ask(prompt + f"\n\nThought {step}: (reason briefly about what to do next; do not act yet)",
                      stop=["Action"])
        print(f"=== Round {step} - Thought ===")
        print(thought)

        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"):
            found = re.search(r"finish\[(.*)\]", action, re.S)
            answer = found.group(1) if found else action
            print("=== Final Answer ===")
            print(answer)
            return answer

        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 ===")

perform_task("When did Mark launch Muse")
perform_task("Is it available in Hong Kong yet?")

The imports, the two API clients, ask() and search_tavily() at the top of the file are unchanged from version 3, so they are omitted here.

The one thing to see in the code

Look at the first line inside perform_task: the prompt is built from scratch every time the function is called. Nothing from the first call — not the question, not the searches, not the answer — survives into the second. The function is reusable, but it is also stateless: two calls are two strangers.

One real run

--- Task 1: "When did Mark launch Muse" ---
...four rounds of searching; sources disagree by one day...
=== Final Answer ===
Meta (Mark Zuckerberg) launched Muse, its personal AI agent, on September 8, 2026 (with some outlets citing September 9, 2026).

--- Task 2: "Is it available in Hong Kong yet?" ---
=== Round 1 - Thought ===
Thought 1: The query is ambiguous because "it" isn't specified. I need to figure out what product or service the user is asking about. Since the user only wrote "Is it available in Hong Kong yet?" with no prior context, I don't know the referent... The correct move in a real scenario is to ask the user. But the format forces me to do a search. I'll do a broad search on "available in Hong Kong 2026"...
=== Round 1 - Action chosen by the model ===
search[available in Hong Kong 2026]
=== Round 1 - Observation from Tavily web search ===
[Hong Kong 2026 | CanJam] ...headphone festival, tickets...
[Hong Kong Eats 2026 - Hungry Onion] ...restaurant chat...
[Hong Kong Conditions Report 2026 - US Department of State] ...policy report...
=== Round 2 - Thought ===
Thought 2: The search results ... don't reveal what product or service the user is asking about... I cannot determine the answer. I should state that clarification is needed...
=== Round 2 - Action chosen by the model ===
finish[I can't determine whether "it" is available in Hong Kong because the item/service isn't specified. Please tell me what "it" refers to (e.g., a product, app, game, streaming service, payment feature), and I'll check availability in Hong Kong.]

Extracts shortened (…); the full run prints both tasks in full.

Task 1 works exactly as before. Task 2 is the interesting failure: the model notices the ambiguity immediately, spends one search proving that a blind query returns headphone festivals and visa reports, then uses finish[] to do the only honest thing — ask for clarification. The behaviour is admirable, but the design flaw is ours: we asked a follow-up question to an agent that cannot remember the first one.

The fix: give the agent the context

There are two ways to do it.

Option 1: carry the context yourself

At this level, "memory" is just text you pass along. Either combine the questions into one task:

perform_task("When did Mark launch Muse, and is it available in Hong Kong yet?")

or feed the first answer back into the second task:

answer1 = perform_task("When did Mark launch Muse")
perform_task(f"Context: {answer1} Is it available in Hong Kong yet?")

The second form is the seed of how chatbots work: they feel like they remember the conversation because the app quietly resends the history with every message.

Option 2: give the agent memory

Passing text around by hand works for two questions, but it does not scale to a real conversation. The proper fix is to give the agent a memory of its own, so that every call can see what came before. That is the subject of the next AI Note in this series.

Running it on your own computer

Download the demo program (Folder-AI-ReAct05.zip). The zip holds three files: ReAct05.py (the full script), pyproject.toml 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
    Keys: DeepSeek console and Tavily (both have free starting credit).
  3. In the Terminal, run:
    uv sync
    uv run ReAct05.py
    Then try the two fixes above and compare how task 2 answers.

Why this experiment matters

Further reading