|
| 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). |
0 commit comments