This took me longer than I expected to get organized in my head.
Start by defining the agent state as a growing trajectory of Thought/Action/Observation triples, then present a clean loop that builds the prompt from that trajectory, calls the LLM, parses the output, executes tools, and appends results. Emphasize termination conditions (final answer or step budget) and briefly discuss trade-offs like prompt length and error handling.
Pro tip: Mention that you would include the full trajectory in the prompt each turn to maintain context, but also discuss truncation or summarization strategies for long trajectories to avoid token limits—this shows awareness of real-world constraints.
Initialize a list to hold the trajectory (e.g., thoughts, actions, observations) and set a step counter. The trajectory will be the source for constructing the prompt each turn.
Build a prompt that includes the task description, the ReAct format instructions, and the entire trajectory so far. This gives the LLM the full context to decide the next thought/action.
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 produced, return it and exit. If the step budget is exceeded, return a failure or the best answer so far. Otherwise, increment the step 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.