Streaming tokens over SSE
What is the fastest way to add streaming to my chat app using Moon?
M
The Moon client streams by default. You iterate over the returned reader and push each chunk to the client through whatever transport your app already uses. Here is the shortest path with Server-Sent Events on Node:
// pages/api/chat.ts import { Moon } from "@moon/client"; export default async function handler(req, res) { res.setHeader("Content-Type", "text/event-stream"); const stream = await moon.chat.stream({ model: "claude-sonnet-5", messages: req.body.messages, }); for await (const chunk of stream) { res.write(`data: ${JSON.stringify(chunk)}\n\n`); } res.end(); }
First token usually lands under 120ms from iad. If you are pointing at open weights the same code works, you just swap the model.
How do I fall back to a smaller model if the primary is slow?
M
Add a fallback chain when you create the stream. Moon watches p95 latency and switches to the next entry when the leader crosses your budget