Agent Self-Improvement System

Hard
Agents

Implement self-improving agent with meta-learning:

Learning Loop:

  1. Try strategy → Observe outcome → Update beliefs → Try better strategy

Methods:

  1. record_experience(task, actions, outcome, metadata): Store episode
  2. extract_patterns(n_recent): Find what works/fails
    • Success patterns: common features of successes
    • Failure patterns: warning signs
  3. update_strategies(): Adjust strategy scores
  4. select_strategy(task, strategies): Pick best for task
    • Use learned scores + exploration
  5. adapt_prompt(base, task_type): Customize prompts
  6. get_improvement_report(): Learning stats

Pattern Format: {'features': {...}, 'frequency': ..., 'success_rate': ...}

Examples

Example 1:
Input: agent = SelfImprovingAgent(); agent.record_experience('task1', ['a1'], 'success', {}); len(agent.experience_buffer)
Output: 1
Explanation: Experience recorded in buffer

Starter Code

class SelfImprovingAgent:
    """
    Agent that learns from experience and improves over time.
    """
    
    def __init__(self):
        self.experience_buffer = []
        self.success_patterns = {}
        self.failure_patterns = {}
        self.strategy_scores = {}
    
    def record_experience(self, task, actions, outcome, metadata):
        """Record task execution experience"""
        # Your implementation here
        pass
    
    def extract_patterns(self, n_recent=100):
        """Extract success/failure patterns from recent experiences"""
        # Your implementation here
        pass
    
    def update_strategies(self):
        """Update strategy preferences based on outcomes"""
        # Your implementation here
        pass
    
    def select_strategy(self, task, available_strategies):
        """
        Select best strategy for task based on learned preferences.
        """
        # Your implementation here
        pass
    
    def adapt_prompt(self, base_prompt, task_type):
        """Adapt prompt based on learned patterns for task type"""
        # Your implementation here
        pass
    
    def get_improvement_report(self):
        """Report on learning progress"""
        # Your implementation here
        pass
Lines: 1Characters: 0
Ready
The AI Interview - Master AI/ML Interviews