Skip to content

Latest commit

 

History

History
251 lines (190 loc) · 5.77 KB

File metadata and controls

251 lines (190 loc) · 5.77 KB

Tutorial 14: Autonomous Agents

Time: 90 minutes | Difficulty: Advanced

Autonomous agents are self-directed systems that pursue goals independently, maintaining state across sessions and adapting to changing conditions.

🎯 Learning Objectives

  • Build goal-directed autonomous agents
  • Implement persistent state management
  • Handle multi-session agent execution
  • Create safety and termination conditions
  • Design self-monitoring systems

🏗️ What We're Building

An autonomous agent with:

  1. Goal Tracking - Define and pursue objectives
  2. State Persistence - Save progress between runs
  3. Self-Monitoring - Track progress toward goals
  4. Adaptation - Adjust strategy based on results

📋 Prerequisites

🤔 What Makes Agents Autonomous?

Autonomous agents:

  • Set their own sub-goals
  • Run independently over time
  • Persist state between sessions
  • Adapt strategies dynamically
  • Monitor their own progress

🔑 Key Concepts

Goal Management

$goals = [
    'primary' => "Research and write article",
    'sub_goals' => [
        'research' => ['status' => 'in_progress', 'progress' => 0.6],
        'outline' => ['status' => 'pending', 'progress' => 0],
        'write' => ['status' => 'pending', 'progress' => 0]
    ]
];

State Persistence

function saveState($state, $file = 'agent_state.json') {
    file_put_contents($file, json_encode($state, JSON_PRETTY_PRINT));
}

function loadState($file = 'agent_state.json') {
    return json_decode(file_get_contents($file), true);
}

Progress Monitoring

function assessProgress($state) {
    $total = count($state['sub_goals']);
    $completed = array_filter($state['sub_goals'], 
        fn($g) => $g['status'] === 'completed'
    );
    return count($completed) / $total;
}

Safety Limits

$safety = [
    'max_iterations' => 100,
    'max_cost' => 5.00, // dollars
    'max_duration' => 3600, // seconds
    'termination_conditions' => ['goal_achieved', 'budget_exceeded']
];

💡 Autonomous Agent Loop

while (!goalAchieved() && !terminationCondition()) {
    $state = loadState();
    
    // Assess current situation
    $assessment = assess($state);
    
    // Decide next action
    $action = decide($assessment);
    
    // Execute action
    $result = execute($action);
    
    // Update state
    $state = updateState($state, $result);
    
    // Persist for next session
    saveState($state);
    
    // Check if we should pause
    if (shouldPause($state)) {
        echo "Pausing... Resume later.\n";
        break;
    }
}

🎯 Example: Multi-Session Agent

Session 1:

Goal: Research topic (30% complete)
State saved: research_data.json

Session 2:

Load state: research_data.json
Continue: Research topic (60% complete)
New goal: Outline article
State saved: research_data.json

Session 3:

Load state: research_data.json
Complete: Outline article
Begin: Write draft

⚙️ Advanced Features

Checkpoint System

$checkpoints = [
    'research_complete' => $researchData,
    'outline_done' => $outline,
    'draft_v1' => $draft
];

Adaptive Strategies

if ($progress < 0.2 && $iterations > 20) {
    // Strategy not working, try different approach
    $strategy = 'alternative_method';
}

Resource Budgeting

$budget = [
    'tokens_used' => 15000,
    'tokens_limit' => 100000,
    'cost_used' => 0.45,
    'cost_limit' => 5.00
];

⚠️ Safety Considerations

Critical safeguards:

  1. Maximum iterations - Prevent infinite loops
  2. Cost limits - Avoid runaway expenses
  3. Time limits - Bound execution time
  4. Human oversight - Approval for critical actions
  5. Rollback capability - Undo if needed
function checkSafety($state) {
    if ($state['iterations'] > MAX_ITERATIONS) {
        throw new Exception("Max iterations exceeded");
    }
    if ($state['cost'] > MAX_COST) {
        throw new Exception("Budget exceeded");
    }
    return true;
}

✅ Checkpoint

  • Understand autonomous agent architecture
  • State persistence techniques
  • Progress monitoring
  • Safety and termination conditions
  • Multi-session execution

💻 Try It Yourself

Run the complete working example:

php tutorials/14-autonomous-agents/autonomous_agent.php

The script demonstrates:

  • ✅ Goal-directed autonomous behavior
  • ✅ State persistence between sessions
  • ✅ Progress tracking and monitoring
  • ✅ Multi-session execution
  • ✅ Self-monitoring and adaptation
  • ✅ Safety limits and termination conditions

Note: This agent saves state to agent_state.json. Run it multiple times to see autonomous continuation across sessions!

🎓 Congratulations!

You've completed the entire Agentic AI tutorial series! You now know:

  • Core Patterns: ReAct, CoT, ToT
  • Execution Patterns: Plan-and-Execute, Reflection
  • Multi-Agent Systems: Hierarchical, Debate
  • Advanced Techniques: RAG, Autonomous Agents

🚀 Next Steps

  • Build your own agent applications
  • Explore Claude's advanced features
  • Join the community
  • Share your creations!

📚 Further Reading

💡 Key Takeaways

  1. Autonomous ≠ Uncontrolled - Always have safety limits
  2. State is crucial - Persist between sessions
  3. Monitor progress - Track toward goals
  4. Adapt strategies - Change approach if not working
  5. Human oversight - For critical decisions
  6. Start simple - Add autonomy incrementally