You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This guide details the Agents API within Cloudflare Workers, including methods, types, and usage examples.
11
+
An Agent is configured like any other Cloudflare Workers project, and uses [a wrangler configuration](/workers/wrangler/configuration/) file to define where your code is and what services (bindings) it will use.
12
12
13
-
## Agent
13
+
The typical file structure for an Agent project created from `npm create cloudflare@latest -- --template cloudflare/agents` follows:
14
14
15
-
TODO
15
+
```sh
16
+
.
17
+
|-- package-lock.json
18
+
|-- package.json
19
+
|-- public
20
+
|`-- index.html
21
+
|-- src
22
+
|`-- index.ts // your Agent definition
23
+
|-- test
24
+
||-- index.spec.ts // your tests
25
+
|`-- tsconfig.json
26
+
|-- tsconfig.json
27
+
|-- vitest.config.mts
28
+
|-- worker-configuration.d.ts
29
+
`-- wrangler.jsonc // your Workers & Agent configuration
30
+
```
16
31
17
-
The `Agent` class is the core element of a Agent definition:
32
+
Below is a minimal `wrangler.jsonc` file that defines the configuration for an Agent, including the entry point, `durable_object` namespace, and code `migrations`:
Each Agent is a JavaScript class that extends the `Agent` class from the `@cloudflare/workers-agents` package. Agents are designed to run in a separate thread from the main Worker, allowing them to perform long-running tasks without blocking the main thread.
An Agent can schedule tasks to be run in the future by calling `this.schedule(when, callback, data)`, where `when` can be a delay, a `Date`, or a cron string; callback the function name to call, and data is an object of data to pass to the function.
10
+
An Agent can schedule tasks to be run in the future by calling `this.schedule(when, callback, data)`, where `when` can be a delay, a `Date`, or a cron string; `callback` the function name to call, and `data` is an object of data to pass to the function.
11
+
12
+
Scheduled tasks can do anything a request or message from a user can: make requests, query databases, send emails, read+write state, etc.
12
13
13
14
### Scheduling tasks
14
15
15
-
You can call `this.schedule` within any method on an Agent:
16
+
You can call `this.schedule` within any method on an Agent, and schedule tens-of-thousands of tasks per individual Agent.
Calling `await this.schedule` returns a `Schedule`, which includes the task's randomly generated `id`. You can use this `id` to retrieve or cancel the task in the future.
40
+
41
+
:::note[Maximum scheduled tasks]
42
+
43
+
Each task is mapped to a row in the Agent's underlying [SQLite database](/sqlite-in-durable-objects/), which means that each task can be up to 2MB in size. The maximum number of tasks must be `(task_size * tasks) + all_other_state < maximum_database_size` (currently 1GB per Agent).
44
+
45
+
:::
38
46
39
47
### Managing scheduled tasks
40
48
@@ -44,17 +52,20 @@ You can get, cancel and filter across scheduled tasks within an Agent using the
44
52
45
53
```ts
46
54
// Get a specific schedule by ID
55
+
// Returns undefined if the task does not exist
47
56
let task =awaitthis.getSchedule(task.id)
48
57
49
58
// Get all scheduled tasks
50
-
let tasks =awaitthis.getScheduledTasks();
59
+
// Returns an array of Schedule objects
60
+
let tasks =this.getSchedules();
51
61
52
62
// Cancel a task by its ID
63
+
// Returns true if the task was cancelled, false if it did not exist
53
64
awaitthis.cancelSchedule(task.id);
54
65
55
66
// Filter for specific tasks
56
67
// e.g. all tasks starting in the next hour
57
-
let tasks =this.getSchedules({// returns an iterator
Limits that apply to authoring, deploying, and running Agents are detailed below.
12
+
13
+
Many limits are inherited from those applied to Workers scripts and/or Durable Objects, and are detailed in the [Workers limits](/workers/platform/limits/) documentation.
| Max concurrent (running) Agents per account | Tens of millions+ [^1]
20
+
| Max definitions per account | ~250,000+ [^2]
21
+
| Max state stored per unique Agent | 1GB |
22
+
| Max compute time per Agent | 30 seconds (refreshed per HTTP request / incoming WebSocket message) [^3]|
23
+
| Duration (wall clock) per step [^3]| Unlimited (e.g. waiting on a database call or an LLM response) |
24
+
25
+
---
26
+
27
+
[^1]: Yes, really. You can have tens of millions of Agents running concurrently, as each Agent is mapped to a [unique Durable Object](/durable-objects/what-are-durable-objects/) (actor).
28
+
[^2]: You can deploy up to [500 scripts per account](/workers/platform/limits/), but each script (project) can define multiple Agents. Each deployed script can be up to 10MB on the [Workers Paid Plan](/workers/platform/pricing/#workers)
29
+
[^3]: Compute (CPU) time per Agent is limited to 30 seconds, but this is refreshed when an Agent receives a new HTTP request, runs a [scheduled task](/agents/examples/schedule-tasks/), or an incoming WebSocket message.
0 commit comments