7.2 Core Agent Loop
Perceive → Reason → Act → Reflect cycle
The Agent Loop Foundation
The core agent loop represents the fundamental cycle that drives intelligent agent behavior. This cyclical process enables agents to continuously interact with their environment, make decisions, and improve their performance over time.
Core Loop Components
The agent loop consists of four essential phases: Perceive (gather information), Reason (process and plan), Act (execute decisions), and Reflect (learn and improve). This cycle repeats continuously to maintain agent autonomy and effectiveness.
The Four Phases
👁️ Perceive
Environmental Sensing
- Gather sensory input from environment
- Process incoming messages and data
- Update knowledge base with new information
- Identify changes and opportunities
Input: Raw environmental data, messages, sensors
🧠 Reason
Decision Making & Planning
- Analyze perceived information
- Generate possible action plans
- Evaluate options against goals
- Select optimal course of action
Output: Action plan, decision rationale
⚡ Act
Action Execution
- Execute planned actions
- Use tools and resources
- Communicate with other agents
- Modify environment state
Result: Environmental changes, communications sent
🔄 Reflect
Learning & Improvement
- Evaluate action outcomes
- Update knowledge and beliefs
- Learn from successes and failures
- Adjust strategies for future cycles
Impact: Improved future decision making
Implementation Patterns
The core agent loop can be implemented in various ways depending on the agent framework and application requirements.
class CoreAgentLoop:
def __init__(self):
self.perception_module = PerceptionModule()
self.reasoning_module = ReasoningModule()
self.action_module = ActionModule()
self.reflection_module = ReflectionModule()
self.memory = AgentMemory()
def run_cycle(self):
"""Execute one complete agent loop cycle"""
# Phase 1: Perceive
perception_data = self.perception_module.perceive()
self.memory.store_perception(perception_data)
# Phase 2: Reason
action_plan = self.reasoning_module.reason(
perception_data,
self.memory.get_context()
)
# Phase 3: Act
action_result = self.action_module.execute(action_plan)
# Phase 4: Reflect
reflection = self.reflection_module.reflect(
perception_data,
action_plan,
action_result
)
# Update memory with complete cycle
self.memory.store_cycle(
perception_data,
action_plan,
action_result,
reflection
)
return action_result
class PerceptionModule:
def perceive(self):
"""Gather information from environment and internal state"""
return {
'environment_state': self.sense_environment(),
'internal_state': self.check_internal_status(),
'messages': self.receive_messages(),
'goals': self.get_current_goals()
}
class ReasoningModule:
def reason(self, perception, context):
"""Generate action plan based on perception and context"""
# Analyze current situation
situation_analysis = self.analyze_situation(perception, context)
# Generate possible actions
action_options = self.generate_options(situation_analysis)
# Evaluate and select best action
selected_action = self.evaluate_options(action_options, context)
return selected_action
class ActionModule:
def execute(self, action_plan):
"""Execute the planned action"""
try:
result = self.perform_action(action_plan)
return {'success': True, 'result': result}
except Exception as e:
return {'success': False, 'error': str(e)}
class ReflectionModule:
def reflect(self, perception, action_plan, action_result):
"""Learn from the cycle and update knowledge"""
# Evaluate action effectiveness
effectiveness = self.evaluate_effectiveness(
action_plan,
action_result
)
# Update beliefs and knowledge
knowledge_updates = self.update_knowledge(
perception,
action_result,
effectiveness
)
# Generate learning insights
insights = self.generate_insights(effectiveness, knowledge_updates)
return {
'effectiveness': effectiveness,
'knowledge_updates': knowledge_updates,
'insights': insights
}
Continuous Loop Operation
Agents typically run the core loop continuously, adapting their behavior based on environmental changes and learning from experience.
class ContinuousAgent:
def __init__(self):
self.loop = CoreAgentLoop()
self.running = False
self.cycle_count = 0
def start(self):
"""Start the continuous agent loop"""
self.running = True
while self.running:
try:
# Execute one cycle
result = self.loop.run_cycle()
self.cycle_count += 1
# Optional: pause between cycles
if self.should_pause():
time.sleep(self.get_pause_duration())
# Check for termination conditions
if self.should_terminate():
self.stop()
except Exception as e:
self.handle_error(e)
def stop(self):
"""Stop the agent loop gracefully"""
self.running = False
self.loop.reflection_module.final_reflection()
def should_pause(self):
"""Determine if agent should pause between cycles"""
return not self.has_urgent_tasks()
def should_terminate(self):
"""Check termination conditions"""
return (
self.all_goals_achieved() or
self.cycle_count > self.max_cycles or
self.received_stop_signal()
)
Advanced Loop Patterns
🔄 Interruptible Loop
Allows external interruption for urgent events
def interruptible_cycle(self):
# Check for interrupts before each phase
if self.has_interrupt():
return self.handle_interrupt()
perception = self.perceive()
if self.has_interrupt():
return self.handle_interrupt()
plan = self.reason(perception)
# ... continue with interrupt checks
⚡ Parallel Processing
Run phases concurrently when possible
async def parallel_cycle(self):
# Perception and reflection can run in parallel
perception_task = asyncio.create_task(
self.perceive_async()
)
reflection_task = asyncio.create_task(
self.reflect_on_previous_cycle()
)
perception, reflection = await asyncio.gather(
perception_task, reflection_task
)
# Reasoning and action are sequential
plan = await self.reason_async(perception)
result = await self.act_async(plan)
Strategic reasoning, long-term goals, resource allocation
Task decomposition, plan monitoring, coordination
Immediate responses, safety behaviors, reflex actions