Skip to main content
Agent Clients

Call an Agent from Your Backend

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

The client is the rivetkit/client package. It calls actions on the agent and gets the agent’s events over a connection. A browser connects with a short-lived token from your backend.

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

Handles and connections

  • With no options, createClient() reads RIVET_ENDPOINT, RIVET_NAMESPACE, and RIVET_TOKEN from the environment, and defaults to a local Rivet at localhost:6420.
  • The getOrCreate method returns a handle to the agent with that key, and creates the agent on first use. Actions called on the handle, like conversation.context, are single requests.
  • An agent starts with one root conversation. The harness.root() action returns its id, and prompt uses it unless you pass a conversationId.
  • Calling connect() opens a live connection that can receive events. Actions called before it opens wait until it does.
  • A dropped connection reconnects on its own. The onStatusChange callback reports connecting, connected, and disconnected, and idle after dispose() closes the connection for good.

Events

Events go only to a connection that asks for them. Call conversation.watch(id) on the connection. It returns the conversation’s view as { value }, including any answer in progress, and then the connection receives pi.view frames as the agent works.

  • Each frame after the first value has Chord operations in ops. Apply them with applyImmutable from @earendil-works/chord/delta, which you install with npm add @earendil-works/chord.
  • The view holds the newest entries of the transcript. Read older ones with conversation.entries.
  • The seq number goes up by one for each frame. If a number is missing, call conversation.watch again to get a new view.
  • To stop listening, call the function that on returns, or conversation.unwatch(id).

See Stream a conversation for what the view holds.

Results and errors

The prompt action resolves with { status, text } when the run ends, and its requestId and whenBusy options decide what a resent prompt or a prompt during a run does. See Call the agent.

  • The prompt action rejects with an ActorError when the action itself fails. For example, the run passes the ten-minute action timeout (action_timed_out). A timeout only stops the wait.
  • To start a run without waiting for it, call conversation.submit(id, { type: "input", content }). Follow its progress with conversation.watch.

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.

  • In token.ts, your backend resolves the user’s agent and calls issueToken, which returns a token for that one agent that expires in 15 minutes.
  • In browser.ts, the client connects with getForId, and 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.