-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathobservability_basic.php
More file actions
151 lines (123 loc) Β· 4.38 KB
/
Copy pathobservability_basic.php
File metadata and controls
151 lines (123 loc) Β· 4.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
<?php
/**
* Basic Observability Example
*
* Demonstrates basic usage of tracing, metrics, and cost estimation
* in agent execution.
*/
declare(strict_types=1);
require_once __DIR__ . '/../vendor/autoload.php';
use ClaudeAgents\Agent;
use ClaudeAgents\Observability\Tracer;
use ClaudeAgents\Observability\Metrics;
use ClaudeAgents\Observability\CostEstimator;
use ClaudeAgents\Tools\Tool;
use ClaudePhp\ClaudePhp;
// Initialize observability components
$tracer = new Tracer();
$metrics = new Metrics();
$estimator = new CostEstimator();
// Start a trace
$tracer->startTrace();
// Initialize Claude client and agent
$client = new ClaudePhp(getenv('ANTHROPIC_API_KEY'));
$agent = Agent::create($client)
->withSystemPrompt('You are a helpful assistant')
->withTool(new Tool(
name: 'calculator',
description: 'Perform basic math operations',
parameters: [
'operation' => ['type' => 'string', 'enum' => ['add', 'subtract', 'multiply', 'divide']],
'a' => ['type' => 'number'],
'b' => ['type' => 'number'],
],
handler: function (array $input): array {
$result = match ($input['operation']) {
'add' => $input['a'] + $input['b'],
'subtract' => $input['a'] - $input['b'],
'multiply' => $input['a'] * $input['b'],
'divide' => $input['a'] / $input['b'],
};
return ['result' => $result];
}
));
// Set up callbacks to track execution
$agent->onIteration(function ($iteration, $response, $context) use ($tracer, $metrics) {
// Start a span for this iteration
$span = $tracer->startSpan("iteration.{$iteration}");
// Record metrics if we have token usage
if (isset($response->usage)) {
$metrics->recordRequest(
success: true,
tokensInput: $response->usage->inputTokens ?? 0,
tokensOutput: $response->usage->outputTokens ?? 0,
duration: $span->getDuration()
);
}
$tracer->endSpan($span);
});
// Execute the agent
echo "π Running agent with observability...\n\n";
$span = $tracer->startSpan('agent.execution');
try {
$result = $agent->run('What is 157 multiplied by 23?');
$span->setStatus('OK');
echo "β
Result: {$result->getAnswer()}\n\n";
} catch (\Exception $e) {
$span->setStatus('ERROR', $e->getMessage());
echo "β Error: {$e->getMessage()}\n\n";
}
$tracer->endSpan($span);
// End the trace
$tracer->endTrace();
// Display observability data
echo "π Observability Summary\n";
echo str_repeat('=', 50) . "\n\n";
// Trace information
$traceData = $tracer->toArray();
echo "π Tracing:\n";
echo " Trace ID: {$traceData['trace_id']}\n";
echo " Total Spans: {$traceData['span_count']}\n";
echo " Total Duration: " . number_format($traceData['total_duration'], 2) . " ms\n\n";
// Display span tree
echo " Span Tree:\n";
$tree = $tracer->buildSpanTree();
displaySpanTree($tree, 4);
echo "\n";
// Metrics summary
$metricsSummary = $metrics->getSummary();
echo "π Metrics:\n";
echo " Total Requests: {$metricsSummary['total_requests']}\n";
echo " Success Rate: " . number_format($metricsSummary['success_rate'] * 100, 1) . "%\n";
echo " Total Tokens:\n";
echo " Input: {$metricsSummary['total_tokens']['input']}\n";
echo " Output: {$metricsSummary['total_tokens']['output']}\n";
echo " Total: {$metricsSummary['total_tokens']['total']}\n";
echo " Average Duration: " . number_format($metricsSummary['average_duration_ms'], 2) . " ms\n\n";
// Cost estimation
if ($metricsSummary['total_tokens']['total'] > 0) {
$cost = $estimator->estimateCost(
'claude-sonnet-4-5',
$metricsSummary['total_tokens']['input'],
$metricsSummary['total_tokens']['output']
);
echo "π° Cost Estimation:\n";
echo " Model: claude-sonnet-4-5\n";
echo " Estimated Cost: " . CostEstimator::formatCost($cost) . "\n";
}
/**
* Helper function to display span tree
*/
function displaySpanTree(array $tree, int $indent = 0): void
{
foreach ($tree as $node) {
$span = $node['span'];
$prefix = str_repeat(' ', $indent) . 'ββ ';
echo $prefix . $span->getName();
echo ' (' . number_format($span->getDuration(), 2) . 'ms)';
echo ' [' . $span->getStatus() . "]\n";
if (!empty($node['children'])) {
displaySpanTree($node['children'], $indent + 2);
}
}
}