Skip to content

Commit 7416e63

Browse files
committed
limits, schedules
1 parent 84eef5f commit 7416e63

File tree

4 files changed

+109
-46
lines changed

4 files changed

+109
-46
lines changed
Lines changed: 56 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,69 @@
11
---
2-
title: Agents SDK
2+
title:
33
pcx_content_type: concept
44
sidebar:
5-
order: 2
5+
order: 3
66

77
---
88

99
import { MetaInfo, Render, Type, WranglerConfig } from "~/components";
1010

11-
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.
1212

13-
## Agent
13+
The typical file structure for an Agent project created from `npm create cloudflare@latest -- --template cloudflare/agents` follows:
1414

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+
```
1631

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`:
1833

19-
```ts
20-
import { Agent } from "@cloudflare/agents";
34+
<WranglerConfig>
2135

22-
export class MyAgent extends Agent<Env> {
23-
// methods here
24-
async onRequest(req: Request) {
25-
26-
}
27-
};
28-
```
36+
```jsonc
37+
{
38+
"$schema": "node_modules/wrangler/config-schema.json",
39+
"name": "agents-example",
40+
"main": "src/index.ts",
41+
"compatibility_date": "2025-02-23",
42+
"compatibility_flags": ["nodejs_compat"],
43+
"durable_objects": {
44+
"bindings": [
45+
{
46+
"name": "MyAgent",
47+
"class_name": "MyAgent",
48+
},
49+
],
50+
},
51+
"migrations": [
52+
{
53+
"tag": "v1",
54+
"new_sqlite_classes": ["MyAgent"], // Mandatory
55+
},
56+
],
57+
"observability": {
58+
"enabled": true,
59+
},
60+
}
61+
```
62+
63+
</WranglerConfig>
64+
65+
The configuration includes:
66+
67+
- A `main` field that points to the entry point of your Agent, which is typically a TypeScript (or JavaScript) file.
68+
- A `durable_objects` field that defines the [Durable Object namespace](/durable-objects/reference/glossary/) that your Agents will run within.
69+
- A `migrations` field that defines the code migrations that your Agent will use. This field is mandatory and must contain at least one migration.
Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,11 @@
11
---
2-
title: Configuration
2+
title: Agents SDK
33
pcx_content_type: concept
44
sidebar:
5-
order: 3
5+
order: 2
66

77
---
88

99
import { MetaInfo, Render, Type, WranglerConfig } from "~/components";
1010

11-
TODO - how to configure an Agent
12-
13-
<WranglerConfig>
14-
15-
```json
16-
{
17-
"name": "my-agent",
18-
"script": "src/index.ts",
19-
// @cloudflare/agents uses Durable Objects
20-
"durable_objects": {
21-
"bindings": [
22-
{
23-
"binding": "MyAgent",
24-
"class_name": "MyAgent"
25-
}
26-
]
27-
}
28-
}
29-
```
30-
31-
</WranglerConfig>
11+
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.

src/content/docs/agents/examples/schedule-tasks.mdx

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,29 @@ title: Schedule tasks
33
pcx_content_type: concept
44
sidebar:
55
order: 5
6-
76
---
87

98
import { MetaInfo, Render, Type, TypeScriptExample, WranglerConfig } from "~/components";
109

11-
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.
1213

1314
### Scheduling tasks
1415

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.
1617

1718
<TypeScriptExample>
1819

1920
```ts
2021
// schedule a task to run in 10 seconds
21-
this.schedule(10, "myTask", { message: "hello" });
22+
let task = await this.schedule(10, "myTask", { message: "hello" });
2223

2324
// schedule a task to run at a specific date
24-
this.schedule(new Date("2025-01-01"), "myTask", { message: "hello" });
25+
let task = await this.schedule(new Date("2025-01-01"), "myTask", { message: "hello" });
2526

2627
// schedule a task to run every 10 seconds
27-
this.schedule("*/10 * * * *", "myTask", { message: "hello" });
28+
let { id } = await this.schedule("*/10 * * * *", "myTask", { message: "hello" });
2829

2930
// schedule a task to run every 10 seconds, but only on Mondays
3031
let task = await this.schedule("0 0 * * 1", "myTask", { message: "hello" });
@@ -35,6 +36,13 @@ this.cancelSchedule(task.id);
3536

3637
</TypeScriptExample>
3738

39+
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+
:::
3846

3947
### Managing scheduled tasks
4048

@@ -44,17 +52,20 @@ You can get, cancel and filter across scheduled tasks within an Agent using the
4452

4553
```ts
4654
// Get a specific schedule by ID
55+
// Returns undefined if the task does not exist
4756
let task = await this.getSchedule(task.id)
4857

4958
// Get all scheduled tasks
50-
let tasks = await this.getScheduledTasks();
59+
// Returns an array of Schedule objects
60+
let tasks = this.getSchedules();
5161

5262
// Cancel a task by its ID
63+
// Returns true if the task was cancelled, false if it did not exist
5364
await this.cancelSchedule(task.id);
5465

5566
// Filter for specific tasks
5667
// e.g. all tasks starting in the next hour
57-
let tasks = this.getSchedules({ // returns an iterator
68+
let tasks = this.getSchedules({
5869
timeRange: {
5970
start: new Date(Date.now()),
6071
end: new Date(Date.now() + 60 * 60 * 1000),
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---
2+
pcx_content_type: concept
3+
title: Limits
4+
sidebar:
5+
order: 4
6+
7+
---
8+
9+
import { Render } from "~/components"
10+
11+
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.
14+
15+
::: note
16+
17+
| Feature | Limit |
18+
| ----------------------------------------- | ----------------------- |
19+
| 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.
30+
31+
<Render file="limits_increase" product="workers" />

0 commit comments

Comments
 (0)