A comprehensive, progressive tutorial series that teaches you how to build intelligent AI agents from the ground up, culminating in advanced ReAct patterns with planning, reflection, and tool composition.
By the end of this series, you'll understand:
- Core Concepts: What AI agents are and how they differ from chatbots
- ReAct Pattern: The Reason-Act-Observe loop that powers autonomous agents
- Tool Use: How to give Claude capabilities through function calling
- Production Patterns: Error handling, retries, memory, and state management
- Advanced Techniques: Planning, reflection, and multi-step reasoning
- Agent Architectures: Building complete orchestration systems
This series is designed for PHP developers new to AI agents. We'll explain AI concepts as we go, so prior experience with LLMs is helpful but not required. You should be comfortable with:
- PHP 8.1+ syntax
- Basic HTTP/API concepts
- JSON structures
- Async patterns (helpful but not required)
Before starting, make sure you have:
-
PHP 8.1 or higher installed
-
Composer for dependency management
-
Anthropic API Key (Get one here)
-
Claude PHP SDK installed:
composer require claude-php/claude-php-sdk
-
Environment Setup: Create a
.envfile in the project root:ANTHROPIC_API_KEY=your-api-key-here
Time: 20 minutes | Difficulty: Beginner
Understand the fundamental concepts of AI agents, autonomy, and the ReAct pattern.
What You'll Learn:
- Agents vs chatbots
- What makes an agent "agentic"
- The ReAct (Reason-Act-Observe) pattern
- When to use agents vs simple API calls
Files:
- 📖 README.md - Concepts guide
- 💻 concepts.php - Interactive examples
Time: 30 minutes | Difficulty: Beginner
Build your first working agent with a single tool (calculator).
What You'll Learn:
- Tool definitions and input schemas
- Request → Tool call → Execute → Response flow
- Handling tool results
- Basic conversation continuity
Files:
- 📖 README.md - Step-by-step guide
- 💻 simple_agent.php - Working code
Time: 45 minutes | Difficulty: Intermediate
Implement a ReAct loop that enables iterative reasoning and action.
What You'll Learn:
- The Reason → Act → Observe loop
- Stop conditions and loop control
- Multi-turn conversations with state
- Debugging agent reasoning
Files:
- 📖 README.md - ReAct pattern explained
- 💻 react_agent.php - ReAct implementation
Time: 45 minutes | Difficulty: Intermediate
Expand your agent with multiple tools and intelligent tool selection.
What You'll Learn:
- Defining multiple diverse tools
- How Claude selects the right tool
- Parameter extraction and validation
- Tool result formatting
- Debugging tool selection
Files:
- 📖 README.md - Multi-tool patterns
- 💻 multi_tool_agent.php - Agent with 4 tools
Time: 60 minutes | Difficulty: Intermediate
Build a robust, production-ready agent with proper error handling and memory.
What You'll Learn:
- Comprehensive error handling
- Retry logic with exponential backoff
- Tool execution error reporting
- Persistent memory with the Memory Tool
- Graceful degradation
- Logging and monitoring
Files:
- 📖 README.md - Production patterns
- 💻 production_agent.php - Robust implementation
Time: 60 minutes | Difficulty: Advanced
Enhance your agent with planning, reflection, and extended thinking.
What You'll Learn:
- Plan → Execute → Reflect → Adjust pattern
- Extended thinking for complex reasoning
- Self-correction and adaptation
- Multi-step task decomposition
- Reasoning transparency
Files:
- 📖 README.md - Advanced patterns
- 💻 advanced_react_agent.php - Planning & reflection
Time: 90 minutes | Difficulty: Advanced
Build a complete orchestration system with task decomposition and parallel execution.
What You'll Learn:
- Agent architecture patterns
- Task decomposition strategies
- Parallel tool execution
- State management at scale
- Tool composition
- Complex workflow orchestration
Files:
- 📖 README.md - Framework design
- 💻 agentic_framework.php - Complete system
Beyond the foundational patterns, explore advanced agentic AI techniques:
Time: 45 minutes | Difficulty: Intermediate
Master step-by-step reasoning without tools using the Chain of Thought pattern.
What You'll Learn:
- Zero-shot and few-shot CoT prompting
- When to use CoT vs ReAct
- Transparent reasoning processes
- CoT for math, logic, and decision making
Files:
Time: 60 minutes | Difficulty: Advanced
Explore multiple reasoning paths simultaneously with Tree of Thoughts.
What You'll Learn:
- Multi-path exploration strategies
- Evaluation and backtracking
- BFS vs DFS vs best-first search
- Solving puzzles and optimization problems
Files:
Time: 45 minutes | Difficulty: Intermediate
Separate planning from execution for more efficient agents.
What You'll Learn:
- Explicit planning phases
- Systematic execution
- Plan revision and monitoring
- When to use vs ReAct
Files:
Time: 45 minutes | Difficulty: Intermediate
Build agents that evaluate and improve their own work.
What You'll Learn:
- Generate-Reflect-Refine loops
- Quality assessment criteria
- Iterative improvement
- Self-correction techniques
Files:
Time: 60 minutes | Difficulty: Advanced
Organize agents into master-worker hierarchies for complex tasks.
What You'll Learn:
- Master-worker architectures
- Task delegation strategies
- Specialized sub-agents
- Result aggregation
Files:
Time: 60 minutes | Difficulty: Advanced
Use multiple agents with different perspectives to reach better decisions.
What You'll Learn:
- Debate protocols and rounds
- Role-based prompting (proposer, critic, judge)
- Consensus building
- When debate improves outcomes
Files:
Time: 60 minutes | Difficulty: Advanced
Integrate document retrieval with generation for knowledge-grounded responses.
What You'll Learn:
- Retrieval-Augmented Generation
- Document retrieval strategies
- Context injection techniques
- Citation tracking
Files:
Time: 90 minutes | Difficulty: Advanced
Build self-directed agents that pursue goals independently across sessions.
What You'll Learn:
- Goal-directed behavior
- State persistence between sessions
- Progress monitoring
- Safety and termination conditions
Files:
Time: 60 minutes | Difficulty: Advanced
Master context window management with auto-compaction, effort levels, and advanced tool discovery.
What You'll Learn:
- Auto-compaction for managing context window size
- Effort levels for controlling response quality
- Tool search for dynamic tool discovery
- MCP toolset configuration
- Computer use v5 features
Files:
Time: 60 minutes | Difficulty: Intermediate
Learn about the powerful new features in v0.5.2 that achieve parity with Python SDK v0.76.0.
What You'll Learn:
- Server-side tools (executed by Claude's API)
- Authentication flexibility (OAuth2, Bearer tokens, proxies)
- Enhanced stream management with automatic cleanup
- Binary request streaming capabilities
- How to mix client-side and server-side tools
Files:
Time: 60 minutes | Difficulty: Intermediate
Learn about the features added in v0.6.0 for full parity with Python SDK v0.80.0, including the new Claude 4.6 models, adaptive thinking, fast-mode inference, and the complete server-side tool suite.
What You'll Learn:
- Adaptive thinking (
type: "adaptive") — model decides how much to think - Speed / fast-mode parameter for high-throughput Beta Messages
output_configfor structured outputs in GA and Beta Messages- Typed
ModelParamconstants for all current Claude models - Code execution tool (GA + Beta REPL-state persistence)
- Memory tool for file-based cross-session persistence
- Web fetch tool with domain restrictions and token caps
- Beta web search v2 (
web_search_20260209) withallowed_callers
Files:
These tutorials build on patterns from the SDK's examples/ directory:
- Tool Use Basics: tool_use_overview.php
- Tool Implementation: tool_use_implementation.php
- Extended Thinking: extended_thinking.php
- Memory Tool: memory_tool.php
- Error Handling: error_handling.php
- Token Efficiency: token_efficient_tool_use.php
The tutorials use shared helper functions from helpers.php including:
runAgentLoop()- Execute ReAct loops with configurable limitsformatToolResult()- Standardize tool execution resultsdebugAgentStep()- Visual debugging for agent reasoningmanageConversationHistory()- History management with token limitsextractToolUses()- Parse tool use blocks from responses
Follow these in order to build core competency:
Tutorial 0 (Concepts)
↓
Tutorial 1 (First Agent)
↓
Tutorial 2 (ReAct Loop)
↓
Tutorial 3 (Multi-Tool)
↓
Tutorial 4 (Production)
↓
Tutorial 5 (Advanced ReAct)
↓
Tutorial 6 (Framework)
After completing foundations, explore these specialized patterns:
Reasoning Patterns:
- Tutorial 7 (Chain of Thought) - Step-by-step reasoning
- Tutorial 8 (Tree of Thoughts) - Multi-path exploration
Execution Patterns:
- Tutorial 9 (Plan-and-Execute) - Systematic task completion
- Tutorial 10 (Reflection) - Self-improvement
Multi-Agent Patterns:
- Tutorial 11 (Hierarchical) - Master-worker systems
- Tutorial 12 (Debate) - Consensus building
Advanced Systems:
- Tutorial 13 (RAG) - Knowledge integration
- Tutorial 14 (Autonomous) - Self-directed agents
- Tutorial 15 (Context Management) - Advanced context handling
- Tutorial 16 (v0.5.2 Features) - Server tools, auth, stream management
- Tutorial 17 (v0.6.0 Features) - Adaptive thinking, fast mode, new tool suite
If you're already familiar with agents, you can jump to:
- Tutorial 2 if you understand tool calling
- Tutorial 4 if you understand ReAct
- Tutorial 7 for reasoning patterns
- Tutorial 11 for multi-agent systems
- Run the Code: Each tutorial has executable PHP files. Run them to see agents in action!
- Experiment: Modify the examples, add new tools, change prompts
- Read Comments: The code is heavily commented to explain every decision
- Check Costs: We show token usage - be mindful when experimenting
- Debug Reasoning: Use the debug helpers to understand agent decisions
- Start Simple: Don't skip ahead - foundations matter!
"API Key not found"
- Ensure your
.envfile exists in the project root - Check the key is set:
ANTHROPIC_API_KEY=sk-ant-... - Load environment in PHP:
loadEnv(__DIR__ . '/../.env')
"Tool not executing"
- Verify tool name matches exactly
- Check input schema matches the data
- Look at
stop_reason- should betool_use
"Agent loops infinitely"
- Set max iterations (we use 10 by default)
- Check stop conditions in your loop
- Verify tool results are being returned correctly
"High token usage"
- Use prompt caching for repeated context
- Limit conversation history length
- Optimize tool descriptions
- See token_efficient_tool_use.php
After completing this series, explore:
- Anthropic Prompt Engineering Guide
- Tool Use Best Practices
- Extended Thinking Documentation
- Research Papers:
Found an issue or have suggestions? Please open an issue or PR on the SDK repository!
Ready to start? → Begin with Tutorial 0: Introduction to Agentic AI