Perception and Action › The Perception-Action Cycle

Michael BrenndoerferJuly 15, 20252 min read
AI Agent HandbookAll chapters
Part 8 of 15Environment and Interaction - The Agent's World
Chapter 22Perception and Action › The Perception-Action Cycle
Reading section 4 of 10 · The Perception-Action Cycle · 2 minSection 4 of 10 · 2 min

Book contents

Section 4 of 10 · 2 min read

Perception and Action › The Perception-Action CycleOne section · Sections in this chapter4 / 10 ↓

Here's where it gets interesting. Perception and action aren't separate processes; they're part of a continuous cycle. Each action creates new things to perceive, and each perception informs the next action.

Let's trace through a more complex example:

User: "Schedule a meeting with Alice next Tuesday at 2pm"

Cycle 1:
  Perception: Agent reads user request
  Action: Agent calls calendar tool to check availability
  
Cycle 2:
  Perception: Agent sees Tuesday 2pm is already booked
  Action: Agent generates clarification request
  
Agent: "Tuesday at 2pm is already taken. Would 3pm work instead?"

Cycle 3:
  Perception: Agent reads user's response "Yes, 3pm works"
  Action: Agent calls calendar tool to create meeting
  
Cycle 4:
  Perception: Agent sees meeting was created successfully
  Action: Agent generates confirmation message
  
Agent: "Meeting with Alice scheduled for Tuesday at 3pm."

Each cycle builds on the previous one. The agent's actions change the environment (checking the calendar, creating a meeting), and these changes create new perceptions (seeing the conflict, seeing the successful creation).

Implementing the Cycle (Claude Sonnet 4.5)

Here's a simplified implementation of the perception-action cycle:

In[5]:
Code
class AgentCycle:
    def __init__(self):
        self.state = {
            "conversation_history": [],
            "current_goal": None
        }
    
    def perceive(self, input_data):
        """Process incoming information"""
        self.state["conversation_history"].append({
            "role": "perception",
            "data": input_data
        })
        return input_data
    
    def decide(self, perception):
        """Decide what action to take based on perception"""
        # In a real agent, this would use the LLM to reason
        if "weather" in perception.lower():
            return {"action": "call_tool", "tool": "weather_api"}
        else:
            return {"action": "respond", "message": "I understand."}
    
    def act(self, decision):
        """Execute the decided action"""
        if decision["action"] == "call_tool":
            # Simulate tool call
            result = {"temperature": 65, "condition": "sunny"}
            return result
        elif decision["action"] == "respond":
            return decision["message"]
    
    def run_cycle(self, user_input):
        """Run one complete perception-action cycle"""
        # Perceive
        perception = self.perceive(user_input)
        
        # Decide
        decision = self.decide(perception)
        
        # Act
        result = self.act(decision)
        
        return result

## Example usage
agent = AgentCycle()
result = agent.run_cycle("What's the weather like?")
print(f"Agent action result: {result}")
Out[5]:
Console
Agent action result: {'temperature': 65, 'condition': 'sunny'}

This example shows the three stages of each cycle: perceive, decide, and act. The agent's state persists across cycles, allowing it to maintain context and build on previous interactions.