liveBook · Manning

ReAct

As explained in Designing AI Systems

An agent loop is an iterative execution pattern in which a model selects tools, observes their results, and continues reasoning until it produces a final response or reaches a stopping condition.

ReAct is an agent loop that alternates reasoning and action: a model requests tools, observes their results, and continues until it produces a final response or reaches an iteration limit. Unlike a single request-response call, the workflow can make several model and tool calls before answering. section 10.6 section 10.9

A production-oriented workflow surrounds the loop with checks and service calls:

  • Validate the input, load the session and memory, and assemble context before calling the model. section 10.9
  • If the model requests tools, validate each tool’s arguments, execute the calls, and feed their results back into another model call. section 10.9
  • Stop on a final text response or the iteration limit; filter the output, store the exchange, and return the response. section 10.9

Actions can change external state, so tool registration carries operational constraints. Credential references keep secrets out of application code; rate limits constrain calls; and read-only and idempotency metadata inform guardrail decisions. A blocked action can be returned to the model as a tool result, allowing it to continue gracefully. section 10.6 section 10.14

Assistant with action tools

A personal assistant registers calendar-event creation, ticket creation, and web search. Calendar and ticket creation are marked non-read-only and non-idempotent: repeating either action creates another meeting or ticket. section 10.6

Listing 10.10 The agent loop: reasoning, acting, and observing
def run_agent_loop(
    message: str,
    session_id: str,
    user_id: str,
    max_iterations: int = 5    #A
) -> str:
    """Execute the agent loop: reason, act, observe, repeat."""
    model_tools, tool_names = (    #B
        platform.tools.build_model_tools(    #B
            namespace="claw"    #B
        )    #B
    )    #B
 
    system_prompt = platform.models.get_prompt("claw-assistant")
    user_memory = platform.sessions.get_memory(user_id=user_id)
    memory_context = "\n".join(
        [f"- {key}: {value}" for key, value in user_memory.items()]
    )
    relevant_docs = platform.data.search(
        index_name="company-knowledge",
        query=message,
        top_k=5,
        score_threshold=0.7
    )
    doc_context = "\n\n".join(
        [f"[Source: {doc.document_id}]\n{doc.text}"
         for doc in relevant_docs]
    )
    history, _ = platform.sessions.get_messages(
        session_id=session_id, limit=50
    )
    messages = [
        {"role": "system",
         "content": system_prompt["content"]},
        {"role": "system",
         "content": (
             f"User memory:\n{memory_context}\n\n"
             f"Retrieved documents:\n{doc_context}"
         )},
        *[{"role": m.role, "content": m.content}
          for m in history],
        {"role": "user", "content": message}
    ]
 
    for iteration in range(max_iterations):
        response = platform.models.chat(
            model="gpt-4o",
            messages=messages,
            tools=model_tools    #C
        )
        if not response.tool_calls:    #D
            return response.content    #D
        messages.append({    #E
            "role": "assistant",    #E
            "content": response.content,    #E
            "tool_calls": response.tool_calls    #E
        })    #E
 
        for tool_call in response.tool_calls:    #F
            name = tool_names[    #F
                tool_call["function"]["name"]    #F
            ]    #F
            args = json.loads(    #F
                tool_call["function"]["arguments"]    #F
            )    #F
            result = platform.tools.execute(    #G
                tool_name=name, arguments=args,    #G
                session_id=session_id    #G
            )    #G
            messages.append({
                "role": "tool",
                "content": str(result.result),
                "tool_call_id": tool_call["id"]
            })
 
    return response.content    #H

Inspect the Python loop and its setup for tool discovery, memory, and knowledge retrieval. section 10.6

For streaming, each iteration uses a streaming model call. Content tokens are sent incrementally, while tool-call deltas are buffered until the turn ends; requested tools are then executed before the next iteration. Incremental output filtering buffers short spans before release, trading a small increase in time-to-first-token for protection against exposing unfiltered content. section 10.9.1

Listing 10.17 Streaming variant of the Claw workflow: only changes from listing 10.16
@workflow(name="claw-assistant", api_path="/claw/chat",
          response_mode="stream", ...)    #A
def claw_assistant(message, user_id, session_id=None):
    # Steps 1-3 (input validation, session load, context assembly)
    # are identical to Listing 10.16
 
    model_tools, tool_names = (
        platform.tools.build_model_tools(
            namespace="claw"
        )
    )
    max_iterations = 5
 
    def token_stream():    #B
        for iteration in range(max_iterations):
            content_buffer = ""
            tool_calls = []
            finish_reason = None
            stream = platform.models.chat_stream(    #C
                model="gpt-4o", messages=messages,    #C
                tools=model_tools    #C
            )    #C
            for chunk in stream:
                if chunk.token:    #D
                    content_buffer += chunk.token    #D
                    yield {"type": "token",    #D
                           "value": chunk.token}    #D
                if chunk.tool_call_delta:    #E
                    _merge_tool_call_delta(    #E
                        tool_calls, chunk.tool_call_delta    #E
                    )    #E
                if chunk.finish_reason:
                    finish_reason = chunk.finish_reason
            if finish_reason == "stop":
                return
            messages.append({
                "role": "assistant",
                "content": content_buffer,
                "tool_calls": tool_calls
            })
            for tool_call in tool_calls:
                yield {"type": "status",
                       "value": (f"Calling "
                                 f"{tool_call['function']['name']}...")}
            # Tool validation and execution: identical to Listing 10.16
 
    for event in platform.guardrails.filter_output_stream(    #F
        event_stream=token_stream(),    #F
        filters=["claw-output-safety"]    #F
    ):    #F
        yield event    #F
    # Step 6 (store the exchange) is identical to Listing 10.16
    yield {"done": True, "session_id": session.session_id}

Compare the streaming workflow changes with the synchronous loop. section 10.9.1

How Designing AI Systems covers it

How deep it goes
Works through tool registration, bounded iteration, safety checks, and a streaming variant within a complete workflow.
Examples
Uses a personal assistant that creates calendar events and tickets and searches the web.
What you can do after
Apply it via platform model, tool, session, data, and guardrail services to build an assistant workflow.
How it connects
Connects tool calling with context engineering, guardrails, session memory, and knowledge retrieval.

Also explained in

Compare how 15 books explain ReAct →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Designing AI Systems. The section links point to the parts of the book it draws on.