Skip to content

AgentRun

The return value of agent.run(). Dual interface inspired by fetch():

  • Streaming: iterate with for await (const event of run) { ... }
  • Await result: const result = await run.result

Both can be used on the same AgentRun instance. The .result promise resolves when the session completes (after all events have been emitted).

// Streaming
for await (const event of agent.run({ input: "Hello" })) {
if (event.type === "model:chunk") process.stdout.write(event.text)
}
// Await result
const { output, cost } = await agent.run({ input: "Hello" }).result

new AgentRun(sessionId): AgentRun

string

AgentRun

readonly result: Promise<RunResult>

Promise that resolves to the final RunResult when the session completes. Rejects if the session fails with an unhandled error.

[asyncIterator](): AsyncIterator<StreamEvent>

Async iterator — yields StreamEvents as they arrive during execution.

AsyncIterator<StreamEvent>

AsyncIterable.[asyncIterator]


complete(result): void

Signal successful completion. Emits session:end, closes the stream, and resolves the .result promise.

RunResult

void


emit(event): void

Emit a stream event to all iterating consumers. Called by the agent loop.

StreamEvent

void


fail(error): void

Signal failure. Emits an error event, closes the stream, and rejects the .result promise.

Error

void