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.
The four patterns
Sync
SimplestYour server sends a request and gets the full AI result back in the same call.
POST /v1/aiSee how it works →Async
Live to usersGet a channel back right away. The result streams to your users and arrives at your webhook.
POST /v1/ai/asyncBackend pipeline
Your data in the loopAI steps and your backend take turns. ModelRiver calls you when a step needs your data.
POST /v1/ai/async + backend stepsSee how it works →Agent
AI calls your APIThe model decides which of your tools to call. Each tool is an endpoint of yours, and ModelRiver makes the signed calls.
POST /v1/ai/async + toolsSee how it works →How an async request runs
Your request returns in milliseconds. The result follows on two paths.
- Your backend
Send the request
Your backend calls
POST /v1/ai/asyncPOST /v1/ai/async. Your API key stays on the server. - ModelRiver
Return a channel
Responds right away (202) with a
{ "status": "pending", "channel_id": "…", "ws_token": "…" }channel_idand a single-usews_token. - 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 }) - 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 - 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
POST /v1/ai/async
{
"workflow": "support_reply",
"stream": true,
"messages": [
{ "role": "user", "content": "Where is my order?" }
]
}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.