Skip to main content
An agent runs in a Restate handler, so your application sends requests to that handler through the Restate ingress: over HTTP, or with the TypeScript client @restatedev/restate-sdk-clients, which runs in Node and in the browser.

Call the agent

The examples on this page call the following Virtual Object. Its ask handler runs the agent with the conversation of the chat ID, which is the object key:
Call ask with the chat ID in the path. The response contains the agent’s answer:
With the TypeScript client, create a client from the object definition:
  • Requests to the same chat ID run one at a time: Restate queues a request until the running ask finishes. To add input to a running request instead, steer it.
  • The shared history handler runs immediately, also while ask runs. It returns the saved messages from index from on, and next, the index to read from next time. To show what the agent does during a run, publish progress events.
The client of the agent reference architecture follows a conversation as it grows: it reads the history in pages, and then waits for changes with a long poll. The AG-UI example serves a conversation and its progress to AG-UI clients, such as CopilotKit.

Long runs

A run can take minutes, for example while it waits for a human approval. If the HTTP connection breaks during that time, for example because a proxy times out, the run continues in Restate, but the client doesn’t receive the answer. Send such requests with an idempotency key, and attach to them to wait for the answer:
Restate responds as soon as it accepts the request, with the invocation ID, for example {"invocationId": "inv_1gdJBtdVEcM942bjcDmb1c1khoaJe11Hbz", "status": "Accepted"}. Attach with the invocation ID:
With the TypeScript client:
A request with the same idempotency key attaches to the running request, or returns its stored answer, instead of adding another turn to the conversation. Restate keeps the answer for the idempotency retention period, one day by default. Use a new idempotency key for each new message. To attach with the idempotency key instead of the invocation ID, or to let the TypeScript client retry calls, see Attach to an invocation and Automatic retries.

Errors

  • A retryable error, such as a crash of the service, doesn’t reach the client. Restate retries the invocation, the run continues from its journal, and the client keeps waiting.
  • The TypeScript client throws an HttpCallError with the status and the response body in responseText.
  • An error with the source invocation is the final outcome of the invocation. Sending the request again with the same idempotency key returns the same error. Sending it without an idempotency key starts a new run, which adds another turn to the conversation. See Retrying requests.
  • The conversation keeps a failed turn, with a note on why it failed. See Failed runs.

Expose the agent to a browser

The Restate ingress doesn’t authenticate your users: a self-hosted ingress accepts every request, and Restate Cloud checks only your API key, not who the user is. A caller that reaches the ingress can call every public handler with any key, so a browser that calls the ingress directly can read and write every user’s chats. See Security. Put your backend between the browser and Restate. The backend authenticates the user, chooses the chat ID from the user’s identity, calls only the handlers that the UI needs, and keeps Restate credentials, such as a Restate Cloud API key, out of the browser. The web app of the agent reference architecture routes the browser’s requests through a Next.js route, which maps a fixed set of operations to the agent’s handlers. It doesn’t authenticate users, so add authentication before you deploy it. Mark handlers that only your services call as private, so that the ingress rejects requests for them.