Time: 90 minutes | Difficulty: Advanced
Autonomous agents are self-directed systems that pursue goals independently, maintaining state across sessions and adapting to changing conditions.
- Build goal-directed autonomous agents
- Implement persistent state management
- Handle multi-session agent execution
- Create safety and termination conditions
- Design self-monitoring systems
An autonomous agent with:
- Goal Tracking - Define and pursue objectives
- State Persistence - Save progress between runs
- Self-Monitoring - Track progress toward goals
- Adaptation - Adjust strategy based on results
- Completed Tutorial 13: RAG Pattern
- Understanding of all previous patterns
Autonomous agents:
- Set their own sub-goals
- Run independently over time
- Persist state between sessions
- Adapt strategies dynamically
- Monitor their own progress
$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]
]
];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);
}function assessProgress($state) {
$total = count($state['sub_goals']);
$completed = array_filter($state['sub_goals'],
fn($g) => $g['status'] === 'completed'
);
return count($completed) / $total;
}$safety = [
'max_iterations' => 100,
'max_cost' => 5.00, // dollars
'max_duration' => 3600, // seconds
'termination_conditions' => ['goal_achieved', 'budget_exceeded']
];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;
}
}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
$checkpoints = [
'research_complete' => $researchData,
'outline_done' => $outline,
'draft_v1' => $draft
];if ($progress < 0.2 && $iterations > 20) {
// Strategy not working, try different approach
$strategy = 'alternative_method';
}$budget = [
'tokens_used' => 15000,
'tokens_limit' => 100000,
'cost_used' => 0.45,
'cost_limit' => 5.00
];Critical safeguards:
- Maximum iterations - Prevent infinite loops
- Cost limits - Avoid runaway expenses
- Time limits - Bound execution time
- Human oversight - Approval for critical actions
- 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;
}- Understand autonomous agent architecture
- State persistence techniques
- Progress monitoring
- Safety and termination conditions
- Multi-session execution
Run the complete working example:
php tutorials/14-autonomous-agents/autonomous_agent.phpThe 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!
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
- Build your own agent applications
- Explore Claude's advanced features
- Join the community
- Share your creations!
- Autonomous ≠ Uncontrolled - Always have safety limits
- State is crucial - Persist between sessions
- Monitor progress - Track toward goals
- Adapt strategies - Change approach if not working
- Human oversight - For critical decisions
- Start simple - Add autonomy incrementally