API v2 · updated today

Documentation

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

EventPayloadWhen
run.startedrun_id, modelImmediately after the request is accepted
tokendeltaOnce per generated token
tool.callname, argumentsThe agent decided to invoke a tool
doneusage, finish_reasonThe 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 },
);
Demo · dados fictíciosVoltar ao portfólio