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 → ...
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.
Python — ReAct agent#
If each step of an agent is right 95% of the time, a 5-step task succeeds 77% of the time, 10 steps 60% and 20 steps 36%. Checking each step and retrying once on failure lifts 20 steps to 95%. Reliability compounds, so agents are built around verification, not just more reasoning.
- The retry calculation assumes failures can be detected; silent errors compound unchecked.
- Agent benchmarks often ignore cost and over-fit to the benchmark (Kapoor et al. 2024); compare against simple baselines.
- Every tool an agent can call widens what can go wrong; give it the least access that works.
chain = {steps: round(0.95 ** steps, 3) for steps in (5, 10, 20)} # each step right 95% of the time
retry = round((1 - 0.05 ** 2) ** 20, 3) # 20 steps, each checked and retried once on failureAssumes import numpy as np. Configurations named after real models use their published shapes; other numbers are made up or simulated, as the comments say.
The same idea with a library
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"- S. Yao et al., “ReAct: Synergizing Reasoning and Acting in Language Models”, ICLR, 2023
- N. Shinn et al., “Reflexion: Language Agents with Verbal Reinforcement Learning”, NeurIPS, 2023
- S. Kapoor, B. Stroebl, Z. S. Siegel, N. Nadgir & A. Narayanan, “AI Agents That Matter”, arXiv:2407.01502, 2024
Last reviewed October 2026 · How this site checks its content · Report a mistake