-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbatch-processor.js
More file actions
91 lines (77 loc) · 2.22 KB
/
batch-processor.js
File metadata and controls
91 lines (77 loc) · 2.22 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
#!/usr/bin/env node
/**
* Batch Processor - Submit requests asynchronously for 50% cost savings
* Minimal batch processing with cost estimation
*/
class BatchProcessor {
constructor(apiKey) {
this.apiKey = apiKey;
this.queue = [];
}
// Add request to batch
addRequest(message, model = 'claude-haiku-4-5', options = {}) {
const req = {
id: `req_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`,
model,
message: Array.isArray(message) ? message : [{ role: 'user', content: message }],
maxTokens: options.maxTokens || 1024
};
this.queue.push(req);
return req.id;
}
// Estimate metrics
estimateMetrics() {
let tokens = 0;
this.queue.forEach(req => {
tokens += Math.ceil(JSON.stringify(req).length / 4);
});
const baseCost = (tokens / 1e6) * 3; // Sonnet baseline
const batchCost = baseCost * 0.5; // 50% discount
return {
totalRequests: this.queue.length,
estimatedTokens: tokens,
standardCost: baseCost.toFixed(4),
batchCost: batchCost.toFixed(4),
savings: (baseCost - batchCost).toFixed(4),
savingsPercent: '50%',
processingTime: '5-24 hours'
};
}
// Format for API submission
formatForSubmission() {
return {
requests: this.queue.map(r => ({
custom_id: r.id,
params: r
})),
metadata: {
created_at: new Date().toISOString(),
total_requests: this.queue.length
}
};
}
// Create JSONL batch file
createBatchFile() {
return this.queue.map(r => JSON.stringify(r)).join('\n');
}
// Clear queue
clear() {
this.queue = [];
}
// Get status
getStatus() {
return {
queued: this.queue.length,
metrics: this.estimateMetrics()
};
}
}
module.exports = BatchProcessor;
// CLI usage
if (require.main === module) {
const processor = new BatchProcessor(process.env.ANTHROPIC_API_KEY);
processor.addRequest('Classify: spam or not?', 'claude-haiku-4-5');
processor.addRequest('Extract names: John at Google', 'claude-haiku-4-5');
processor.addRequest('Summarize: The quick brown fox...', 'claude-haiku-4-5');
console.log('Batch Status:', JSON.stringify(processor.getStatus(), null, 2));
}