|
| 1 | +--- |
| 2 | +title: npm i agents |
| 3 | +description: Install the latest version of the `agents` SDK to build multi-agent applications, use the new RPC API, and visit the latest documentation updates. |
| 4 | +products: |
| 5 | + - agents |
| 6 | + - workers |
| 7 | +date: 2025-03-18T14:00:00Z |
| 8 | +--- |
| 9 | + |
| 10 | +import { Badge, MetaInfo, Render, TypeScriptExample } from "~/components" |
| 11 | +import { Image } from 'astro:assets'; |
| 12 | +import npmAgentsAnimated from "~/assets/images/agents/npm-i-agents.apng" |
| 13 | + |
| 14 | +<Image src={npmAgentsAnimated} alt="npm i agents" width="1000" height="541" /> |
| 15 | + |
| 16 | +#### `agents-sdk` -> `agents` <Badge text="Updated" variant="tip" size="small" /> |
| 17 | + |
| 18 | +📝 **We've renamed the Agents package to `agents`**! |
| 19 | + |
| 20 | +If you've already been building with the Agents SDK, you can update your dependencies to use the new package name, and replace references to `agents-sdk` with `agents`: |
| 21 | + |
| 22 | +```sh |
| 23 | +# Install the new package |
| 24 | +npm i agents |
| 25 | +``` |
| 26 | + |
| 27 | +```sh |
| 28 | +# Remove the old (deprecated) package |
| 29 | +npm uninstall agents-sdk |
| 30 | + |
| 31 | +# Find instances of the old package name in your codebase |
| 32 | +grep -r 'agents-sdk' . |
| 33 | +# Replace instances of the old package name with the new one |
| 34 | +# (or use find-replace in your editor) |
| 35 | +sed -i 's/agents-sdk/agents/g' $(grep -rl 'agents-sdk' .) |
| 36 | +``` |
| 37 | + |
| 38 | +All future updates will be pushed to the new `agents` package, and the older package has been marked as deprecated. |
| 39 | + |
| 40 | +#### Agents SDK updates <Badge text="New" variant="tip" size="small" /> |
| 41 | + |
| 42 | +We've added a number of big new features to the Agents SDK over the past few weeks, including: |
| 43 | + |
| 44 | +- You can now set `cors: true` when using `routeAgentRequest` to return permissive default CORS headers to Agent responses. |
| 45 | +- The regular client now syncs state on the agent (just like the React version). |
| 46 | +- `useAgentChat` bug fixes for passing headers/credentials, includng properly clearing cache on unmount. |
| 47 | +- Experimental `/schedule` module with a prompt/schema for adding scheduling to your app (with evals!). |
| 48 | +- Changed the internal `zod` schema to be compatible with the limitations of Google's Gemini models by removing the discriminated union, allowing you to use Gemini models with the scheduling API. |
| 49 | + |
| 50 | +We've also fixed a number of bugs with state synchronization and the React hooks. |
| 51 | + |
| 52 | +<TypeScriptExample> |
| 53 | + |
| 54 | +```ts |
| 55 | +// via https://github.com/cloudflare/agents/tree/main/examples/cross-domain |
| 56 | +export default { |
| 57 | + async fetch(request: Request, env: Env) { |
| 58 | + return ( |
| 59 | + // Set { cors: true } to enable CORS headers. |
| 60 | + (await routeAgentRequest(request, env, { cors: true })) || |
| 61 | + new Response("Not found", { status: 404 }) |
| 62 | + ); |
| 63 | + }, |
| 64 | +} satisfies ExportedHandler<Env>; |
| 65 | +``` |
| 66 | + |
| 67 | +</TypeScriptExample> |
| 68 | + |
| 69 | +#### Call Agent methods from your client code <Badge text="New" variant="tip" size="small" /> |
| 70 | + |
| 71 | +We've added a new [`@unstable_callable()`](/agents/api-reference/agents-api/) decorator for defining methods that can be called directly from clients. This allows you call methods from within your client code: you can call methods (with arguments) and get native JavaScript objects back. |
| 72 | + |
| 73 | +<TypeScriptExample> |
| 74 | + |
| 75 | +```ts |
| 76 | +// server.ts |
| 77 | +import { unstable_callable, Agent, type StreamingResponse } from "agents"; |
| 78 | +import type { Env } from "../server"; |
| 79 | + |
| 80 | + export class Rpc extends Agent<Env> { |
| 81 | + // Use the decorator to define a callable method |
| 82 | + @unstable_callable({ |
| 83 | + description: "rpc test", |
| 84 | + }) |
| 85 | + async getHistory() { |
| 86 | + return this.sql`SELECT * FROM history ORDER BY created_at DESC LIMIT 10`; |
| 87 | + } |
| 88 | +} |
| 89 | +``` |
| 90 | +```tsx |
| 91 | +// client.tsx |
| 92 | +const { call } = useAgent({ agent: "rpc" }); |
| 93 | + |
| 94 | +const fetchUserHistory = async () => { |
| 95 | + try { |
| 96 | + setLoading(true); |
| 97 | + // Call methods directly on the Agent! |
| 98 | + const result = await call("getHistory"); |
| 99 | + addToast(`RPC result: ${result}`, "success"); |
| 100 | + } catch (error) { |
| 101 | + addToast(`Error: ${error}`, "error"); |
| 102 | + } finally { |
| 103 | + setLoading(false); |
| 104 | + } |
| 105 | +}; |
| 106 | +``` |
| 107 | + |
| 108 | +</TypeScriptExample> |
| 109 | + |
| 110 | +#### agents-starter <Badge text="Updated" variant="tip" size="small" /> |
| 111 | + |
| 112 | +The [`agents-starter`](https://github.com/cloudflare/agents-starter) project — a real-time, chat-based example application with tool-calling & human-in-the-loop built using the Agents SDK — has seen the following updates: |
| 113 | + |
| 114 | +- Upgraded to use the latest [wrangler v4](/changelog/2025-03-13-wrangler-v4/) release. |
| 115 | +- [Workers AI](/workers-ai/) is now the default AI provider in the [`agents-starter`](https://github.com/cloudflare/agents-starter) project: this uses the new [structured outputs](/changelog/2025-02-25-json-mode/) (or "JSON mode") support now in Workers AI and the [`workers-ai-provider`](https://sdk.vercel.ai/providers/community-providers/cloudflare-workers-ai#generateobject). |
| 116 | + |
| 117 | +If you're new to Agents, you can install and run the `agents-starter` project in two commands: |
| 118 | + |
| 119 | +```sh |
| 120 | +# Install it |
| 121 | +$ npm create cloudflare@latest agents-starter -- --template="cloudflare/agents-starter" |
| 122 | +# Run it |
| 123 | +$ npm run start |
| 124 | +``` |
| 125 | + |
| 126 | +#### More documentation <Badge text="Updated" variant="tip" size="small" /> |
| 127 | + |
| 128 | +We've heard your feedback on the Agents SDK documentation, and we're shipping more API reference material and usage examples, including: |
| 129 | + |
| 130 | +- Expanded [API reference documentation](/agents/api-reference/), covering the methods and properties exposed by the Agents SDK, as well as more usage examples. |
| 131 | +- More [Client API](/agents/api-reference/agents-api/#client-api) documentation that documents `useAgent`, `useAgentChat` and the new `@unstable_callable` RPC decorator exposed by the SDK. |
| 132 | +- New documentation on how to [call agents](/agents/api-reference/calling-agents/) and (optionally) authenticate clients before they connect to your Agents. |
| 133 | + |
| 134 | +Note that the Agents SDK is continually growing: the type definitions included in the SDK will always include the latest APIs exposed by the `agents` package. |
| 135 | + |
| 136 | +If you're still wondering what Agents are, [read our blog on building AI Agents on Cloudflare](https://blog.cloudflare.com/build-ai-agents-on-cloudflare/) and/or visit the [Agents documentation](/agents/) to learn more. |
0 commit comments