Observability
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.
The default behavior is to console.log() the event value:
{ displayMessage: 'State updated', id: 'EnOzrS_tEo_8dHy5oyl8q', payload: {}, timestamp: 1758005142787, type: 'state:update'}The SDK emits the following event types:
connect- New client connection establisheddisconnect- Client connection closederror- Connection error occurred
state:update- Agent state was modifiedstate:read- Agent state was accessed
message:sent- Message sent to clientmessage:received- Message received from clientmessage:error- Message processing error
ai:request- AI model request startedai:response- AI model response receivedai:error- AI model request failed
tool:call- Tool execution startedtool:result- Tool execution completedtool:error- Tool execution failed
You can configure custom observability by implementing the Observability interface:
import { Agent } from "agents";import { type Observability } from "agents/observability";
const observability: Observability = { emit(event) { if (event.type === "connect") { console.log(event.timestamp, event.payload.connectionId); } }};
class MyAgent extends Agent { override observability = observability;}import { type Observability } from "agents/observability";
const loggingObservability: Observability = { async emit(event) { // Send to logging service await fetch("https://logs.example.com/events", { method: "POST", body: JSON.stringify({ service: "my-agent", level: event.type.includes("error") ? "error" : "info", timestamp: event.timestamp, message: event.displayMessage, data: event.payload }) }); }};import { type Observability } from "agents/observability";
const metricsObservability: Observability = { emit(event) { // Track connection metrics if (event.type === "connect") { // Increment active connections counter env.METRICS.writeDataPoint({ metric: "agent.connections.active", value: 1, timestamp: event.timestamp }); }
// Track AI model latency if (event.type === "ai:response") { const latency = event.payload.duration; env.METRICS.writeDataPoint({ metric: "agent.ai.latency", value: latency, timestamp: event.timestamp }); } }};import { type Observability } from "agents/observability";
const filteredObservability: Observability = { emit(event) { // Only log errors and AI events if (event.type.includes("error") || event.type.startsWith("ai:")) { console.error("[Agent Event]", { type: event.type, message: event.displayMessage, payload: event.payload }); } }};To disable all observability events, set the property to undefined:
import { Agent } from "agents";
class MyAgent extends Agent { override observability = undefined;}- Observability handlers should be non-blocking
- Use async operations carefully to avoid slowing down agent operations
- Consider batching events for external services
- Filter sensitive data from event payloads before sending to external services
- Use secure connections for external logging services
- Implement rate limiting for observability endpoints
- Enable full observability in development environments
- Use filtered observability in production to reduce noise
- Include correlation IDs in events for distributed tracing
Was this helpful?
- Resources
- API
- New to Cloudflare?
- Directory
- Sponsorships
- Open Source
- Support
- Help Center
- System Status
- Compliance
- GDPR
- Company
- cloudflare.com
- Our team
- Careers
- © 2025 Cloudflare, Inc.
- Privacy Policy
- Terms of Use
- Report Security Issues
- Trademark