-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathbatch_processing.php
More file actions
executable file
·168 lines (139 loc) · 5.9 KB
/
batch_processing.php
File metadata and controls
executable file
·168 lines (139 loc) · 5.9 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
#!/usr/bin/env php
<?php
/**
* Batch Processing - PHP examples from:
* https://docs.claude.com/en/docs/build-with-claude/batch-processing
*
* Process multiple requests asynchronously with 50% cost savings.
* Results available within 24 hours.
*/
require_once __DIR__ . '/../vendor/autoload.php';
require_once __DIR__ . '/helpers.php';
use ClaudePhp\ClaudePhp;
loadEnv(__DIR__ . '/../.env');
$client = new ClaudePhp(apiKey: getApiKey());
echo "=== Batch Processing - 50% Cost Savings ===\n\n";
// Example 1: Creating a batch
echo "Example 1: Creating a Message Batch\n";
echo "------------------------------------\n";
echo "Process multiple requests with 50% cost savings\n";
echo "Results available within 24 hours\n\n";
try {
$requests = [
[
'custom_id' => 'request-1',
'params' => [
'model' => 'claude-sonnet-4-5',
'max_tokens' => 1024,
'messages' => [
['role' => 'user', 'content' => 'What is the capital of France?']
]
]
],
[
'custom_id' => 'request-2',
'params' => [
'model' => 'claude-sonnet-4-5',
'max_tokens' => 1024,
'messages' => [
['role' => 'user', 'content' => 'What is 2+2?']
]
]
],
[
'custom_id' => 'request-3',
'params' => [
'model' => 'claude-sonnet-4-5',
'max_tokens' => 1024,
'messages' => [
['role' => 'user', 'content' => 'Write a haiku about coding']
]
]
]
];
echo "Creating batch with " . count($requests) . " requests...\n";
$batch = $client->messages()->batches()->create([
'requests' => $requests
]);
echo "✓ Batch created successfully!\n";
echo " Batch ID: {$batch['id']}\n";
echo " Status: {$batch['processing_status']}\n";
echo " Request counts:\n";
$counts = $batch['request_counts'];
echo " Processing: {$counts['processing']}\n";
echo " Succeeded: {$counts['succeeded']}\n";
echo " Errored: {$counts['errored']}\n";
echo " Canceled: {$counts['canceled']}\n";
echo " Expired: {$counts['expired']}\n\n";
echo "Use this batch_id to retrieve results later:\n";
echo " \$results = \$client->messages()->batches()->retrieve('{$batch['id']}');\n";
} catch (Exception $e) {
echo "Error: " . $e->getMessage() . "\n";
}
echo "\n" . str_repeat("=", 80) . "\n\n";
// Example 2: Listing batches
echo "Example 2: Listing Batches\n";
echo "--------------------------\n";
try {
$batches = $client->messages()->batches()->list([
'limit' => 5
]);
echo "Recent batches:\n";
foreach ($batches['data'] ?? [] as $batch) {
$counts = $batch['request_counts'];
$total = $counts['processing'] + $counts['succeeded'] + $counts['errored'] + $counts['canceled'] + $counts['expired'];
echo " • ID: {$batch['id']}\n";
echo " Status: {$batch['processing_status']}\n";
echo " Requests: {$total} total, {$counts['succeeded']} succeeded\n";
echo " Created: {$batch['created_at']}\n\n";
}
} catch (Exception $e) {
echo "Note: List batches to see your batch processing history\n";
echo "Error: " . $e->getMessage() . "\n";
}
echo "\n" . str_repeat("=", 80) . "\n\n";
// Example 3: Retrieving batch results
echo "Example 3: Retrieving Batch Results\n";
echo "------------------------------------\n";
echo "Check status and download results when processing is complete\n\n";
echo "Step 1: Retrieve batch status\n";
echo "```php\n";
echo "\$batch = \$client->messages()->batches()->retrieve('batch_01Ab...');\n";
echo "echo \$batch->processing_status; // 'in_progress', 'ended', etc.\n";
echo "```\n\n";
echo "Step 2: Get results URL when complete\n";
echo "```php\n";
echo "if (\$batch->processing_status === 'ended') {\n";
echo " \$resultsUrl = \$batch->results_url;\n";
echo " // Download and process results\n";
echo "}\n";
echo "```\n\n";
echo "Processing statuses:\n";
echo " • in_progress - Batch is being processed\n";
echo " • canceling - Cancellation requested\n";
echo " • ended - Processing complete (check results)\n";
echo "\n" . str_repeat("=", 80) . "\n\n";
// Example 4: Canceling a batch
echo "Example 4: Canceling a Batch\n";
echo "-----------------------------\n";
echo "Cancel in-progress batches if needed\n\n";
echo "```php\n";
echo "\$result = \$client->messages()->batches()->cancel('batch_01Ab...');\n";
echo "echo \$result->processing_status; // 'canceling'\n";
echo "```\n\n";
echo "Note: Only batches in 'in_progress' status can be canceled\n";
echo "\n" . str_repeat("=", 80) . "\n\n";
echo "✓ Batch processing examples completed!\n\n";
echo "Key Takeaways:\n";
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n";
echo "• 50% cost savings compared to standard API\n";
echo "• Results available within 24 hours\n";
echo "• Create batches with \$client->messages()->batches()->create()\n";
echo "• Each request needs a unique custom_id\n";
echo "• Poll with retrieve() to check status\n";
echo "• Download results from results_url when status is 'ended'\n";
echo "• Can cancel in-progress batches\n";
echo "• Ideal for: Analysis, evaluation, classification at scale\n";
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n\n";
echo "Related examples:\n";
echo " • See tests/Integration/BatchResultsTest.php for complete batch workflow\n";