Skip to main content
Clients

Client SDK

Call an agent from your backend, scripts, or any JavaScript runtime, and stream its events.

Call an agent from your backend, a script, or any JavaScript runtime with rivetkit/client.

client.tsyour backendBrowserbrowser.tstoken.tsissueTokenAgent Actoractionseventsfetch tokenconnect with token

Handles and connections

  • createClient() reads RIVET_ENDPOINT, RIVET_NAMESPACE, and RIVET_TOKEN from the environment, and defaults to a local Rivet at localhost:6420.
  • getOrCreate returns a handle to the agent with that key, and creates the agent on first use. Actions called on the handle, like getMessages, are single requests.
  • connect() opens a live connection that receives the session’s events. Actions called before it opens wait until it does.
  • A dropped connection reconnects on its own. onStatusChange reports connecting, connected, and disconnected, and idle after dispose() closes the connection for good.

Events

on("event", ...) receives every event of the session as the agent works. See Session Lifecycle for the event types.

Results and errors

  • prompt resolves when the run ends. Read the reply with getLastAssistantText or getMessages.
  • prompt rejects with an ActorError when the action itself fails. For example, another prompt is already running (pass streamingBehavior to queue it instead), the model has no credential (model_unavailable), or the run passes the ten-minute action timeout (action_timed_out).
  • A model error doesn’t reject. The run ends with an assistant message whose stopReason is "error", and errorMessage explains why.

Browsers

A browser shouldn’t hold your Rivet credentials. Your backend checks who the user is, then mints a short-lived token that reaches only their agent.

  • token.ts resolves the user’s agent and calls issueToken, which returns a token for that one agent that expires in 15 minutes.
  • browser.ts connects with getForId. getToken fetches a new token whenever the old one expires, so the connection stays up.

See JWTs and Authentication.

The same actions work over HTTP. See the curl tab in the Quickstart.

See the client documentation in the Actors docs for everything rivetkit/client can do.