agentleFS
lauren-frameworklauren-framework/lauren-ai/skills/react-agent-loop/SKILL.md

react-agent-loop

Skill0 starsChanged 6 months agoMITView on GitHub

Implements the ReAct (Reason + Act) agentic loop where the model alternates between thinking, calling tools, observing results, and producing a final answer. Use when building an agent that must iteratively use tools over multiple turns before arriving at a response.

The file

Files
SKILL.md

Use codemap find "SymbolName" to locate any symbol before reading — it gives exact file + line range and is faster than grep across the whole repo.

ReAct Agent Loop (Reason + Act)

How the loop works

User message
  → Think (LLM reasoning, optional tool call)
  → Act   (tool execution if stop_reason == "tool_use")
  → Observe (tool result injected as next message)
  → Think (LLM sees tool result, decides next action)
  → ...
  → Final answer (stop_reason == "end_turn")

AgentRunnerBase implements this loop automatically. Each iteration is one "turn". The loop ends when:

  • stop_reason == "end_turn" (model is done)
  • turn == max_turns → raises AgentMaxTurnsError
  • Cost exceeds max_cost_usd → raises AgentBudgetExceededError

Quick start

# agent.py — from __future__ import annotations IS safe here
from __future__ import annotations

from lauren_ai import agent, use_tools
from .tools import search_web, run_python

@agent(
    model="claude-opus-4-6",
    system="""You are a research assistant.
Think through problems step-by-step.
Use tools to gather information before answering.""",
    max_turns=10,
)
@use_tools(search_web, run_python)
class ReActAgent: ...

Lifecycle hooks for observability

@agent(model="claude-opus-4-6", max_turns=10)
@use_tools(search_web)
class ObservableAgent:
    async def on_start(self, ctx):
        print(f"Starting run {ctx.run_id} — message: {ctx.message}")

    async def on_turn_complete(self, completion, ctx):
        print(f"Turn {ctx.turn}: stop_reason={completion.stop_reason}")

    async def on_tool_result(self, result, ctx):
        print(f"Tool result: {result.content[:80]}")

    async def on_finish(self, response, ctx):
        print(f"Finished in {response.turns} turns")

Inspecting turn count

resp = await runner.run(ReActAgent(), "Research quantum computing trends")
print(f"Used {resp.turns} turns")
print(f"Tools called: {[t.name for t in resp.tool_calls_made]}")
print(f"Cost: ${resp.total_usage.cost_usd('claude-opus-4-6'):.4f}")

Testing the ReAct loop

from lauren_ai._transport._mock import MockTransport

mock = MockTransport()
# Turn 1: think → tool call
mock.queue_tool_use("search_web", {"query": "quantum computing"})
# Turn 2: observe → think → tool call
mock.queue_tool_use("run_python", {"code": "print(42)"})
# Turn 3: observe → final answer
mock.queue_response(Completion(
    id="c3", model="mock-model",
    content="Quantum computing is advancing rapidly. 42.",
    tool_calls=[], stop_reason="end_turn",
    usage=TokenUsage(input_tokens=50, output_tokens=20),
))
# Runner drives the loop automatically
resp = await runner.run(ReActAgent(), "Tell me about quantum computing")
assert resp.turns == 3

Reference files

FileContents
src/lauren_ai/_agents/_runner.pyAgentRunnerBase — the full ReAct loop
src/lauren_ai/_agents/__init__.pyAgentContext — ctx.turn, ctx.run_id
src/lauren_ai/_exceptions.pyAgentMaxTurnsError, AgentBudgetExceededError

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.