-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathconcepts.php
More file actions
739 lines (608 loc) · 30.5 KB
/
concepts.php
File metadata and controls
739 lines (608 loc) · 30.5 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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
#!/usr/bin/env php
<?php
/**
* Tutorial 0: Claude PHP SDK & Introduction to Agentic AI - Code Examples
*
* This file demonstrates SDK fundamentals and core concepts of agentic AI
* through working examples. Run this to see both the SDK basics and
* the difference between chatbot-style and agent-style interactions.
*/
require_once __DIR__ . '/../../vendor/autoload.php';
require_once __DIR__ . '/../../examples/helpers.php';
use ClaudePhp\ClaudePhp;
loadEnv(__DIR__ . '/../../.env');
$client = new ClaudePhp(apiKey: getApiKey());
echo "╔════════════════════════════════════════════════════════════════════════════╗\n";
echo "║ Tutorial 0: Claude PHP SDK & Introduction to Agentic AI ║\n";
echo "║ ║\n";
echo "║ This tutorial demonstrates SDK fundamentals and agentic AI concepts ║\n";
echo "║ through working code examples. Follow along to learn both! ║\n";
echo "╚════════════════════════════════════════════════════════════════════════════╝\n\n";
echo "⏱️ Estimated runtime: 2-3 minutes\n";
echo "💰 Estimated cost: ~$0.05-0.10 (depending on models)\n\n";
// ============================================================================
// PART 1: SDK FUNDAMENTALS
// ============================================================================
echo "\n";
echo "┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n";
echo "┃ PART 1: SDK FUNDAMENTALS ┃\n";
echo "┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛\n";
// ============================================================================
// Example 1: Basic Message Creation
// ============================================================================
echo "\n";
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n";
echo "Example 1: Basic Message Creation (Hello World)\n";
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n\n";
try {
echo "📤 Sending: 'Hello Claude, what can you do?'\n\n";
$response = $client->messages()->create([
'model' => 'claude-sonnet-4-5',
'max_tokens' => 500,
'messages' => [
['role' => 'user', 'content' => 'Hello Claude, what can you do?']
]
]);
echo "✓ Response received!\n\n";
echo "📥 Claude's response:\n";
echo " " . str_repeat("─", 70) . "\n";
foreach ($response->content as $block) {
if ($block['type'] === 'text') {
// Truncate for display
$text = $block['text'];
if (strlen($text) > 300) {
$text = substr($text, 0, 300) . "...[truncated]";
}
echo " " . str_replace("\n", "\n ", $text) . "\n";
}
}
echo " " . str_repeat("─", 70) . "\n";
echo "\n💡 Notice how we accessed the response content through the content array.\n";
} catch (Exception $e) {
echo "Error: {$e->getMessage()}\n";
}
echo "\n" . str_repeat("═", 80) . "\n\n";
// ============================================================================
// Example 2: Response Structure & Token Usage
// ============================================================================
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n";
echo "Example 2: Understanding Response Structure\n";
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n\n";
try {
echo "📋 Response Properties:\n\n";
$response = $client->messages()->create([
'model' => 'claude-sonnet-4-5',
'max_tokens' => 200,
'messages' => [
['role' => 'user', 'content' => 'What is PHP?']
]
]);
echo " ID: {$response->id}\n";
echo " Model: {$response->model}\n";
echo " Stop Reason: {$response->stop_reason}\n";
echo " Content Blocks: " . count($response->content) . "\n";
echo "\n💰 Token Usage:\n";
echo " Input tokens: {$response->usage->input_tokens}\n";
echo " Output tokens: {$response->usage->output_tokens}\n";
echo " Total tokens: " . ($response->usage->input_tokens + $response->usage->output_tokens) . "\n";
echo "\n📄 Content:\n";
foreach ($response->content as $block) {
if ($block['type'] === 'text') {
$text = $block['text'];
if (strlen($text) > 150) {
$text = substr($text, 0, 150) . "...";
}
echo " " . str_replace("\n", "\n ", $text) . "\n";
}
}
} catch (Exception $e) {
echo "Error: {$e->getMessage()}\n";
}
echo "\n" . str_repeat("═", 80) . "\n\n";
// ============================================================================
// Example 3: Model Comparison
// ============================================================================
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n";
echo "Example 3: Comparing Different Models\n";
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n\n";
$testQuestion = "Summarize machine learning in one sentence.";
$models = [
[
'name' => 'Claude Sonnet 4.5',
'id' => 'claude-sonnet-4-5',
'characteristics' => 'Balanced - best for most tasks'
],
[
'name' => 'Claude Haiku 4.5',
'id' => 'claude-haiku-4-5',
'characteristics' => 'Fast & cost-effective - best for simple tasks'
]
];
foreach ($models as $model) {
echo "🤖 {$model['name']}\n";
echo " Characteristics: {$model['characteristics']}\n";
echo " Question: \"{$testQuestion}\"\n";
try {
$start = microtime(true);
$response = $client->messages()->create([
'model' => $model['id'],
'max_tokens' => 200,
'messages' => [
['role' => 'user', 'content' => $testQuestion]
]
]);
$elapsed = round((microtime(true) - $start) * 1000);
echo " Response: ";
foreach ($response->content as $block) {
if ($block['type'] === 'text') {
echo $block['text'];
}
}
echo "\n";
echo " Tokens: {$response->usage->input_tokens} input, {$response->usage->output_tokens} output\n";
echo " Time: ~{$elapsed}ms\n\n";
} catch (Exception $e) {
echo " Error: {$e->getMessage()}\n\n";
}
}
echo str_repeat("═", 80) . "\n\n";
// ============================================================================
// Example 4: Multi-Turn Conversations
// ============================================================================
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n";
echo "Example 4: Multi-Turn Conversations (Conversation History)\n";
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n\n";
echo "💬 Building a conversation with context...\n\n";
try {
// First turn
echo "User: Hello! What's a good programming language to learn?\n";
$response1 = $client->messages()->create([
'model' => 'claude-sonnet-4-5',
'max_tokens' => 300,
'messages' => [
['role' => 'user', 'content' => 'Hello! What\'s a good programming language to learn?']
]
]);
$assistantMessage1 = '';
foreach ($response1->content as $block) {
if ($block['type'] === 'text') {
$text = $block['text'];
if (strlen($text) > 150) {
$text = substr($text, 0, 150) . "...";
}
echo "Claude: " . $text . "\n\n";
$assistantMessage1 = $block['text'];
}
}
// Second turn (building on first)
echo "User: Why would you recommend that one?\n";
$response2 = $client->messages()->create([
'model' => 'claude-sonnet-4-5',
'max_tokens' => 300,
'messages' => [
['role' => 'user', 'content' => 'Hello! What\'s a good programming language to learn?'],
['role' => 'assistant', 'content' => $assistantMessage1],
['role' => 'user', 'content' => 'Why would you recommend that one?']
]
]);
foreach ($response2->content as $block) {
if ($block['type'] === 'text') {
$text = $block['text'];
if (strlen($text) > 150) {
$text = substr($text, 0, 150) . "...";
}
echo "Claude: " . $text . "\n\n";
}
}
echo "✓ Conversation completed. Claude remembered the context!\n";
} catch (Exception $e) {
echo "Error: {$e->getMessage()}\n";
}
echo "\n" . str_repeat("═", 80) . "\n\n";
// ============================================================================
// Example 5: System Prompts
// ============================================================================
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n";
echo "Example 5: Using System Prompts to Set Personality\n";
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n\n";
echo "📋 System prompts guide Claude's behavior...\n\n";
try {
// Without system prompt
echo "1️⃣ Without system prompt:\n";
echo " Question: 'Explain quantum computing'\n";
$response1 = $client->messages()->create([
'model' => 'claude-sonnet-4-5',
'max_tokens' => 200,
'messages' => [
['role' => 'user', 'content' => 'Explain quantum computing in a single sentence.']
]
]);
foreach ($response1->content as $block) {
if ($block['type'] === 'text') {
$text = $block['text'];
if (strlen($text) > 100) {
$text = substr($text, 0, 100) . "...";
}
echo " Response: " . $text . "\n\n";
}
}
// With system prompt
echo "2️⃣ With system prompt (ELI5 - Explain Like I'm 5):\n";
echo " Question: 'Explain quantum computing'\n";
$response2 = $client->messages()->create([
'model' => 'claude-sonnet-4-5',
'max_tokens' => 200,
'system' => 'You are explaining technical concepts to a 5-year-old. Use simple words and analogies.',
'messages' => [
['role' => 'user', 'content' => 'Explain quantum computing in a single sentence.']
]
]);
foreach ($response2->content as $block) {
if ($block['type'] === 'text') {
$text = $block['text'];
if (strlen($text) > 100) {
$text = substr($text, 0, 100) . "...";
}
echo " Response: " . $text . "\n";
}
}
} catch (Exception $e) {
echo "Error: {$e->getMessage()}\n";
}
echo "\n" . str_repeat("═", 80) . "\n\n";
// ============================================================================
// Example 6: Temperature Control
// ============================================================================
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n";
echo "Example 6: Temperature - Controlling Randomness\n";
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n\n";
echo "🎚️ Temperature controls how creative/random responses are:\n";
echo " 0.0 = Deterministic (always the same answer)\n";
echo " 1.0 = Creative (different each time)\n\n";
try {
// Deterministic response
echo "1️⃣ Temperature = 0.0 (Deterministic):\n";
echo " Question: 'List three colors'\n";
$responses_deterministic = [];
for ($i = 0; $i < 2; $i++) {
$response = $client->messages()->create([
'model' => 'claude-sonnet-4-5',
'max_tokens' => 100,
'temperature' => 0.0,
'messages' => [
['role' => 'user', 'content' => 'List exactly three colors, nothing else.']
]
]);
foreach ($response->content as $block) {
if ($block['type'] === 'text') {
$responses_deterministic[] = trim($block['text']);
}
}
}
echo " Try 1: " . $responses_deterministic[0] . "\n";
echo " Try 2: " . $responses_deterministic[1] . "\n";
echo " ✓ Same every time (deterministic)\n\n";
} catch (Exception $e) {
echo " Error: {$e->getMessage()}\n\n";
}
echo str_repeat("═", 80) . "\n\n";
// ============================================================================
// Example 7: Error Handling
// ============================================================================
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n";
echo "Example 7: Error Handling & Exception Types\n";
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n\n";
echo "🛡️ Proper error handling is critical...\n\n";
try {
echo "1️⃣ Handling validation errors:\n";
// This will fail because max_tokens is required
try {
$response = $client->messages()->create([
'model' => 'claude-sonnet-4-5',
'messages' => [
['role' => 'user', 'content' => 'Hello!']
]
// Missing max_tokens
]);
} catch (InvalidArgumentException $e) {
echo " ✓ Caught validation error: max_tokens is required\n";
echo " Error: " . $e->getMessage() . "\n\n";
}
echo "2️⃣ Handling successful request with try-catch:\n";
try {
$response = $client->messages()->create([
'model' => 'claude-sonnet-4-5',
'max_tokens' => 100,
'messages' => [
['role' => 'user', 'content' => 'Hello!']
]
]);
echo " ✓ Request succeeded\n";
echo " Response ID: " . $response->id . "\n";
} catch (Exception $e) {
echo " Error: " . $e->getMessage() . "\n";
}
} catch (Exception $e) {
echo "Error: {$e->getMessage()}\n";
}
echo "\n" . str_repeat("═", 80) . "\n\n";
// ============================================================================
// PART 2: AGENTIC AI CONCEPTS
// ============================================================================
echo "\n";
echo "┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓\n";
echo "┃ PART 2: AGENTIC AI CONCEPTS ┃\n";
echo "┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛\n";
// ============================================================================
// Example 8: Traditional Chatbot (No Tools)
// ============================================================================
echo "\n";
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n";
echo "Example 8: Traditional Chatbot Behavior (No Tools)\n";
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n\n";
echo "Question: What is 1,234 × 5,678?\n\n";
try {
$response = $client->messages()->create([
'model' => 'claude-sonnet-4-5',
'max_tokens' => 1024,
'messages' => [
['role' => 'user', 'content' => 'What is 1,234 × 5,678?']
]
]);
echo "🤖 Chatbot Response:\n";
foreach ($response->content as $block) {
if ($block['type'] === 'text') {
$text = $block['text'];
if (strlen($text) > 200) {
$text = substr($text, 0, 200) . "...";
}
echo " {$text}\n";
}
}
echo "\n💡 Observation: The chatbot tries to calculate mentally but may be approximate.\n";
echo " It's limited to its training and reasoning capabilities.\n";
echo " Let's see how an agent with a calculator tool does better...\n";
} catch (Exception $e) {
echo "Error: {$e->getMessage()}\n";
}
echo "\n" . str_repeat("═", 80) . "\n\n";
// ============================================================================
// Example 9: Agent with Tool (Calculator)
// ============================================================================
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n";
echo "Example 9: Agent Behavior (With Calculator Tool)\n";
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n\n";
echo "Same question: What is 1,234 × 5,678?\n\n";
// Define a calculator tool
$calculatorTool = [
'name' => 'calculate',
'description' => 'Perform precise mathematical calculations. Supports +, -, *, /, and parentheses.',
'input_schema' => [
'type' => 'object',
'properties' => [
'expression' => [
'type' => 'string',
'description' => 'Mathematical expression to evaluate, e.g., "1234 * 5678"'
]
],
'required' => ['expression']
]
];
try {
// Step 1: Send request with tool
echo "🧠 Agent Reasoning Phase:\n";
$response1 = $client->messages()->create([
'model' => 'claude-sonnet-4-5',
'max_tokens' => 1024,
'tools' => [$calculatorTool],
'messages' => [
['role' => 'user', 'content' => 'What is 1,234 × 5,678?']
]
]);
// Check if agent wants to use the tool
$toolUse = null;
foreach ($response1->content as $block) {
if ($block['type'] === 'tool_use') {
$toolUse = $block;
echo " ✓ Agent decided to use tool: '{$block['name']}'\n";
echo " ✓ With parameters: " . json_encode($block['input']) . "\n";
}
}
if ($toolUse) {
// Step 2: Execute the tool (our code)
echo "\n🔧 Tool Execution Phase:\n";
$expression = $toolUse['input']['expression'];
echo " ✓ Evaluating: {$expression}\n";
// Simple eval (in production, use a proper math parser!)
// For demo purposes only - eval() is dangerous with untrusted input
$result = eval("return {$expression};");
echo " ✓ Result: {$result}\n";
// Step 3: Return result to agent
echo "\n🧠 Agent Response Phase:\n";
$response2 = $client->messages()->create([
'model' => 'claude-sonnet-4-5',
'max_tokens' => 1024,
'tools' => [$calculatorTool],
'messages' => [
['role' => 'user', 'content' => 'What is 1,234 × 5,678?'],
['role' => 'assistant', 'content' => $response1->content],
[
'role' => 'user',
'content' => [
[
'type' => 'tool_result',
'tool_use_id' => $toolUse['id'],
'content' => (string)$result
]
]
]
]
]);
echo " Agent Final Response:\n";
foreach ($response2->content as $block) {
if ($block['type'] === 'text') {
echo " {$block['text']}\n";
}
}
echo "\n💡 Observation: The agent used a tool to get the EXACT answer.\n";
echo " This is the power of agentic behavior!\n";
}
} catch (Exception $e) {
echo "Error: {$e->getMessage()}\n";
}
echo "\n" . str_repeat("═", 80) . "\n\n";
// ============================================================================
// Example 10: Demonstrating the ReAct Loop
// ============================================================================
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n";
echo "Example 10: The ReAct Loop in Action\n";
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n\n";
echo "Task: Calculate (100 × 50) + (25 × 30)\n";
echo "This requires TWO tool calls - let's see the agent figure it out!\n\n";
$messages = [
['role' => 'user', 'content' => 'What is (100 × 50) + (25 × 30)?']
];
$iteration = 0;
$maxIterations = 5;
while ($iteration < $maxIterations) {
$iteration++;
echo "╔════ Iteration {$iteration} ════╗\n\n";
// Call Claude
$response = $client->messages()->create([
'model' => 'claude-sonnet-4-5',
'max_tokens' => 1024,
'tools' => [$calculatorTool],
'messages' => $messages
]);
echo "🧠 REASON: Agent is thinking...\n";
echo "🛑 Stop Reason: {$response->stop_reason}\n\n";
// Add assistant response to conversation
$messages[] = ['role' => 'assistant', 'content' => $response->content];
// Check what the agent wants to do
if ($response->stop_reason === 'end_turn') {
echo "✅ COMPLETE: Agent finished!\n\n";
echo "📝 Final Answer:\n";
foreach ($response->content as $block) {
if ($block['type'] === 'text') {
echo " {$block['text']}\n";
}
}
break;
}
if ($response->stop_reason === 'tool_use') {
$toolResults = [];
foreach ($response->content as $block) {
if ($block['type'] === 'tool_use') {
echo "🔧 ACT: Using tool '{$block['name']}'\n";
echo " Input: {$block['input']['expression']}\n";
// Execute tool
$expression = $block['input']['expression'];
$result = eval("return {$expression};");
echo "👁️ OBSERVE: Tool returned: {$result}\n\n";
$toolResults[] = [
'type' => 'tool_result',
'tool_use_id' => $block['id'],
'content' => (string)$result
];
}
}
// Add tool results to conversation
if (!empty($toolResults)) {
$messages[] = ['role' => 'user', 'content' => $toolResults];
}
}
}
if ($iteration >= $maxIterations) {
echo "⚠️ Max iterations reached!\n";
}
echo "\n💡 Observation: The agent executed multiple tool calls in sequence,\n";
echo " using the results to solve the complete problem. This is ReAct!\n";
echo "\n" . str_repeat("═", 80) . "\n\n";
// ============================================================================
// Example 11: Understanding Stop Reasons
// ============================================================================
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n";
echo "Example 11: Understanding Stop Reasons\n";
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n\n";
$testCases = [
[
'name' => 'Direct Answer (No Tool)',
'message' => 'What is the capital of France?',
'expected' => 'end_turn'
],
[
'name' => 'Tool Use Needed',
'message' => 'What is 987 × 654?',
'expected' => 'tool_use'
]
];
foreach ($testCases as $test) {
echo "Test: {$test['name']}\n";
echo "Question: \"{$test['message']}\"\n";
try {
$response = $client->messages()->create([
'model' => 'claude-sonnet-4-5',
'max_tokens' => 1024,
'tools' => [$calculatorTool],
'messages' => [
['role' => 'user', 'content' => $test['message']]
]
]);
$stopReason = $response->stop_reason;
$match = $stopReason === $test['expected'] ? '✓' : '✗';
echo " Stop Reason: {$stopReason} {$match}\n";
echo " Expected: {$test['expected']}\n\n";
} catch (Exception $e) {
echo " Error: {$e->getMessage()}\n\n";
}
}
echo "💡 Key Insight: The 'stop_reason' tells you what the agent wants to do next:\n";
echo " • 'end_turn': Agent has completed its response\n";
echo " • 'tool_use': Agent wants to execute a tool\n";
echo " • 'max_tokens': Response was cut off (increase max_tokens)\n\n";
echo str_repeat("═", 80) . "\n\n";
// ============================================================================
// Summary
// ============================================================================
echo "╔════════════════════════════════════════════════════════════════════════════╗\n";
echo "║ Key Takeaways ║\n";
echo "╚════════════════════════════════════════════════════════════════════════════╝\n\n";
echo "SDK FUNDAMENTALS:\n";
echo "1️⃣ Installation & Configuration\n";
echo " The SDK is easy to set up with Composer and environment variables.\n\n";
echo "2️⃣ Basic API Usage\n";
echo " Create a client, call messages().create(), extract content.\n\n";
echo "3️⃣ Models & When to Use Them\n";
echo " Sonnet for quality, Haiku for speed and cost, Opus for specialized tasks.\n\n";
echo "4️⃣ Message Patterns\n";
echo " Single-turn, multi-turn, system prompts, and response prefilling.\n\n";
echo "5️⃣ Error Handling\n";
echo " Always use try-catch and handle specific exception types.\n\n";
echo "AGENTIC AI CONCEPTS:\n";
echo "1️⃣ Chatbots respond → Agents take action\n";
echo " Chatbots are limited to their training. Agents use tools to extend capabilities.\n\n";
echo "2️⃣ ReAct Loop = Reason → Act → Observe → Repeat\n";
echo " Agents iterate until the task is complete or max iterations reached.\n\n";
echo "3️⃣ Tools are the agent's superpowers\n";
echo " They enable getting real-time data, performing calculations, taking actions.\n\n";
echo "4️⃣ Stop reasons guide the loop\n";
echo " 'tool_use' = needs to execute a tool\n";
echo " 'end_turn' = task complete\n\n";
echo "5️⃣ Iteration limits prevent infinite loops\n";
echo " Always set a maximum to avoid runaway execution.\n\n";
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n\n";
echo "🎓 Ready to build your first real agent?\n";
echo " Continue to Tutorial 1: Your First Agent\n";
echo " → tutorials/01-first-agent/\n\n";
echo "📚 Concepts covered:\n";
echo " ✓ SDK Installation & Configuration\n";
echo " ✓ Basic API Usage & Response Handling\n";
echo " ✓ Available Models & Selection\n";
echo " ✓ Message Patterns & Conversation History\n";
echo " ✓ System Prompts & Advanced Parameters\n";
echo " ✓ Error Handling & Exception Types\n";
echo " ✓ Chatbot vs Agent behavior\n";
echo " ✓ The ReAct loop pattern\n";
echo " ✓ Tool use and agent capabilities\n";
echo " ✓ Stop reasons and their meanings\n";
echo " ✓ Multi-step problem solving\n\n";