Skip to content

Commit 9ce4d8b

Browse files
committed
updates
1 parent 1dc8068 commit 9ce4d8b

File tree

3 files changed

+34
-14
lines changed

3 files changed

+34
-14
lines changed

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

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,11 @@ sidebar:
88

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

11-
This page shows you how to call your Agents from Workers, including approaches for how to address and route requests to Agents.
11+
Learn how to call your Agents from Workers, including how to create Agents on-the-fly, address them, and route requests to specific instances of an Agent.
1212

1313
## Calling your Agent
1414

15-
TODO:
16-
17-
- created on the fly
18-
- retrieved if exists
19-
- naming
15+
Agents are created on-the-fly and can serve multiple requests concurrently. Each Agent instance is isolated from other instances, can maintain its own state, and has a unique address.
2016

2117
You can create and run an instance of an Agent directly from a Worker in one of three ways:
2218

@@ -71,17 +67,43 @@ export class MyAgent extends Agent<Env> {
7167

7268
### Naming your Agents
7369

74-
TODO:
70+
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?
71+
72+
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.
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. 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.
7575

76-
- naming strategies
77-
- how to address them directly
78-
- validating names before you route to an agent
79-
- `this.name`
76+
<TypeScriptExample>
77+
78+
```ts
79+
import { Agent, AgentNamespace, getAgentByName, routeAgentRequest } from 'agents-sdk';
80+
81+
interface Env {
82+
MyAgent: AgentNamespace<MyAgent>;
83+
}
8084

85+
export default {
86+
async fetch(request, env, ctx): Promise<Response> {
87+
let userId = new URL(request.url).searchParams.get('userId') || 'anonymous';
88+
// Use an identifier that allows you to route to requests, WebSockets or call methods on the Agent
89+
// You can also put authentication logic here - e.g. to only create or retrieve Agents for known users.
90+
let namedAgent = getAgentByName<Env, MyAgent>(env.MyAgent, 'my-unique-agent-id');
91+
return (await namedAgent).fetch(request);
92+
},
93+
} satisfies ExportedHandler<Env>;
94+
95+
export class MyAgent extends Agent<Env> {
96+
// You can access the name of the agent via this.name in any method within
97+
// the Agent
98+
async onStartup() { console.log(`agent ${this.name} ready!`)}
99+
}
100+
```
101+
</TypeScriptExample>
81102

82103

83104
### Next steps
84105

106+
* Review the [API documentation](/agents/api-reference/sdk/) for the Agents class to learn how to define
85107
* [Build a chat Agent](/agents/getting-started/build-a-chat-agent/) using the `agents-sdk` and deploy it to Workers.
86108
* Learn more [using WebSockets](/agents/api-reference/websockets/) to build interactive Agents and stream data back from your Agent.
87109
* [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/configuration.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Configuration
33
pcx_content_type: concept
44
sidebar:
5-
order: 2
5+
order: 100
66
---
77

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

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -472,8 +472,6 @@ To learn more about how to manage state within an Agent, refer to the documentat
472472
:::
473473

474474

475-
476-
477475
### Next steps
478476

479477
* [Build a chat Agent](/agents/getting-started/build-a-chat-agent/) using the `agents-sdk` and deploy it to Workers.

0 commit comments

Comments
 (0)