Bird
Raised Fist0
Prompt Engineering / GenAIml~10 mins

ReAct pattern in Prompt Engineering / GenAI - Interactive Code Practice

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to initialize the ReAct agent with the correct reasoning step.

Prompt Engineering / GenAI
agent = ReActAgent(reasoning_steps=[1])
Drag options to blanks, or click blank then click option'
A1
B3
C0
D5
Attempts:
3 left
💡 Hint
Common Mistakes
Setting reasoning_steps to 0 disables reasoning.
Using too many steps initially can complicate the agent's behavior.
2fill in blank
medium

Complete the code to add an observation after the agent takes an action.

Prompt Engineering / GenAI
agent.observe(action_output=[1])
Drag options to blanks, or click blank then click option'
Aobservation
Baction_result
Caction_output
Dresponse
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name that is not defined or unrelated.
Confusing 'observation' with the variable holding the action output.
3fill in blank
hard

Fix the error in the code to correctly perform the agent's think-act cycle.

Prompt Engineering / GenAI
thought = agent.think(input_text)
action = agent.act([1])
Drag options to blanks, or click blank then click option'
Aagent
Binput_text
Caction
Dthought
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the original input instead of the thought.
Passing the action variable before it is defined.
4fill in blank
hard

Fill both blanks to create a loop that runs the ReAct agent for 3 cycles.

Prompt Engineering / GenAI
for _ in range([1]):
    thought = agent.think(input_text)
    action = agent.act(thought)
    agent.observe([2])
Drag options to blanks, or click blank then click option'
A3
Baction
Cthought
Dinput_text
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'thought' instead of 'action' for observation.
Setting the loop count incorrectly.
5fill in blank
hard

Fill all three blanks to define a function that runs the ReAct agent for a given number of steps and returns the final thought.

Prompt Engineering / GenAI
def run_react_agent(agent, input_text, steps):
    for _ in range([1]):
        thought = agent.think(input_text)
        action = agent.act([2])
        agent.observe([3])
    return thought
Drag options to blanks, or click blank then click option'
Asteps
Bthought
Caction
Dinput_text
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'input_text' instead of 'thought' for the act method.
Observing 'thought' instead of 'action'.

Practice

(1/5)
1. What is the main purpose of the ReAct pattern in AI?
easy
A. To speed up AI training by skipping reasoning
B. To combine thinking and acting steps for better problem solving
C. To store large datasets efficiently
D. To replace human decision making completely

Solution

  1. Step 1: Understand the ReAct pattern concept

    The ReAct pattern mixes reasoning (thinking) and actions (doing) to solve problems step-by-step.
  2. Step 2: Identify the main goal

    This approach helps AI be more transparent and effective by breaking down tasks into Thought, Action, Observation, and Final Answer.
  3. Final Answer:

    To combine thinking and acting steps for better problem solving -> Option B
  4. Quick Check:

    ReAct = Reason + Act [OK]
Hint: Remember ReAct means think then do, step-by-step [OK]
Common Mistakes:
  • Thinking AI skips actions
  • ReAct stores data only
  • ReAct replaces humans fully
2. Which of the following shows the correct sequence in the ReAct pattern?
easy
A. Thought -> Action -> Observation -> Final Answer
B. Action -> Thought -> Final Answer -> Observation
C. Observation -> Final Answer -> Thought -> Action
D. Final Answer -> Thought -> Action -> Observation

Solution

  1. Step 1: Recall the ReAct step order

    The ReAct pattern follows a clear order: first the AI thinks (Thought), then acts (Action), then sees results (Observation), and finally gives the answer.
  2. Step 2: Match the correct sequence

    Thought -> Action -> Observation -> Final Answer correctly lists this order as Thought -> Action -> Observation -> Final Answer.
  3. Final Answer:

    Thought -> Action -> Observation -> Final Answer -> Option A
  4. Quick Check:

    Order = T -> A -> O -> FA [OK]
Hint: Think first, then act, observe, answer [OK]
Common Mistakes:
  • Mixing up Observation and Action order
  • Putting Final Answer before Observation
  • Skipping Thought step
3. Given this simplified ReAct code snippet:
thought = 'Check weather'
action = 'Query weather API'
observation = 'It is sunny'
final_answer = f"Weather is {observation}"
print(final_answer)

What will be the printed output?
medium
A. Check weather
B. It is sunny
C. Query weather API
D. Weather is It is sunny

Solution

  1. Step 1: Understand variable assignments

    The variable observation holds the string 'It is sunny'. The final_answer uses this to create 'Weather is It is sunny'.
  2. Step 2: Evaluate the print statement

    The print outputs the final_answer string, which is 'Weather is It is sunny' because the f-string inserts the full observation string.
  3. Final Answer:

    Weather is It is sunny -> Option D
  4. Quick Check:

    Output includes 'Weather is' + observation [OK]
Hint: Look at final_answer string formatting carefully [OK]
Common Mistakes:
  • Ignoring f-string variable insertion
  • Printing wrong variable
  • Confusing observation with action
4. Identify the error in this ReAct step code:
thought = 'Calculate sum'
action = 'Add 2 and 3'
observation = 2 + 3
final_answer = 'Sum is ' + observation
print(final_answer)
medium
A. Cannot concatenate string and integer directly
B. Missing action execution step
C. Observation should be a string, not a number
D. Final answer should be a number, not string

Solution

  1. Step 1: Analyze the final_answer concatenation

    The code tries to add a string 'Sum is ' and an integer observation (5) directly, which causes a TypeError in Python.
  2. Step 2: Identify the fix

    To fix, convert observation to string using str(observation) before concatenation.
  3. Final Answer:

    Cannot concatenate string and integer directly -> Option A
  4. Quick Check:

    String + int causes error [OK]
Hint: Convert numbers to strings before adding to text [OK]
Common Mistakes:
  • Ignoring type mismatch in concatenation
  • Thinking observation must be string always
  • Confusing action with observation
5. You want to build a ReAct-based AI assistant that solves math problems step-by-step. Which approach best applies the ReAct pattern?
hard
A. AI randomly guesses answers and checks correctness later
B. AI immediately gives the answer without intermediate steps
C. AI thinks about the problem, performs a calculation action, observes the result, then states the final answer
D. AI stores all previous answers without reasoning

Solution

  1. Step 1: Understand ReAct for stepwise problem solving

    The ReAct pattern requires the AI to think (reason), act (calculate), observe (check result), and then answer.
  2. Step 2: Match the approach to ReAct steps

    AI thinks about the problem, performs a calculation action, observes the result, then states the final answer describes this exact process, making the AI transparent and effective in solving math problems step-by-step.
  3. Final Answer:

    AI thinks about the problem, performs a calculation action, observes the result, then states the final answer -> Option C
  4. Quick Check:

    ReAct = Thought + Action + Observation + Answer [OK]
Hint: Follow Thought -> Action -> Observation -> Answer for stepwise AI [OK]
Common Mistakes:
  • Skipping reasoning steps
  • Guessing without observation
  • Ignoring stepwise transparency