Connect clients and call Python agent methods
Clients connect to an agent over a WebSocket with the
agents npm package. The TypeScript
SDK uses the same package, and it works with a Python agent unchanged. A
connected client receives the agent's state and can call the agent's
@callable methods.
The examples on this page assume the Worker from
Getting started, which routes /agents/... with
route_agent_request.
Connect from React
npm install agents
import { useState } from "react";
import { useAgent } from "agents/react";
export function Counter() {
const [count, setCount] = useState(0);
const agent = useAgent({
agent: "Counter",
name: "my-counter",
onStateUpdate: (state: { count: number }) => setCount(state.count),
});
return <button onClick={() => agent.stub.increment(1)}>{count}</button>;
}
agent is the binding name and name is the instance. The hook connects to
/agents/counter/my-counter on the page's own host. Set host when the
Worker runs on another origin.
Connect from Node.js or a script
AgentClient takes the same options as useAgent, plus host:
import { AgentClient } from "agents/client";
const client = new AgentClient({
agent: "Counter",
name: "my-counter",
host: "localhost:8787",
});
const count = await client.call("increment", [10]);
client.close();
client.call(method, args) sends args as positional arguments, so
call("increment", [10]) runs increment(10).
Make a method callable
Decorate an async def method with @callable. Only decorated methods can be
called from a client:
from agents import Agent, callable
class Counter(Agent):
initial_state = {"count": 0}
@callable(description="Add to the count and return the new value")
async def increment(self, by: int = 1) -> int:
self.set_state({"count": self.state["count"] + by})
return self.state["count"]
@callableworks bare or with arguments.descriptionis optional and shows up inself.get_callable_methods().- The method must be
async. A plaindefraisesTypeErrorwhen the class is defined. - Arguments and return values must be JSON-serializable. A return value of
Noneresolves the client's call withundefined. Any other value JSON can't encode rejects the call withResult is not JSON-serializable: .... - Clients pass arguments positionally only. A keyword-only parameter can't be set from a client, so give it a default.
- A subclass that overrides a callable method must decorate the override again, or the method stops being callable.
Handle errors
When a callable method raises, the SDK logs the traceback and rejects the client's call with the exception's message:
@callable
async def withdraw(self, amount: int) -> int:
if amount > self.state["balance"]:
raise ValueError("insufficient funds")
...
try {
await client.call("withdraw", [500]);
} catch (error) {
console.log(error.message); // "insufficient funds"
}
Only the message reaches the client. An exception with an empty message
sends its class name instead, so raise KeyError() sends KeyError. A call to
a method that isn't @callable fails with Method <name> is not callable, and one that
doesn't exist fails with Method <name> does not exist.
Stream results
A callable method can send several results before it finishes. Write it as an
async generator, and each yield becomes one chunk:
@callable
async def count_up(self, n: int):
for i in range(1, n + 1):
yield i
await client.call("count_up", [3], {
onChunk: (chunk) => console.log("chunk", chunk), // 1, 2, 3
onDone: () => console.log("done"),
});
To send a final value as well, declare streaming=True. The SDK passes a
StreamingResponse before the client's arguments:
from agents import StreamingResponse
@callable(streaming=True)
async def countdown(self, response: StreamingResponse, n: int) -> None:
for i in range(n, 0, -1):
response.send(i)
response.end("liftoff")
end(value) resolves the client's call with value. If the method returns
without calling end(), the SDK ends the stream for you. If it raises, the
SDK rejects the client's call with the message, as above.
send() returns False once the client has disconnected, so a long stream can
check it and stop. An async generator stops at its first yield after the
client leaves. The SDK closes it, so its finally blocks run.
Call agents from Worker code
To call an agent from your own Worker code rather than a client, use
get_agent_by_name. That
path can call any public async method, decorated or not.