方寸 Portal

AI Engineer Portal

Your personal operating system for career transition.

Private mode

Exercise

Implement a ReAct reasoning loop

Implement a ReAct Reasoning Loop

The ReAct (Reasoning + Acting) pattern is the most widely used agent loop in production. The model alternates between thinking (reasoning about what to do next), acting (calling a tool), and observing (reading the tool result) until it can produce a final answer.

What you are building

Create a react_loop function that:

  1. Takes a user question and a dict of available tools.
  2. Sends the question to a (simulated) LLM that returns structured steps.
  3. Parses each step as either a Thought, a Tool Call, or a Final Answer.
  4. Executes tool calls and feeds observations back into the next iteration.
  5. Terminates when the model produces a Final Answer or an iteration cap is reached.
  6. Returns a structured trace of all reasoning steps plus the final answer.

Why this matters

Understanding the ReAct loop from scratch means you can debug agent stalls (infinite loops), optimize token usage (trimming intermediate context), and add features like step-level logging or human-in-the-loop approval. Every agent framework wraps this pattern; knowing the internals makes you dangerous.

Constraints

  • Simulate the LLM with a provided mock_llm callable so the exercise is self-contained.
  • The iteration cap should default to 10 and be configurable.
  • Each trace entry should include: step number, type (thought/action/observation/answer), and content.
  • If the cap is reached without a final answer, return the trace with a timeout indicator.

Agents / intermediate / Step 2 of 8

Practice stage

Agent architecture patterns

Hint

Focus on explicit control surfaces — function schemas, state machines, and structured outputs. Agents are most useful when the task requires dynamic tool selection or multi-step reasoning. Start with the simplest pattern (single tool call) before reaching for ReAct loops.

Success criteria
  • - Tools return structured, typed responses
  • - Agent completes the task within a bounded number of steps
  • - All tool calls include error handling and retries
  • - Memory/state management prevents unbounded context growth
Review checklist
  • - Tool schemas validate inputs and handle errors gracefully
  • - Agent loop has explicit termination conditions
  • - State is serializable and inspectable between steps
  • - Cost and token usage are tracked per invocation
  • - Fallback behavior exists for tool call failures

Practice

Generate a variation

Generate a new exercise variation to deepen understanding or practice a related concept.

Attempt history

Recent submissions

Before you submit, decide what a strong answer should make obvious to the reviewer.

No attempts yet.