Content not available for this pattern.
Build This Pattern
Copy this prompt and paste it into Claude Code, OpenCode, Codex, or Cursor to implement this pattern.
Build an autonomous AI agent with a ReAct (Reasoning + Acting) loop.
ROLE: You are an autonomous agent that follows a think-act-observe cycle to accomplish tasks using available tools, maintaining context across iterations.
CONSTRAINTS:
- Maximum 25 iterations per task; hard cap stops loop and returns partial progress
- Tool timeout: 15 seconds per tool call; configurable per tool
- Circuit breaker: after 3 consecutive failures, tool is marked unavailable
- Memory retention: agent must track all tool calls and observations across iterations
- Safety: agent cannot call tools that modify data without explicit user confirmation
TOOL CALLING:
- Use function calling for: think(context, available_tools[]), act(tool_name, parameters), observe(action_result), replan(context, history[]), get_agent_status(agent_id?)
- Each tool returns structured JSON with agent state and metadata
STRUCTURED OUTPUT:
- Thinking must return JSON: { agent_id: string, iteration: number, current_state: string, reasoning: string, selected_tool: string, tool_parameters: Record<string, any>, confidence: number }
- Action must return JSON: { agent_id: string, tool_name: string, parameters: Record<string, any>, result: any, status: 'success' | 'error' | 'timeout', latency_ms: number }
- Observation must return JSON: { agent_id: string, action_result: any, new_knowledge: string, progress_assessment: string, next_steps: string[] }
- Agent status must return JSON: { agent_id: string, iterations: number, tools_used: string[], tools_available: string[], current_goal: string, progress_percent: number, total_latency_ms: number }
CHAIN OF THOUGHT:
- Thinking: analyze current state → review history → select best tool → plan parameters
- Acting: validate parameters → execute tool → capture result → handle errors
- Observing: analyze result → update knowledge → assess progress → determine next step
- Replanning: if stuck → reconsider approach → select different tool → continue loop
FEW-SHOT EXAMPLES:
Iteration 1: { reasoning: 'User wants to find information about AI agents. Start with web search.', selected_tool: 'search_web', tool_parameters: { query: 'AI agents overview' } }
Observation: { new_knowledge: 'Found 5 relevant articles about AI agents', progress_assessment: 'Initial research complete', next_steps: ['Read top article', 'Extract key points'] }
Iteration 5: { reasoning: 'Have enough information to answer the question. Compile findings.', selected_tool: 'generate_response', tool_parameters: { prompt: 'Summarize findings about AI agents' } }
Observation: { new_knowledge: 'Generated comprehensive answer', progress_assessment: 'Task complete', next_steps: [] }
EVALUATION CRITERIA:
- Task completion: percentage of tasks that reach successful conclusion
- Iteration efficiency: average iterations per task completion
- Tool utilization: percentage of available tools that are effectively used
- Error recovery: percentage of tool failures that result in successful replanning
The system should: 1) Implement loop controller that manages think-act-observe cycle, 2) Agent has access to tools defined in registry with schemas, handlers, and access controls, 3) Each iteration: agent thinks about current state, decides action, executes tool call, observes result, updates plan, 4) Use context object to carry state across iterations, 5) Include memory system (tool call history plus observations) so agent tracks what it has tried, 6) On tool failure, log and let agent try alternative approach, 7) Implement circuit breaker per tool: if tool fails 3 consecutive times, mark unavailable, 8) Handle LLM parse errors (malformed tool call JSON) with retry, 9) Set max iterations limit (default 25) with hard cap that stops loop and returns partial progress, 10) Handle tool timeouts (15s per call, configurable), 11) Detect infinite loops by tracking repeated action-observation cycles without progress and force replan.