-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmicro_agent_example.php
More file actions
266 lines (202 loc) · 8.38 KB
/
micro_agent_example.php
File metadata and controls
266 lines (202 loc) · 8.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
<?php
declare(strict_types=1);
/**
* MicroAgent Example
*
* Demonstrates the MicroAgent - a lightweight, focused agent designed for
* single-purpose tasks as part of the MAKER framework.
*
* MicroAgents are the atomic units of complex agent systems. Each micro-agent
* has a specific role and executes independently with high consistency.
*
* Key Features:
* - Specialized roles (decomposer, executor, composer, validator, discriminator)
* - Low temperature (0.1) for consistent, deterministic outputs
* - Built-in retry logic with exponential backoff
* - Minimal overhead - fast and efficient
* - Designed for parallel execution
*/
require_once __DIR__ . '/../vendor/autoload.php';
use ClaudeAgents\Agents\MicroAgent;
use ClaudePhp\ClaudePhp;
// Load environment
$dotenv = __DIR__ . '/../.env';
if (file_exists($dotenv)) {
$lines = file($dotenv, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach ($lines as $line) {
if (strpos(trim($line), '#') === 0) continue;
if (strpos($line, '=') === false) continue;
[$name, $value] = explode('=', $line, 2);
$_ENV[trim($name)] = trim($value);
}
}
$apiKey = $_ENV['ANTHROPIC_API_KEY'] ?? throw new RuntimeException('ANTHROPIC_API_KEY not set');
$client = new ClaudePhp(apiKey: $apiKey);
echo "=== MicroAgent Demo ===\n\n";
echo "MicroAgents are specialized, lightweight agents designed for single-purpose tasks.\n";
echo "They are the building blocks of complex multi-agent systems like MAKER.\n\n";
// Example 1: Executor Role - Direct Task Execution
echo "--- Example 1: Executor (Default Role) ---\n\n";
$executor = new MicroAgent($client, [
'role' => 'executor',
'temperature' => 0.1, // Very low for consistency
]);
echo "Task: Calculate 15% tip on \$67.43\n";
$result = $executor->execute("Calculate a 15% tip on a bill of \$67.43. Provide just the tip amount.");
echo "Result: {$result}\n\n";
echo str_repeat("-", 70) . "\n\n";
// Example 2: Decomposer Role - Break Down Complex Tasks
echo "--- Example 2: Decomposer (Task Breakdown) ---\n\n";
$decomposer = new MicroAgent($client, [
'role' => 'decomposer',
]);
$complexTask = "Plan a surprise birthday party for 20 people with a \$500 budget";
echo "Task: {$complexTask}\n\n";
$subtasks = $decomposer->execute("Break this task into clear, minimal subtasks:\n{$complexTask}");
echo "Decomposed Subtasks:\n{$subtasks}\n\n";
echo str_repeat("-", 70) . "\n\n";
// Example 3: Validator Role - Check Correctness
echo "--- Example 3: Validator (Result Verification) ---\n\n";
$validator = new MicroAgent($client, [
'role' => 'validator',
]);
$calculationToCheck = "15% of \$67.43 = \$10.11";
echo "Checking: {$calculationToCheck}\n";
$validation = $validator->execute(
"Validate this calculation and respond with 'VALID' or 'INVALID' with brief explanation: {$calculationToCheck}"
);
echo "Validation Result: {$validation}\n\n";
echo str_repeat("-", 70) . "\n\n";
// Example 4: Composer Role - Combine Results
echo "--- Example 4: Composer (Result Synthesis) ---\n\n";
$composer = new MicroAgent($client, [
'role' => 'composer',
]);
$subtaskResults = [
"Venue booked at community center - \$100",
"Catering ordered for 20 people - \$300",
"Decorations purchased - \$50",
"Birthday cake ordered - \$50",
];
echo "Subtask Results to Combine:\n";
foreach ($subtaskResults as $i => $result) {
echo " " . ($i + 1) . ". {$result}\n";
}
echo "\n";
$composedResult = $composer->execute(
"Synthesize these party planning results into a coherent summary:\n" .
implode("\n", $subtaskResults)
);
echo "Composed Summary:\n{$composedResult}\n\n";
echo str_repeat("-", 70) . "\n\n";
// Example 5: Discriminator Role - Choose Best Option
echo "--- Example 5: Discriminator (Option Selection) ---\n\n";
$discriminator = new MicroAgent($client, [
'role' => 'discriminator',
]);
$options = [
"Option A: Host party on Saturday afternoon at 2 PM (good weather, most people available)",
"Option B: Host party on Friday evening at 7 PM (some people working, but more intimate)",
"Option C: Host party on Sunday morning at 11 AM (brunch style, but some may have commitments)",
];
echo "Options to Evaluate:\n";
foreach ($options as $option) {
echo " {$option}\n";
}
echo "\n";
$bestOption = $discriminator->execute(
"Choose the best option for a surprise birthday party and explain why:\n" .
implode("\n", $options)
);
echo "Best Option: {$bestOption}\n\n";
echo str_repeat("-", 70) . "\n\n";
// Example 6: Custom System Prompt
echo "--- Example 6: Custom System Prompt ---\n\n";
$customAgent = new MicroAgent($client, [
'role' => 'executor',
]);
$customAgent->setSystemPrompt(
"You are a pirate expert. Answer all questions in pirate speak but remain factually accurate."
);
echo "Task: Explain what PHP is\n";
$pirateResult = $customAgent->execute("Explain what PHP is in one sentence.");
echo "Result (in pirate speak): {$pirateResult}\n\n";
echo str_repeat("-", 70) . "\n\n";
// Example 7: Retry Logic
echo "--- Example 7: Retry with Exponential Backoff ---\n\n";
$reliableAgent = new MicroAgent($client, [
'role' => 'executor',
]);
echo "Task: Complex calculation with retry protection\n";
$task = "If compound interest is calculated annually at 5% on \$1000 for 3 years, what's the final amount?";
$startTime = microtime(true);
$result = $reliableAgent->executeWithRetry($task, maxRetries: 3);
$duration = microtime(true) - $startTime;
echo "Result: {$result}\n";
echo "Execution Time: " . round($duration, 2) . "s\n\n";
echo str_repeat("-", 70) . "\n\n";
// Example 8: Parallel Micro-Agents (Simulated)
echo "--- Example 8: Parallel Execution Pattern ---\n\n";
echo "In production, you would execute these in parallel for speed.\n";
echo "Here we demonstrate the pattern sequentially:\n\n";
$tasks = [
"Calculate 18% tip on \$45.67",
"Calculate 18% tip on \$123.45",
"Calculate 18% tip on \$89.01",
];
$results = [];
$totalStartTime = microtime(true);
foreach ($tasks as $i => $task) {
$agent = new MicroAgent($client, ['role' => 'executor']);
$results[] = $agent->execute($task);
}
$totalDuration = microtime(true) - $totalStartTime;
echo "Processed " . count($tasks) . " tasks:\n";
foreach ($results as $i => $result) {
echo " Task " . ($i + 1) . ": {$result}\n";
}
echo "\nTotal Time: " . round($totalDuration, 2) . "s\n";
echo "Note: With async execution, this would be much faster!\n\n";
echo str_repeat("-", 70) . "\n\n";
// Summary
echo "=== MicroAgent Design Principles ===\n\n";
echo "1. SINGLE RESPONSIBILITY:\n";
echo " - Each micro-agent has one specific role\n";
echo " - Focused on doing one thing well\n";
echo " - No complex state or dependencies\n\n";
echo "2. CONSISTENCY:\n";
echo " - Low temperature (0.1) by default\n";
echo " - Deterministic outputs for same inputs\n";
echo " - Minimal variance in responses\n\n";
echo "3. RELIABILITY:\n";
echo " - Built-in retry logic\n";
echo " - Exponential backoff on failures\n";
echo " - Graceful error handling\n\n";
echo "4. EFFICIENCY:\n";
echo " - Minimal overhead\n";
echo " - Fast execution\n";
echo " - Designed for parallel processing\n\n";
echo "5. COMPOSABILITY:\n";
echo " - Can be combined into complex systems\n";
echo " - Building blocks for MAKER framework\n";
echo " - Independent and loosely coupled\n\n";
echo "=== Use Cases ===\n\n";
echo "MicroAgents are perfect for:\n\n";
echo " ✓ Task decomposition in multi-step processes\n";
echo " ✓ Parallel processing of independent subtasks\n";
echo " ✓ Voting mechanisms in error correction systems\n";
echo " ✓ Result validation and verification\n";
echo " ✓ Option discrimination and selection\n";
echo " ✓ Atomic operations in complex workflows\n\n";
echo "For complex multi-step tasks, see MakerAgent which orchestrates\n";
echo "multiple MicroAgents with voting and error correction.\n\n";
echo "=== Configuration Options ===\n\n";
echo "Available options:\n";
echo " - role: 'decomposer', 'executor', 'composer', 'validator', 'discriminator'\n";
echo " - model: Claude model to use (default: claude-sonnet-4-5)\n";
echo " - max_tokens: Maximum tokens per response (default: 2048)\n";
echo " - temperature: Sampling temperature (default: 0.1 for consistency)\n";
echo " - logger: PSR-3 logger for monitoring\n\n";
echo "For more information, see:\n";
echo " - MakerAgent example: ./maker_example.php\n";
echo " - MAKER framework: https://arxiv.org/html/2511.09030v1\n";