ReAct · Tool Chains · Memory✓ Mathematical
◆ The PatternAutonomous multi-step reasoning with tools and memory
An agent is an LLM that reasons about what to do, takes actions (tool calls), observes results, and iterates. The ReAct pattern (Reason + Act) interleaves thinking and tool use: Thought → Action → Observation → Thought → ...
ReAct loop: Thought → Action(tool, args) → Observation → Thought → ... → Final Answer
Each iteration: reason about what's needed, call a tool, process the result, decide next step.
Planning: Decompose task → subtasks → execute sequentially or in parallel
Complex tasks need planning: break "book a trip" into search flights, compare prices, book, confirm.
Memory types: (1) Short-term — conversation history in context window. (2) Long-term — vector store of past interactions, retrieved as needed. (3) Working memory — scratchpad for current task state. Key challenge: agents can be unreliable — they get stuck in loops, hallucinate tool calls, or lose track of the plan.
The best agent systems use simple, constrained loops — not complex multi-agent frameworks. A single ReAct loop with 3–5 well-designed tools beats a graph of 10 specialized agents for most tasks.
Interactive — ReAct agent loop
Python — ReAct agent#
def react_agent(query, tools, max_steps=5):
messages = [
{"role": "system", "content":
"You are a helpful agent. Use tools to answer questions. "
"Think step by step."},
{"role": "user", "content": query}
]
for step in range(max_steps):
response = client.chat.completions.create(
model="gpt-4o", messages=messages, tools=tools)
msg = response.choices[0].message
if msg.tool_calls:
messages.append(msg)
for call in msg.tool_calls:
result = execute_tool(call.function.name,
json.loads(call.function.arguments))
messages.append({
"role": "tool",
"tool_call_id": call.id,
"content": str(result)
})
else:
return msg.content # final answer
return "Max steps reached"Pattern bridge: LLMs planning, tool-using, and looping is optimization made autonomous — each step refines the next. In markets, the sentiment cycle is an agent loop: observe, decide, act, observe again.