@@ -133,6 +133,60 @@ export class CodeReviewAgent extends Agent<Env, AgentState> {
133133}
134134```
135135
136+ ### Monitoring Agent status
137+
138+ The Agents SDK provides a ` getAgentStatus ` function that allows you to check if an Agent is currently active and has active connections. This is useful for monitoring, debugging, and understanding the current state of your Agent instances.
139+
140+ <TypeScriptExample >
141+
142+ ``` ts
143+ import { Agent , AgentNamespace , getAgentByName , getAgentStatus } from ' agents' ;
144+
145+ interface Env {
146+ MyAgent: AgentNamespace <MyAgent >;
147+ }
148+
149+ export default {
150+ async fetch(request , env , ctx ): Promise <Response > {
151+ // Get a handle to an Agent instance
152+ let agent = await getAgentByName <Env , MyAgent >(env .MyAgent , ' user-123' );
153+
154+ // Check the agent's status
155+ let status = getAgentStatus (agent );
156+
157+ // status contains:
158+ // - connectionCount: number of active WebSocket connections
159+ // - isActive: boolean indicating if the agent has any active connections
160+ // - agentId: the unique identifier for this agent instance
161+
162+ return Response .json ({
163+ agentId: status .agentId ,
164+ isActive: status .isActive ,
165+ activeConnections: status .connectionCount
166+ });
167+ },
168+ } satisfies ExportedHandler <Env >;
169+
170+ export class MyAgent extends Agent <Env > {
171+ // Your Agent implementation
172+ }
173+ ```
174+
175+ </TypeScriptExample >
176+
177+ The ` getAgentStatus ` function returns an object with three properties:
178+
179+ * ` connectionCount ` : The number of active WebSocket connections to this Agent instance
180+ * ` isActive ` : A boolean indicating whether the Agent has any active connections (` true ` if ` connectionCount > 0 ` )
181+ * ` agentId ` : The unique identifier for this Agent instance (may be ` undefined ` for some Agent types)
182+
183+ This is particularly useful for:
184+
185+ * Building admin dashboards to monitor Agent activity
186+ * Debugging connection issues
187+ * Implementing logic that depends on whether an Agent is actively serving clients
188+ * Logging and analytics about Agent usage
189+
136190### Naming your Agents
137191
138192When creating names for your Agents, think about what the Agent represents. A unique user? A team or company? A room or channel for collaboration?
0 commit comments