Observability
Agent instances use the observability property to emit various internal events that can be used for logging and monitoring.
The default behavior is to console.log() the event value:
{ "displayMessage": "State updated", "id": "EnOzrS_tEo_8dHy5oyl8q", "payload": {}, "timestamp": 1758005142787, "type": "state:update"}You can configure observability by overriding the property with an implementation of the Observability interface. This interface has a single emit() method that takes an ObservabilityEvent.
import { Agent } from "agents";import {} from "agents/observability";
const observability = { emit(event) { if (event.type === "connect") { console.log(event.timestamp, event.payload.connectionId); } },};
class MyAgent extends Agent { observability = observability;}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;}Alternatively, you can set the property to undefined to ignore all events:
import { Agent } from "agents";
class MyAgent extends Agent { observability = undefined;}import { Agent } from "agents";
class MyAgent extends Agent { override observability = undefined;}The observability system emits events for various agent activities:
| Event Type | Description |
|---|---|
connect | WebSocket connection established |
disconnect | WebSocket connection closed |
state:update | Agent state was updated |
message | Message received from client |
error | Error occurred during processing |
schedule:execute | Scheduled task executed |
queue:process | Queue task processed |
Each event has the following structure:
type ObservabilityEvent = { id: string; // Unique event identifier type: string; // Event type (e.g., "connect", "state:update") displayMessage: string; // Human-readable description timestamp: number; // Unix timestamp in milliseconds payload: Record<string, unknown>; // Event-specific data};You can integrate observability with external logging and monitoring services:
import { Agent } from "agents";import {} from "agents/observability";
const observability = { emit(event) { // Send to external logging service fetch("https://logging.example.com/ingest", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ service: "my-agent", level: event.type === "error" ? "error" : "info", message: event.displayMessage, metadata: { eventId: event.id, eventType: event.type, timestamp: event.timestamp, ...event.payload, }, }), }).catch(console.error); },};
class MyAgent extends Agent { observability = observability;}import { Agent } from "agents";import { type Observability, type ObservabilityEvent,} from "agents/observability";
const observability: Observability = { emit(event: ObservabilityEvent) { // Send to external logging service fetch("https://logging.example.com/ingest", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ service: "my-agent", level: event.type === "error" ? "error" : "info", message: event.displayMessage, metadata: { eventId: event.id, eventType: event.type, timestamp: event.timestamp, ...event.payload, }, }), }).catch(console.error); },};
class MyAgent extends Agent { override observability = observability;}You can filter which events to process:
const observability = { emit(event) { // Only log errors and state updates if (event.type === "error" || event.type === "state:update") { console.log(JSON.stringify(event)); } },};const observability: Observability = { emit(event) { // Only log errors and state updates if (event.type === "error" || event.type === "state:update") { console.log(JSON.stringify(event)); } },}; Configuration wrangler.jsonc setup and deployment.
Dashboard setup View logs in the Cloudflare dashboard.
Agents API Complete API reference for the Agents SDK.
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
- © 2026 Cloudflare, Inc.
- Privacy Policy
- Terms of Use
- Report Security Issues
- Trademark
-