This one took me a second to get my footing.
Start by outlining the core ReAct loop structure: initialize the trajectory, then iteratively construct a prompt from the trajectory, call the LLM, parse the output for Thought/Action, execute the action, append the Observation, and check for termination. Use clear pseudocode with comments to show how the prompt is built and how the trajectory grows, and explicitly handle both final answer and step budget termination.
Pro tip: Mention that you'd cap the trajectory length or summarize past steps to avoid exceeding the LLM's context window, and log each step for debugging—this shows production awareness beyond the basic algorithm.
Create an empty list to store the trajectory (Thought/Action/Observation triples) and set a step counter to 0. Define the maximum step budget.
Build the LLM prompt by concatenating the system instructions, the user query, and all previous steps in the trajectory. Format it so the LLM can continue the pattern.
Send the prompt to the LLM and parse the response to extract either a Thought followed by an Action, or a Final Answer. Handle parsing errors gracefully.
If an Action is present, execute the corresponding tool with the given input, capture the output, and append the Thought, Action, and Observation to the trajectory.
If a Final Answer is parsed, return it and exit. If the step counter reaches the budget, return a failure or the best answer so far. Otherwise, increment the counter and loop back to step 2.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.