Agent Loop guide

ReAct loop interview problems and implementation patterns

The ReAct loop is a compact way to test whether an engineer understands model-driven control flow. A reliable implementation must parse actions, dispatch only known tools, feed observations back into state, and stop under explicit conditions.

What to take into an interview

  • Represent each loop transition explicitly so failures can be traced.
  • Check terminal output and action validity before executing a tool.
  • Use budgets and duplicate-action controls to prevent runaway behavior.

Model the loop as a state machine

A ReAct loop alternates between model decisions and application-controlled execution. The application owns the step counter, available tools, accumulated observations, and terminal result. The model only proposes the next action or final answer.

This separation is a frequent interview focus because it reveals where validation belongs. Treat the model response as untrusted input and convert it into a known state transition before any external call.

Parse and route actions safely

Free-form parsing is acceptable for a small exercise, but production systems usually prefer structured output with a schema. In either case, reject missing tool names, unknown tools, malformed arguments, and values outside the tool contract.

  • Keep the tool registry allowlisted and separate from model text.
  • Return validation errors as observations the model can reason about.
  • Record the proposed action, validated arguments, and tool result.
  • Require confirmation before consequential or irreversible actions.

Recover without looping forever

Tool failures should become structured observations rather than uncaught exceptions that erase the trace. Distinguish transient failures, which may justify a bounded retry, from invalid requests, which should return corrective feedback to the model.

Always enforce a maximum number of model steps. Consider additional controls for repeated identical actions, unchanged state, cumulative cost, or elapsed time. A clear exhausted-budget result is better than a silent infinite loop.

Test the trajectory, not only the answer

A final answer can hide a poor trajectory. Test that the expected tool was called, arguments were valid, observations were appended in order, and the loop stopped at the correct point. Include malformed actions, unknown tools, failures, and no-final-answer cases.

During an interview, narrate the invariant for each test. This demonstrates that your implementation is intentional rather than a collection of conditionals that happen to satisfy one example.

Hands-on practice

Related coding problems

Build the foundation

Related curriculum tracks

Common questions

Frequently asked questions

What does ReAct mean in AI agents?

ReAct combines reasoning and acting: the model proposes an action, the application executes it, the result becomes an observation, and the cycle repeats until a terminal answer.

Should a ReAct loop expose chain-of-thought?

No. Systems can log concise action rationales and structured decisions without requesting or exposing private chain-of-thought. Focus observability on inputs, actions, observations, and outcomes.

How should a ReAct loop stop?

Use an explicit final-answer state plus application-enforced limits such as maximum steps, elapsed time, cost, or repeated-action detection.

Practice next

Implement the loop before adding abstractions

Solve the minimal version first, then practice error recovery and multi-tool coordination so you can explain every state transition in an interview.

ReAct Loop Interview Problems for AI Engineers