Observation-Action Cycle Logger

Easy
Agents

Implement an Observation-Action cycle logger that validates proper sequence:

  1. log_observation(): Starts new cycle with observation
  2. log_thought(): Adds thought to current cycle (must have observation first)
  3. log_action(): Completes cycle with action (must have thought first)
  4. get_cycles(): Returns list of completed cycles

Validation Rules:

  • Must log in order: observation → thought → action
  • Skip logging and return False if sequence violated
  • Complete cycle only when action is logged
  • Each cycle: {'observation': ..., 'thought': ..., 'action': ...}

Return Values:

  • log_* methods return True if logged successfully, False if sequence error

Examples

Example 1:
Input: logger = OALogger(); logger.log_observation('User asks weather'); logger.log_thought('Need weather API'); logger.log_action('call_weather_api()'); len(logger.get_cycles())
Output: 1
Explanation: Complete cycle logged, one cycle in history
Example 2:
Input: logger = OALogger(); logger.log_thought('No observation')
Output: False
Explanation: Thought without observation returns False

Starter Code

class OALogger:
    """
    Logger for Observation → Thought → Action cycles.
    Validates and records agent reasoning traces.
    """
    
    def __init__(self):
        self.cycles = []
        self.current_cycle = {}
    
    def log_observation(self, observation):
        """Log observation, starting new cycle"""
        # Your implementation here
        pass
    
    def log_thought(self, thought):
        """Log thought for current cycle"""
        # Your implementation here
        pass
    
    def log_action(self, action):
        """Log action and complete cycle"""
        # Your implementation here
        pass
    
    def get_cycles(self):
        """Return all completed cycles"""
        # Your implementation here
        pass
Lines: 1Characters: 0
Ready
The AI Interview - Master AI/ML Interviews