Skip to content

Commit 9d16aba

Browse files
docs: add comprehensive observability documentation for Agents SDK
Add detailed observability documentation including: - Event types (connection, state, message, AI, tool events) - Custom observability implementation - Integration examples (logging, metrics, filtering) - Best practices for performance, security, and debugging This syncs documentation changes from cloudflare/agents PR #639. Co-Authored-By: Claude <[email protected]>
1 parent 14dcac6 commit 9d16aba

File tree

1 file changed

+181
-0
lines changed

1 file changed

+181
-0
lines changed
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
---
2+
title: Observability
3+
pcx_content_type: reference
4+
sidebar:
5+
order: 95
6+
---
7+
8+
`Agent` instances use the `observability` property to emit various internal events that can be used for logging, monitoring, and debugging. This provides deep visibility into agent behavior and lifecycle events.
9+
10+
## Default Behavior
11+
12+
The default behavior is to `console.log()` the event value:
13+
14+
```json
15+
{
16+
displayMessage: 'State updated',
17+
id: 'EnOzrS_tEo_8dHy5oyl8q',
18+
payload: {},
19+
timestamp: 1758005142787,
20+
type: 'state:update'
21+
}
22+
```
23+
24+
## Event Types
25+
26+
The SDK emits the following event types:
27+
28+
### Connection Events
29+
30+
- `connect` - New client connection established
31+
- `disconnect` - Client connection closed
32+
- `error` - Connection error occurred
33+
34+
### State Events
35+
36+
- `state:update` - Agent state was modified
37+
- `state:read` - Agent state was accessed
38+
39+
### Message Events
40+
41+
- `message:sent` - Message sent to client
42+
- `message:received` - Message received from client
43+
- `message:error` - Message processing error
44+
45+
### AI Events
46+
47+
- `ai:request` - AI model request started
48+
- `ai:response` - AI model response received
49+
- `ai:error` - AI model request failed
50+
51+
### Tool Events
52+
53+
- `tool:call` - Tool execution started
54+
- `tool:result` - Tool execution completed
55+
- `tool:error` - Tool execution failed
56+
57+
## Custom Observability
58+
59+
You can configure custom observability by implementing the `Observability` interface:
60+
61+
```ts
62+
import { Agent } from "agents";
63+
import { type Observability } from "agents/observability";
64+
65+
const observability: Observability = {
66+
emit(event) {
67+
if (event.type === "connect") {
68+
console.log(event.timestamp, event.payload.connectionId);
69+
}
70+
}
71+
};
72+
73+
class MyAgent extends Agent {
74+
override observability = observability;
75+
}
76+
```
77+
78+
## Integration Examples
79+
80+
### Logging to External Service
81+
82+
```ts
83+
import { type Observability } from "agents/observability";
84+
85+
const loggingObservability: Observability = {
86+
async emit(event) {
87+
// Send to logging service
88+
await fetch("https://logs.example.com/events", {
89+
method: "POST",
90+
body: JSON.stringify({
91+
service: "my-agent",
92+
level: event.type.includes("error") ? "error" : "info",
93+
timestamp: event.timestamp,
94+
message: event.displayMessage,
95+
data: event.payload
96+
})
97+
});
98+
}
99+
};
100+
```
101+
102+
### Metrics and Monitoring
103+
104+
```ts
105+
import { type Observability } from "agents/observability";
106+
107+
const metricsObservability: Observability = {
108+
emit(event) {
109+
// Track connection metrics
110+
if (event.type === "connect") {
111+
// Increment active connections counter
112+
env.METRICS.writeDataPoint({
113+
metric: "agent.connections.active",
114+
value: 1,
115+
timestamp: event.timestamp
116+
});
117+
}
118+
119+
// Track AI model latency
120+
if (event.type === "ai:response") {
121+
const latency = event.payload.duration;
122+
env.METRICS.writeDataPoint({
123+
metric: "agent.ai.latency",
124+
value: latency,
125+
timestamp: event.timestamp
126+
});
127+
}
128+
}
129+
};
130+
```
131+
132+
### Filtering Events
133+
134+
```ts
135+
import { type Observability } from "agents/observability";
136+
137+
const filteredObservability: Observability = {
138+
emit(event) {
139+
// Only log errors and AI events
140+
if (event.type.includes("error") || event.type.startsWith("ai:")) {
141+
console.error("[Agent Event]", {
142+
type: event.type,
143+
message: event.displayMessage,
144+
payload: event.payload
145+
});
146+
}
147+
}
148+
};
149+
```
150+
151+
## Disabling Observability
152+
153+
To disable all observability events, set the property to `undefined`:
154+
155+
```ts
156+
import { Agent } from "agents";
157+
158+
class MyAgent extends Agent {
159+
override observability = undefined;
160+
}
161+
```
162+
163+
## Best Practices
164+
165+
### Performance Considerations
166+
167+
- Observability handlers should be non-blocking
168+
- Use async operations carefully to avoid slowing down agent operations
169+
- Consider batching events for external services
170+
171+
### Security
172+
173+
- Filter sensitive data from event payloads before sending to external services
174+
- Use secure connections for external logging services
175+
- Implement rate limiting for observability endpoints
176+
177+
### Debugging
178+
179+
- Enable full observability in development environments
180+
- Use filtered observability in production to reduce noise
181+
- Include correlation IDs in events for distributed tracing

0 commit comments

Comments
 (0)