Async: answer now, deliver when ready

Your server gets a channel back immediately, so nothing waits on the AI. When the answer is ready, ModelRiver delivers it twice: live to your users' browsers and to your backend by webhook.

How an async request runs

Your request returns in milliseconds. The result follows on two paths.

  1. Your backend

    Send the request

    Your backend calls POST /v1/ai/async. Your API key stays on the server.

    POST /v1/ai/async
  2. ModelRiver

    Return a channel

    Responds right away (202) with a channel_id and a single-use ws_token.

    { "status": "pending", "channel_id": "…", "ws_token": "…" }
  3. Your frontend

    Join the channel

    Your page connects with the token, using the Client SDK. It never sees your API key.

    client.connect({ channelId, wsToken, websocketChannel })
  4. AI provider

    Generate the answer

    With streaming on, tokens reach the browser as they are written. Backups take over if the primary fails.

    stream_start → stream_delta … → stream_done
  5. ModelRiver

    Deliver the result

    The final result goes to the browser, and a signed webhook goes to your backend to save it.

    response + POST your-webhook
Your backend
POST /v1/ai/async
{
  "workflow": "support_reply",
  "stream": true,
  "messages": [
    { "role": "user", "content": "Where is my order?" }
  ]
}
Your frontend
import { ModelRiverClient } from "@modelriver/client";

const client = new ModelRiverClient();
client.on("stream_delta", (chunk, text) => render(text));
client.connect({
  channelId: res.channel_id,
  wsToken: res.ws_token,
  websocketChannel: res.websocket_channel,
});

When to use async

Use it when

  • A user is waiting on a page and should see the answer appear
  • The request can take longer than a normal HTTP timeout
  • You also want the final result saved on your side, by webhook

Pick another pattern when

  • Your server just needs a quick answer: Sync is simpler
  • The AI needs your data or rules between steps: use a backend pipeline

What ModelRiver handles

202

Instant reply

Your request returns right away, so your server never holds a connection open for the AI.

Single-use

Safe browser access

The browser token works once. Your backend can ask for a fresh one at any time.

3 attempts

Webhook delivery

Every webhook is signed, and retried twice with backoff if your endpoint fails.

2 backups

Provider failover

If the primary model fails, up to two backup models are tried before an error is sent.

Next: let your backend join the workflow