Skip to content

Commit 715d7f2

Browse files
committed
updates
1 parent f1844bd commit 715d7f2

File tree

5 files changed

+52
-190
lines changed

5 files changed

+52
-190
lines changed

src/content/docs/agents/api-reference/calling-agents.mdx

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,12 @@ Agents are created on-the-fly and can serve multiple requests concurrently. Each
1616

1717
<Render file="unique-agents" />
1818

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:
2020

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

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:
2625

2726
<TypeScriptExample>
2827

@@ -41,23 +40,16 @@ export default {
4140
// Routed addressing
4241
// Automatically routes HTTP requests and/or WebSocket connections to /agents/:agent/:name
4342
// Best for: connecting React apps directly to Agents using useAgent from agents-sdk/react
44-
(await routeAgentRequest(request, env)) || Response.json({ msg: 'no agent here' }, { status: 404 });
43+
return (await routeAgentRequest(request, env)) || Response.json({ msg: 'no agent here' }, { status: 404 });
4544

4645
// Named addressing
4746
// 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.
4849
let namedAgent = getAgentByName<Env, MyAgent>(env.MyAgent, 'my-unique-agent-id');
4950
// Pass the incoming request straight to your Agent
5051
let namedResp = (await namedAgent).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 = await agent.fetch(request);
59-
60-
return Response.json({ hello: 'visit https://developers.cloudflare.com/agents for more' });
52+
return namedResp
6153
},
6254
} satisfies ExportedHandler<Env>;
6355

@@ -71,9 +63,19 @@ export class MyAgent extends Agent<Env> {
7163

7264
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?
7365

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

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:
7779

7880
<TypeScriptExample>
7981

@@ -102,6 +104,8 @@ export class MyAgent extends Agent<Env> {
102104
```
103105
</TypeScriptExample>
104106

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+
105109
### Authenticating Agents
106110

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

src/content/docs/agents/api-reference/client-api.mdx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,11 @@ import { useAgent } from "agents-sdk/react";
7171
## Call remote methods
7272

7373
TODO
74+
75+
76+
### Next steps
77+
78+
* Review the [API documentation](/agents/api-reference/agents-api/) for the Agents class to learn how to define
79+
* [Build a chat Agent](/agents/getting-started/build-a-chat-agent/) using the `agents-sdk` and deploy it to Workers.
80+
* Learn more [using WebSockets](/agents/api-reference/websockets/) to build interactive Agents and stream data back from your Agent.
81+
* [Orchestrate asynchronous workflows](/agents/api-reference/run-workflows) from your Agent by combining the `agents-sdk` and [Workflows](/workflows).

src/content/docs/agents/api-reference/http-sse.mdx

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,22 @@ export default {
9393
};
9494
```
9595

96-
97-
9896
</TypeScriptExample>
9997

10098
### WebSockets vs. Server-Sent Events
10199

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

src/content/docs/agents/api-reference/store-and-sync-state.mdx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,3 +281,10 @@ export class ReasoningAgent extends Agent<Env> {
281281
</TypeScriptExample>
282282

283283
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).

src/content/docs/agents/getting-started/quickstart.mdx

Lines changed: 0 additions & 169 deletions
This file was deleted.

0 commit comments

Comments
 (0)