AgentClient class and agentFetch function provide a framework-agnostic way to connect to agents from any JavaScript runtime: browsers, Node.js, Deno, Bun, or edge functions.
Installation
AgentClient
TheAgentClient class provides a WebSocket connection to an agent with state synchronization and RPC calls.
Basic Usage
Constructor Options
string
required
Name of the agent class to connect to. Automatically converted from camelCase to kebab-case for the URL.
string
required
Worker host for the WebSocket connection.
string
default:"default"
Name of the specific agent instance.
string
Custom path prefix for the connection URL.
Record<string, string>
Query parameters to send with the connection.
(state: State, source: 'server' | 'client') => void
Callback invoked when the agent’s state is updated.
(error: string) => void
Callback invoked when a state update fails.
(name: string, agent: string) => void
Callback invoked when the server sends the agent’s identity on connect.
(oldName: string, newName: string, oldAgent: string, newAgent: string) => void
Callback invoked when identity changes on reconnect.
Properties
string
The kebab-case agent name.
string
The agent instance name. Updated when identity is received from the server.
boolean
Whether the client has received identity from the server. Becomes
true after the first identity message is received. Resets to false on connection close.Promise<void>
Promise that resolves when identity has been received from the server. Useful for waiting before making calls that depend on knowing the instance. Resets on connection close so it can be awaited again after reconnect.
Methods
(state: State) => void
Push state updates to the agent.
<T>(method: string, args?: unknown[], options?: CallOptions | StreamOptions) => Promise<T>
Call a method on the agent.
(data: string | ArrayBuffer | Blob) => void
Send raw WebSocket message.
(code?: number, reason?: string) => void
Close the WebSocket connection. Immediately rejects all pending RPC calls.
() => void
Force reconnection.
(event: string, listener: EventListener) => void
Add event listeners for WebSocket events (inherited from PartySocket).
Calling Methods
Basic Calls
Call Options
Thecall method accepts an optional third parameter for configuring timeouts and streaming:
For backward compatibility, the legacy format with streaming options directly in the third parameter is still supported:
client.call(method, args, { onChunk, onDone, onError }).Streaming Responses
Handle streaming responses with callbacks:State Management
Receiving State Updates
Pushing State Updates
When you call
setState(), the agent broadcasts the new state to all connected clients. Your onStateUpdate callback will fire with source: "client".Connection Lifecycle
Waiting for Identity
Use theready promise to wait for the agent to send its identity:
Event Listeners
Manual Reconnection
The client automatically reconnects on connection loss using PartySocket’s reconnection logic.
Closing the Connection
Type Safety
Pass your agent class and state type as type parameters:HTTP Requests
For one-off requests without maintaining a WebSocket connection, useagentFetch:
Basic Usage
POST Request
When to Use agentFetch
Use agentFetch
- One-time requests
- Server-to-server calls
- Simple REST-style API
- No persistent connection needed
Use AgentClient
- Real-time updates needed
- Bidirectional communication
- State synchronization
- Multiple RPC calls
Examples
Simple Counter Client
Streaming AI Client
Node.js Server-to-Agent Communication
Edge Function with agentFetch
Error Handling
RPC Errors
Connection Errors
Streaming Errors
Timeout Errors
Best Practices
Always close connections when done
Always close connections when done
In long-running processes, always call
close() when you are done with the client to free up resources.Wait for ready before making calls
Wait for ready before making calls
Use the
ready promise to ensure the agent identity is received before making RPC calls.Set appropriate timeouts for long operations
Set appropriate timeouts for long operations
Use the
timeout option for operations that may take longer than expected.Use agentFetch for one-off requests
Use agentFetch for one-off requests
If you only need to make a single request, use
agentFetch instead of creating a WebSocket connection.See Also
Client SDK Overview
Learn about the full client SDK capabilities
React Hooks
Use the useAgent hook in React applications