Streaming
Render tokens as the model produces them.
Streaming turns a 6-second wait into a 300 ms one, perceptually. The API emits server-sent events, one per delta, and closes the stream with a usage summary.
Consuming a stream
TS
const stream = await nexus.agents.stream({
model: "nexus-1-turbo",
input: prompt,
});
for await (const event of stream) {
if (event.type === "token") process.stdout.write(event.delta);
if (event.type === "done") console.log("\n", event.usage);
}Event types
| Event | Payload | When |
|---|---|---|
| run.started | run_id, model | Immediately after the request is accepted |
| token | delta | Once per generated token |
| tool.call | name, arguments | The agent decided to invoke a tool |
| done | usage, finish_reason | The run settled or was cancelled |
Cancelling
Abort the underlying request and the run is billed only for tokens produced so far. The Playground's Stop button does exactly this.
TS
const controller = new AbortController();
setTimeout(() => controller.abort(), 5_000);
await nexus.agents.stream(
{ model: "nexus-1-turbo", input: prompt },
{ signal: controller.signal },
);