Cloudflare sub-agents: reuse, abort and deletion boundaries
Create a child by class and name, call user-defined methods, and distinguish abort from permanent storage deletion.
Exports and parent binding · 1. Get or create the named child · 2. Abort execution while preserving storage
At a glance
Cloudflare sub-agents are co-located Durable Object facets with isolated SQLite storage. A parent obtains a typed RPC stub for custom child methods and coordinates lifecycle by instance name. This task defines creation and a teaching call, abort, and a separately authorized deletion path. It keeps stopping execution distinct from clearing data.
Runtime and applicability
- Article type
- Engineering practice
- Verified
- Applies to
- Cloudflare Agents SDK Use the cited documentation and version scope. Not tested.
Execution locations and responsibilities
- Parent coordination methods, Worker entry exports and existing client access entry point The parent resolves children by class and name and controls RPC, abort and authorized deletion.
Arrows show the order of checks in the guide. Follow each step’s prerequisites, branches and stop conditions; unexecuted steps are not completed work.
When to use
- An existing parent manages long-lived isolated children for the same entity.
When not to use
- A simple one-off function call may not need children; no permanent deletion without an explicit authorization decision.
Exports and parent binding
Both classes extend Agent and are exported from the Worker entry point under their actual class names. Production configuration needs the top-level parent Durable Object binding and migration, not an additional binding or new_sqlite_classes entry for a facet-only child. A class also used as a top-level object needs its actual role considered. Sub is reserved and cannot be the child class name.
1. Get or create the named child
The parent calls await this.subAgent(Child, "sample-child"). Creation triggers child onStart; later calls with the same class and name reuse that instance. The stub exposes public user-defined child methods, not inherited Agent methods such as sql, setState or lifecycle hooks. The teaching label method returns its input and performs no network or model action.
import { Agent } from "agents";
export class Parent extends Agent {
async readChildLabel() {
const child = await this.subAgent(Child, "sample-child");
return await child.label("paper cup");
}
stopChild() {
this.abortSubAgent(Child, "sample-child", "Stop this teaching run");
}
resetChildAfterApproval() {
this.deleteSubAgent(Child, "sample-child");
}
}
export class Child extends Agent {
label(value: string): string {
return value;
}
}
2. Abort execution while preserving storage
Use abortSubAgent to stop the current execution. The documentation says the child stops immediately, keeps its SQLite storage and restarts on the next subAgent call. Abort also reaches nested children. Pending RPC callers may receive the supplied reason; preserve the aborted state rather than turning its error into an empty successful result.
3. Confirm scope before permanent reset
deleteSubAgent aborts a running child and permanently clears its storage. The next creation starts with empty SQLite. Deletion also reaches the child’s nested sub-agents and clears their storage. The resetChildAfterApproval name illustrates an API location and supplies no authorization enforcement. The real entry point needs an explicit deletion decision and access control. Use abort when the requirement is only to stop execution; do not invoke permanent reset without a deletion requirement.
Inspect registration and method boundaries
| Need | Interface and boundary |
|---|---|
| Get or reuse by name | subAgent: the same class and name identify the same child |
| Inspect registered children | hasSubAgent or listSubAgents reads actual registration |
| Stop this execution | abortSubAgent keeps storage and permits later restart |
| Permanently clear storage | deleteSubAgent follows an explicit scope and authorization decision |
The parent’s onBeforeSubAgent can restrict client /sub/ requests before a child wakes. Registry existence alone does not authorize the caller; combine it with the application’s existing identity checks. No public child route is added here, and a request carrying a name is not treated as authorization.
Completion and verification
- Class and export names match, parent production binding and migration are correct, and facet-only children have no copied production migration.
- Calls use custom stub methods and actual teaching output matches input; failures are recorded separately.
- Abort and permanent deletion are separate paths; an unauthorized reset never reaches deletion, and client entry points retain identity checks.
The official sub-agent page was read to check creation, stubs, exports, bindings, storage isolation, abort and delete. No Worker was deployed, real child created or data deleted. The code shows API locations. The reset method requires real authorization checks before invocation; its name does not implement approval.
Sources
All links are official pages; verification dates are listed per source, and links point to the version checked.