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
Copy file name to clipboardExpand all lines: src/content/docs/agents/api-reference/calling-agents.mdx
+22-18Lines changed: 22 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -16,13 +16,12 @@ Agents are created on-the-fly and can serve multiple requests concurrently. Each
16
16
17
17
<Renderfile="unique-agents" />
18
18
19
-
You can create and run an instance of an Agent directly from a Worker in one of three ways:
19
+
You can create and run an instance of an Agent directly from a Worker using either:
20
20
21
-
1. Using the `routeAgentRequest` helper: this will automatically map requests to an individual Agent based on the `/agents/:agent/:name` URL pattern. The value of `:agent` will be the name of your Agent class converted to `kebab-case`, and the value of `:name` will be the name of the Agent instance you want to create or retrieve.
22
-
2. Calling `getAgentByName`, which will create a new Agent instance if none exists by that name, or retrieve a handle to an existing instance.
23
-
3. The [Durable Objects stub API](/durable-objects/api/id/), which provides a lower level API for creating and retrieving Agents.
21
+
* The `routeAgentRequest` helper: this will automatically map requests to an individual Agent based on the `/agents/:agent/:name` URL pattern. The value of `:agent` will be the name of your Agent class converted to `kebab-case`, and the value of `:name` will be the name of the Agent instance you want to create or retrieve.
22
+
*`getAgentByName`, which will create a new Agent instance if none exists by that name, or retrieve a handle to an existing instance.
24
23
25
-
These three patterns are shown below: we recommend using either `routeAgentRequest` or `getAgentByName`, which help avoid some boilerplate.
24
+
See the usage patterns in the following example:
26
25
27
26
<TypeScriptExample>
28
27
@@ -41,23 +40,16 @@ export default {
41
40
// Routed addressing
42
41
// Automatically routes HTTP requests and/or WebSocket connections to /agents/:agent/:name
43
42
// Best for: connecting React apps directly to Agents using useAgent from agents-sdk/react
// Best for: convenience method for creating or retrieving an agent by name/ID.
47
+
// Bringing your own routing, middleware and/or plugging into an existing
48
+
// application or framework.
48
49
let namedAgent =getAgentByName<Env, MyAgent>(env.MyAgent, 'my-unique-agent-id');
49
50
// Pass the incoming request straight to your Agent
50
51
let namedResp = (awaitnamedAgent).fetch(request);
51
-
52
-
// Durable Objects-style addressing
53
-
// Best for: controlling ID generation, associating IDs with your existing systems,
54
-
// and customizing when/how an Agent is created or invoked
55
-
const id =env.MyAgent.newUniqueId();
56
-
const agent =env.MyAgent.get(id);
57
-
// Pass the incoming request straight to your Agent
58
-
let resp =awaitagent.fetch(request);
59
-
60
-
returnResponse.json({ hello: 'visit https://developers.cloudflare.com/agents for more' });
52
+
returnnamedResp
61
53
},
62
54
} satisfiesExportedHandler<Env>;
63
55
@@ -71,9 +63,19 @@ export class MyAgent extends Agent<Env> {
71
63
72
64
When creating names for your Agents, think about what the Agent represents. A unique user? A team or company? A room or channel for collaboration?
73
65
74
-
A consistent approach to naming allows you to direct incoming requests directly to the right Agent, and deterministically route new requests back to that Agent, no matter where the client is in the world. For a given Agent definition (or 'namespace' in the code below), there can be millions (or tens of millions) of instances of that Agent, each handling their own requests, making calls to LLMs, and maintaining their own state.
66
+
A consistent approach to naming allows you to:
67
+
68
+
* direct incoming requests directly to the right Agent
69
+
* deterministically route new requests back to that Agent, no matter where the client is in the world.
70
+
* avoid having to rely on centralized session storage or external services for state management, since each Agent instance can maintain its own state.
75
71
76
-
For example, you might have an Agent for every user using your new AI-based code editor. In that case, you'd want to create Agents based on the user ID from your system, which would then allow that Agent to handle all requests for that user. It also ensures that [state within the Agent](/agents/api-reference/store-and-sync-state/), including chat history, language preferences, model configuration and other context can associated specifically with that user, making it easier to manage state.
72
+
For a given Agent definition (or 'namespace' in the code below), there can be millions (or tens of millions) of instances of that Agent, each handling their own requests, making calls to LLMs, and maintaining their own state.
73
+
74
+
For example, you might have an Agent for every user using your new AI-based code editor. In that case, you'd want to create Agents based on the user ID from your system, which would then allow that Agent to handle all requests for that user.
75
+
76
+
It also ensures that [state within the Agent](/agents/api-reference/store-and-sync-state/), including chat history, language preferences, model configuration and other context can associated specifically with that user, making it easier to manage state.
77
+
78
+
The example below shows how to create a unique agent Agent for each `userId` in a request:
77
79
78
80
<TypeScriptExample>
79
81
@@ -102,6 +104,8 @@ export class MyAgent extends Agent<Env> {
102
104
```
103
105
</TypeScriptExample>
104
106
107
+
Replace `userId` with `teamName`, `channel`, `companyName` as fits your Agents goals - and/or configure authentication to ensure Agents are only created for known, authenticated users.
108
+
105
109
### Authenticating Agents
106
110
107
111
When building and deploying Agents using the Agents SDK, you will often want to authenticate clients before passing requests to an Agent in order to restrict who the Agent will call, authorize specific users for specific Agents, and/or to limit who can access administrative or debug APIs exposed by an Agent.
Copy file name to clipboardExpand all lines: src/content/docs/agents/api-reference/http-sse.mdx
+15-3Lines changed: 15 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -93,10 +93,22 @@ export default {
93
93
};
94
94
```
95
95
96
-
97
-
98
96
</TypeScriptExample>
99
97
100
98
### WebSockets vs. Server-Sent Events
101
99
102
-
TODO
100
+
Both WebSockets and Server-Sent Events (SSE) enable real-time communication between clients and Agents. Agents built on the Agents SDK can expose both WebSocket and SSE endpoints directly.
101
+
102
+
* WebSockets provide full-duplex communication, allowing data to flow in both directions simultaneously. SSE only supports server-to-client communication, requiring additional HTTP requests if the client needs to send data back.
103
+
* WebSockets establish a single persistent connection that stays open for the duration of the session. SSE, being built on HTTP, may experience more overhead due to reconnection attempts and header transmission with each reconnection, especially when there is a lot of client-server communication.
104
+
* While SSE works well for simple streaming scenarios, WebSockets are better suited for applications requiring minutes or hours of connection time, as they maintain a more stable connection with built-in ping/pong mechanisms to keep connections alive.
105
+
* WebSockets use their own protocol (ws:// or wss://), separating them from HTTP after the initial handshake. This separation allows WebSockets to better handle binary data transmission and implement custom subprotocols for specialized use cases.
106
+
107
+
If you're unsure of which is better for your use-case, we recommend WebSockets. The [WebSockets API documentation](/agents/api-reference/websockets/) provides detailed information on how to use WebSockets with the `agents-sdk`.
108
+
109
+
### Next steps
110
+
111
+
* Review the [API documentation](/agents/api-reference/agents-api/) for the Agents class to learn how to define
112
+
*[Build a chat Agent](/agents/getting-started/build-a-chat-agent/) using the `agents-sdk` and deploy it to Workers.
113
+
* Learn more [using WebSockets](/agents/api-reference/websockets/) to build interactive Agents and stream data back from your Agent.
114
+
*[Orchestrate asynchronous workflows](/agents/api-reference/run-workflows) from your Agent by combining the `agents-sdk` and [Workflows](/workflows).
Copy file name to clipboardExpand all lines: src/content/docs/agents/api-reference/store-and-sync-state.mdx
+7Lines changed: 7 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -281,3 +281,10 @@ export class ReasoningAgent extends Agent<Env> {
281
281
</TypeScriptExample>
282
282
283
283
This works because each instance of an Agent has its _own_ database, the state stored in that database is private to that Agent: whether it's acting on behalf of a single user, a room or channel, or a deep research tool. By default, you don't have to manage contention or reach out over the network to a centralized database to retrieve and store state.
284
+
285
+
### Next steps
286
+
287
+
* Review the [API documentation](/agents/api-reference/agents-api/) for the Agents class to learn how to define
288
+
*[Build a chat Agent](/agents/getting-started/build-a-chat-agent/) using the `agents-sdk` and deploy it to Workers.
289
+
* Learn more [using WebSockets](/agents/api-reference/websockets/) to build interactive Agents and stream data back from your Agent.
290
+
*[Orchestrate asynchronous workflows](/agents/api-reference/run-workflows) from your Agent by combining the `agents-sdk` and [Workflows](/workflows).
0 commit comments