21. Power Management

Chapter 21 of 24 · 20 min

Running a personal AI agent continuously consumes power. OpenCLaw implements power management that balances availability with energy efficiency.

Activity Scheduling

OpenCLaw reduces power consumption during predicted idle periods. Activity scheduling uses learned patterns to determine low-power modes.

# power_management.py
from dataclasses import dataclass
from datetime import datetime, time
from typing import List, Dict

@dataclass
class PowerMode:
    name: str
    cpu_governor: str
    model_loading: str
    check_interval_seconds: int

class PowerManager:
    MODES = {
        'active': PowerMode('active', 'performance', 'always', 1),
        'idle': PowerMode('idle', 'powersave', 'loaded', 60),
        'sleep': PowerMode('sleep', 'powersave', 'unloaded', 300),
    }
    
    def __init__(self, battery_monitor=None):
        self.battery = battery_monitor
        self.current_mode = 'idle'
        self.schedule = ActivitySchedule()
    
    def determine_mode(self) -> str:
        if self.battery and self.battery.is_critical():
            return 'sleep'
        
        time_now = datetime.now().time()
        if not self.schedule.is_active_hours(time_now):
            return 'sleep'
        
        if self.schedule.is_peak_hours(time_now):
            return 'active'
        
        return 'idle'
    
    def transition_to(self, mode_name: str):
        mode = self.MODES[mode_name]
        self.apply_mode(mode)
        self.current_mode = mode_name
    
    def apply_mode(self, mode: PowerMode):
        pass

class ActivitySchedule:
    def __init__(self):
        self.active_hours = [(time(8, 0), time(22, 0))]
    
    def is_active_hours(self, current_time: time) -> bool:
        for start, end in self.active_hours:
            if start <= current_time <= end:
                return True
        return False
    
    def is_peak_hours(self, current_time: time) -> bool:
        peak = [(time(9, 0), time(11, 0)), (time(14, 0), time(16, 0))]
        for start, end in peak:
            if start <= current_time <= end:
                return True
        return False

Model Loading Strategies

Large models consume significant memory and initialization time. Power management affects when models load and unload.

class ModelPowerStrategy:
    def __init__(self, model_manager):
        self.manager = model_manager
    
    def decide_model_state(self, power_mode: str, activity_level: float) -> str:
        if power_mode == 'sleep':
            return 'unloaded'
        
        if activity_level < 0.2:
            return 'loaded_idle'
        
        if activity_level > 0.8:
            return 'ready'
        
        return 'loaded'
    
    def execute_load_strategy(self, strategy: str):
        if strategy == 'unloaded':
            self.manager.unload_model()
        elif strategy == 'loaded_idle':
            self.manager.load_model_keep_warm()
        elif strategy == 'ready':
            self.manager.prepare_inference()

Battery Integration

On battery-powered devices, OpenCLaw monitors battery state and adjusts behavior accordingly.

class BatteryMonitor:
    def __init__(self, threshold_low: int = 20, threshold_critical: int = 10):
        self.threshold_low = threshold_low
        self.threshold_critical = threshold_critical
        self.current_level = 100
    
    def update(self):
        import subprocess
        result = subprocess.run(
            ['cat', '/sys/class/power_supply/BAT0/capacity'],
            capture_output=True,
            text=True
        )
        if result.returncode == 0:
            self.current_level = int(result.stdout.strip())
    
    def is_low(self) -> bool:
        return self.current_level <= self.threshold_low
    
    def is_critical(self) -> bool:
        return self.current_level <= self.threshold_critical
    
    def should_charge(self) -> bool:
        return self.current_level < 80
EXERCISE

Implement an adaptive power policy that learns from user interaction patterns. The system should predict when users typically interact and prepare resources accordingly while minimizing consumption during likely idle periods.