> ## Documentation Index
> Fetch the complete documentation index at: https://developers.telnyx.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Lifecycle

> How an agent instance comes to life and receives work — the runtime-invoked constructor, the actor primitives ctx and env, the HTTP and WebSocket entry points, and the reserved alarm slot.

The runtime constructs and activates agent instances for you; these are the
members involved in that lifecycle — construction, the inherited actor
primitives, the inbound HTTP/WebSocket entry points, and the alarm slot the
SDK reserves for the task scheduler.

## constructor

> **new Agent**\<`E`, `State`>(`ctx`, `env`): [`Agent`](/docs/agent-sdk/api-reference/agent)\<`E`, `State`>

Called by the runtime on every activation — never construct an agent
yourself. Re-arms the task scheduler to the earliest pending task (inside
`ctx.blockConcurrencyWhile`), so persistent timers resume after a crash or
restart. A subclass constructor must call `super(ctx, env)` first.

**Parameters**

| Parameter | Type                                                                       |
| --------- | -------------------------------------------------------------------------- |
| `ctx`     | [`ActorContext`](/docs/edge-compute/stateful-actors/api-reference/context) |
| `env`     | `E`                                                                        |

**Returns**

[`Agent`](/docs/agent-sdk/api-reference/agent)\<`E`, `State`>

**Overrides**

`StatefulActor<E>.constructor`

## ctx

> `protected` `readonly` **ctx**: [`ActorContext`](/docs/edge-compute/stateful-actors/api-reference/context)

The actor's identity + storage + concurrency primitives.

**Inherited from**

[`StatefulActor`](/docs/edge-compute/stateful-actors/api-reference/base).[`ctx`](/docs/edge-compute/stateful-actors/api-reference/base)

## env

> `protected` `readonly` **env**: `E`

The bindings environment for this actor's worker scope.

**Inherited from**

[`StatefulActor`](/docs/edge-compute/stateful-actors/api-reference/base).[`env`](/docs/edge-compute/stateful-actors/api-reference/base)

## fetch()

> **fetch**(`_req`): `Promise`\<`Response`>

Optional HTTP-style entry. `stub.fetch(req)` from a caller routes here.
Subclasses override; the base default returns 404.

`Request` and `Response` are the standard Fetch API globals (Node 18+
exposes both natively).

**Parameters**

| Parameter | Type      |
| --------- | --------- |
| `_req`    | `Request` |

**Returns**

`Promise`\<`Response`>

**Inherited from**

[`StatefulActor`](/docs/edge-compute/stateful-actors/api-reference/base).[`fetch`](/docs/edge-compute/stateful-actors/api-reference/base)

## webSocket()

> `optional` **webSocket**(`ws`, `req`): `void` | `Promise`\<`void`>

Take ownership of an accepted WebSocket connection.

`ws` is the live socket; `req` is the handshake request, whose headers
carry whatever the front door attached (auth, identity). Declaring this
method is what opts the actor into holding sockets; `ctx.broadcast` /
`ctx.count` / `ctx.sockets` then operate on the sockets it holds.

**Parameters**

| Parameter | Type        |
| --------- | ----------- |
| `ws`      | `WebSocket` |
| `req`     | `Request`   |

**Returns**

`void` | `Promise`\<`void`>

**Inherited from**

[`StatefulActor`](/docs/edge-compute/stateful-actors/api-reference/base).[`webSocket`](/docs/edge-compute/stateful-actors/api-reference/base)

## onConnect()

> `protected` **onConnect**(`_conn`): `Promise`\<`void`>

Hook: a client connection is established. Override to authorize, seed, or
close the connection. The default implementation does nothing.

**Parameters**

| Parameter | Type      | Description                                              |
| --------- | --------- | -------------------------------------------------------- |
| `_conn`   | `unknown` | The connection object handed in by the connection layer. |

**Returns**

`Promise`\<`void`>

## alarm()

> **alarm**(`_info`): `Promise`\<`void`>

Reserved: the SDK claims the actor's alarm slot to drive the task
scheduler.

When the alarm fires, this drains every due task, dispatches each to the
method it names (or [\`onTask\`](/docs/agent-sdk/api-reference/agent/scheduling#ontask)), and re-arms the
alarm to the next deadline. **Do not override `alarm()` in an `Agent`
subclass** — doing so breaks `queue` / `schedule` / `every`. If you need
timed work, schedule a task instead.

**Parameters**

| Parameter | Type                                                     |
| --------- | -------------------------------------------------------- |
| `_info`   | [`AlarmInfo`](/docs/edge-compute/stateful-actors/alarms) |

**Returns**

`Promise`\<`void`>

**Overrides**

[`StatefulActor`](/docs/edge-compute/stateful-actors/api-reference/base).[`alarm`](/docs/edge-compute/stateful-actors/api-reference/base)
