Skip to content

Commit 1dc8068

Browse files
committed
calling agents
1 parent 0841ae8 commit 1dc8068

File tree

3 files changed

+87
-60
lines changed

3 files changed

+87
-60
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
---
2+
title: Calling Agents
3+
pcx_content_type: concept
4+
sidebar:
5+
order: 2
6+
7+
---
8+
9+
import { MetaInfo, Render, Type, TypeScriptExample, WranglerConfig } from "~/components";
10+
11+
This page shows you how to call your Agents from Workers, including approaches for how to address and route requests to Agents.
12+
13+
## Calling your Agent
14+
15+
TODO:
16+
17+
- created on the fly
18+
- retrieved if exists
19+
- naming
20+
21+
You can create and run an instance of an Agent directly from a Worker in one of three ways:
22+
23+
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.
24+
2. Calling `getAgentByName`, which will create a new Agent instance if none exists by that name, or retrieve a handle to an existing instance.
25+
3. The [Durable Objects stub API](/durable-objects/api/id/), which provides a lower level API for creating and retrieving Agents.
26+
27+
These three patterns are shown below: we recommend using either `routeAgentRequest` or `getAgentByName`, which help avoid some boilerplate.
28+
29+
<TypeScriptExample>
30+
31+
```ts
32+
import { Agent, AgentNamespace, getAgentByName, routeAgentRequest } from 'agents-sdk';
33+
34+
interface Env {
35+
// Define your Agent on the environment here
36+
// Passing your Agent class as a TypeScript type parameter allows you to call
37+
// methods defined on your Agent.
38+
MyAgent: AgentNamespace<MyAgent>;
39+
}
40+
41+
export default {
42+
async fetch(request, env, ctx): Promise<Response> {
43+
// Routed addressing
44+
// Automatically routes HTTP requests and/or WebSocket connections to /agents/:agent/:name
45+
// Best for: connecting React apps directly to Agents using useAgent from agents-sdk/react
46+
(await routeAgentRequest(request, env)) || Response.json({ msg: 'no agent here' }, { status: 404 });
47+
48+
// Named addressing
49+
// Best for: convenience method for creating or retrieving an agent by name/ID.
50+
let namedAgent = getAgentByName<Env, MyAgent>(env.MyAgent, 'my-unique-agent-id');
51+
// Pass the incoming request straight to your Agent
52+
let namedResp = (await namedAgent).fetch(request);
53+
54+
// Durable Objects-style addressing
55+
// Best for: controlling ID generation, associating IDs with your existing systems,
56+
// and customizing when/how an Agent is created or invoked
57+
const id = env.MyAgent.newUniqueId();
58+
const agent = env.MyAgent.get(id);
59+
// Pass the incoming request straight to your Agent
60+
let resp = await agent.fetch(request);
61+
62+
return Response.json({ hello: 'visit https://developers.cloudflare.com/agents for more' });
63+
},
64+
} satisfies ExportedHandler<Env>;
65+
66+
export class MyAgent extends Agent<Env> {
67+
// Your Agent implementation goes here
68+
}
69+
```
70+
</TypeScriptExample>
71+
72+
### Naming your Agents
73+
74+
TODO:
75+
76+
- naming strategies
77+
- how to address them directly
78+
- validating names before you route to an agent
79+
- `this.name`
80+
81+
82+
83+
### Next steps
84+
85+
* [Build a chat Agent](/agents/getting-started/build-a-chat-agent/) using the `agents-sdk` and deploy it to Workers.
86+
* Learn more [using WebSockets](/agents/api-reference/websockets/) to build interactive Agents and stream data back from your Agent.
87+
* [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/sdk.mdx

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -471,58 +471,7 @@ To learn more about how to manage state within an Agent, refer to the documentat
471471

472472
:::
473473

474-
## Routing to Agents
475474

476-
You can create and run an instance of an Agent directly from a Worker in one of three ways:
477-
478-
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.
479-
2. Calling `getAgentByName`, which will create a new Agent instance if none exists by that name, or retrieve a handle to an existing instance.
480-
3. The [Durable Objects stub API](/durable-objects/api/id/), which provides a lower level API for creating and retrieving Agents.
481-
482-
These three patterns are shown below: we recommend using either `routeAgentRequest` or `getAgentByName`, which help avoid some boilerplate.
483-
484-
<TypeScriptExample>
485-
486-
```ts
487-
import { Agent, AgentNamespace, getAgentByName, routeAgentRequest } from 'agents-sdk';
488-
489-
interface Env {
490-
// Define your Agent on the environment here
491-
// Passing your Agent class as a TypeScript type parameter allows you to call
492-
// methods defined on your Agent.
493-
MyAgent: AgentNamespace<MyAgent>;
494-
}
495-
496-
export default {
497-
async fetch(request, env, ctx): Promise<Response> {
498-
// Routed addressing
499-
// Automatically routes HTTP requests and/or WebSocket connections to /agents/:agent/:name
500-
// Best for: connecting React apps directly to Agents using useAgent from agents-sdk/react
501-
(await routeAgentRequest(request, env)) || Response.json({ msg: 'no agent here' }, { status: 404 });
502-
503-
// Named addressing
504-
// Best for: convenience method for creating or retrieving an agent by name/ID.
505-
let namedAgent = getAgentByName<Env, MyAgent>(env.MyAgent, 'my-unique-agent-id');
506-
// Pass the incoming request straight to your Agent
507-
let namedResp = (await namedAgent).fetch(request);
508-
509-
// Durable Objects-style addressing
510-
// Best for: controlling ID generation, associating IDs with your existing systems,
511-
// and customizing when/how an Agent is created or invoked
512-
const id = env.MyAgent.newUniqueId();
513-
const agent = env.MyAgent.get(id);
514-
// Pass the incoming request straight to your Agent
515-
let resp = await agent.fetch(request);
516-
517-
return Response.json({ hello: 'visit https://developers.cloudflare.com/agents for more' });
518-
},
519-
} satisfies ExportedHandler<Env>;
520-
521-
export class MyAgent extends Agent<Env> {
522-
// Your Agent implementation goes here
523-
}
524-
```
525-
</TypeScriptExample>
526475

527476

528477
### Next steps

src/content/docs/agents/platform/llms.txt.mdx

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

0 commit comments

Comments
 (0)