The Claude PHP Agent Template System provides a powerful way to discover, instantiate, and share pre-configured agent setups. With 22+ ready-to-use templates, you can quickly get started with any agent pattern in the framework.
- Quick Start
- Core Concepts
- Template Structure
- Searching Templates
- Instantiating Agents
- Creating Custom Templates
- Template Categories
- API Reference
- Best Practices
- Troubleshooting
Templates are included with the framework. No additional installation required.
composer require claude-php/agentuse ClaudeAgents\Templates\TemplateManager;
// Initialize manager
$manager = TemplateManager::getInstance();
// Search for templates
$templates = $manager->search(query: 'chatbot');
// Instantiate an agent
$agent = $manager->instantiate('dialog-agent', [
'api_key' => getenv('ANTHROPIC_API_KEY')
]);
// Run the agent
$result = $agent->run('Hello! How can you help me?');
echo $result->getAnswer();A template is a JSON or PHP file that contains:
- Agent configuration (type, model, parameters)
- Metadata (name, description, tags, difficulty)
- Requirements (PHP version, extensions, packages)
- Use cases and documentation
The central hub for template operations:
- Search: Find templates by name, tags, or category
- Instantiate: Create live agents from templates
- Export: Save agent configurations as templates
- Browse: Explore catalog by category
Templates are organized into categories:
- agents: Basic and foundational agents
- chatbots: Conversational agents
- rag: Retrieval-augmented generation
- workflows: Multi-step and multi-agent systems
- specialized: Domain-specific agents
- production: Enterprise-ready configurations
{
"id": "unique-identifier",
"name": "Template Name",
"description": "Detailed description of what this template does",
"category": "agents",
"tags": ["tag1", "tag2", "tag3"],
"version": "1.0.0",
"author": "Author Name",
"last_tested_version": "1.2.0",
"requirements": {
"php": ">=8.1",
"extensions": ["json", "mbstring"],
"packages": ["claude-php/agent"]
},
"metadata": {
"icon": "🤖",
"difficulty": "beginner",
"estimated_setup": "5 minutes",
"use_cases": [
"Use case 1",
"Use case 2"
]
},
"config": {
"agent_type": "ReactAgent",
"model": "claude-sonnet-4-5",
"max_iterations": 10,
"system_prompt": "Your system prompt here",
"temperature": 0.7,
"max_tokens": 4096
}
}name: Template namedescription: What the template doescategory: One of the valid categoriesconfig.agent_type: Agent class name
tags: For searchabilitymetadata.difficulty: beginner, intermediate, or advancedmetadata.use_cases: Help users understand when to use itmetadata.icon: Visual identifier
// Search by query (searches name and description)
$templates = $manager->search(query: 'conversation');
// Search by category
$chatbots = $manager->search(category: 'chatbots');
// Search by tags
$beginnerTemplates = $manager->search(tags: ['beginner']);// Combined search
$results = $manager->search(
query: 'agent',
category: 'specialized',
tags: ['advanced', 'monitoring']
);
// With field selection (returns arrays instead of Template objects)
$results = $manager->search(
query: 'chatbot',
fields: ['name', 'description', 'difficulty']
);// Get all categories
$categories = $manager->getCategories();
// Get all tags
$tags = $manager->getAllTags();
// Get templates by category
$agentTemplates = $manager->getByCategory('agents');
// Get templates by tags
$conversational = $manager->getByTags(['conversation', 'memory']);
// Get total count
$count = $manager->count();// By template name
$agent = $manager->instantiate('react-agent', [
'api_key' => getenv('ANTHROPIC_API_KEY')
]);
// By template ID
$agent = $manager->instantiate('react-001', [
'api_key' => getenv('ANTHROPIC_API_KEY')
]);
// From template object
$template = $manager->getByName('Dialog Agent');
$agent = $manager->instantiateFromTemplate($template, [
'api_key' => getenv('ANTHROPIC_API_KEY')
]);Override any template configuration:
$agent = $manager->instantiate('dialog-agent', [
'api_key' => getenv('ANTHROPIC_API_KEY'),
'model' => 'claude-opus-4', // Override model
'max_iterations' => 15, // Override iterations
'system_prompt' => 'Custom prompt', // Override system prompt
'temperature' => 0.8, // Override temperature
'tools' => [$myCustomTool] // Add tools
]);use ClaudeAgents\Tools\Tool;
$calculator = Tool::create('calculate')
->description('Perform calculations')
->parameter('expression', 'string', 'Math expression')
->required('expression')
->handler(function (array $input): string {
return (string) eval("return {$input['expression']};");
});
$agent = $manager->instantiate('react-agent', [
'api_key' => getenv('ANTHROPIC_API_KEY'),
'tools' => [$calculator]
]);use ClaudePhp\ClaudePhp;
$client = new ClaudePhp(
apiKey: getenv('ANTHROPIC_API_KEY'),
timeout: 60.0
);
$agent = $manager->instantiate('production-agent', [
'client' => $client
]);use ClaudeAgents\Agent;
// Create your agent
$agent = Agent::create($client)
->withModel('claude-sonnet-4-5')
->withSystemPrompt('You are a helpful assistant')
->withTool($myTool);
// Export as template
$template = $manager->exportAgent($agent, [
'name' => 'My Custom Agent',
'description' => 'A specialized agent for my use case',
'category' => 'custom',
'tags' => ['custom', 'specialized'],
'author' => 'Your Name',
'metadata' => [
'icon' => '🎯',
'difficulty' => 'intermediate',
'estimated_setup' => '15 minutes',
'use_cases' => [
'Use case 1',
'Use case 2'
]
]
]);
// Save to file
$manager->saveTemplate($template, 'custom/my-agent.json');use ClaudeAgents\Templates\Template;
$template = Template::fromArray([
'name' => 'Custom Research Agent',
'description' => 'Specialized for academic research',
'category' => 'custom',
'tags' => ['research', 'academic'],
'version' => '1.0.0',
'author' => 'Research Team',
'requirements' => [
'php' => '>=8.1',
'packages' => ['claude-php/agent']
],
'metadata' => [
'icon' => '🔬',
'difficulty' => 'intermediate',
'use_cases' => ['Literature review', 'Research synthesis']
],
'config' => [
'agent_type' => 'ReactAgent',
'model' => 'claude-sonnet-4-5',
'max_iterations' => 15,
'system_prompt' => 'You are a research assistant...'
]
]);
// Validate
if ($template->isValid()) {
$manager->saveTemplate($template, 'custom/research-agent.json');
}Basic and foundational agent patterns:
- Basic Agent: Simple agent with one tool - perfect for learning
- ReAct Agent: Reason-Act-Observe pattern for autonomous tasks
- Chain-of-Thought Agent: Step-by-step reasoning for complex problems
- Reflex Agent: Rule-based responses for fast, deterministic reactions
- Model-Based Agent: State-aware decision making with planning
Conversational agents with context:
- Dialog Agent: Multi-turn conversations with context tracking
- Memory Chatbot: Persistent memory across sessions
Retrieval-augmented generation:
- RAG Agent: Document retrieval and question answering
- Memory Chatbot: Conversation memory and context
- Knowledge Manager: Knowledge base management
Multi-step and multi-agent systems:
- Sequential Tasks Agent: Pipeline processing
- Debate System: Multi-agent consensus building
Domain-specific agents:
- Hierarchical Agent: Master-worker pattern
- Coordinator Agent: Multi-agent orchestration
- Dialog Agent: Conversational AI
- Intent Classifier: Intent recognition and routing
- Monitoring Agent: System monitoring and alerts
Enterprise-ready configurations:
- Production Agent: Full error handling, logging, monitoring
- Async Batch Processor: Concurrent task processing
Get singleton instance.
Search templates with optional filters and field selection.
Get template by ID. Throws TemplateNotFoundException if not found.
Get template by name. Returns first match.
Get all templates in a category.
Get templates with ANY of the specified tags.
Instantiate agent from template by ID or name.
Export an agent as a template.
Save template to file (.json or .php).
Create template from array.
Create template from JSON string.
Validate template structure.
Get validation errors.
Convert to array.
Convert to JSON.
Convert to PHP array export.
-
Start with difficulty level
- Beginners: Use beginner templates
- Experienced: Try intermediate/advanced
-
Match use case
- Check template metadata use_cases
- Read template descriptions
-
Check requirements
- Verify PHP version compatibility
- Ensure required extensions are installed
-
Use environment variables for secrets
'api_key' => getenv('ANTHROPIC_API_KEY')
-
Override sparingly
- Templates provide sensible defaults
- Only override what you need
-
Validate before production
if ($template->isValid()) { // Use template }
-
Follow naming conventions
- Use clear, descriptive names
- Include agent type in name
-
Document thoroughly
- Detailed description
- List use cases
- Specify difficulty
-
Tag appropriately
- Use existing tags when possible
- Add domain-specific tags
-
Test before sharing
- Validate template
- Test instantiation
- Run with sample tasks
TemplateNotFoundException: Template with name 'XYZ' not found
Solution: Check spelling, use search() to find available templates.
TemplateValidationException: Template validation failed
Solution: Check getErrors() for specific validation issues.
TemplateInstantiationException: Failed to instantiate agent
Solutions:
- Verify ANTHROPIC_API_KEY is set
- Check agent type exists
- Verify configuration is valid
TemplateInstantiationException: Agent type 'XYZ' not found
Solution: Verify agent class exists and is imported.
See the examples/templates directory for comprehensive examples:
01-basic-usage.php- Loading and listing templates02-search-filter.php- Advanced search03-instantiate.php- Creating agents from templates04-custom-template.php- Exporting and creating templates05-categories-tags.php- Browsing by category/tag06-template-metadata.php- Working with metadata07-production-patterns.php- Production best practices
- Template Catalog - All templates documented
- Creating Templates - Template creation guide
- Templates Tutorial - Step-by-step tutorial