Abort + timeouts

Cancel agent loops and provider calls with AbortSignal. Timeouts share the same path and surface as AiError code ABORTED.

Agent abort

abort-agent.ts
import { agent, AiError, mock } from "@monorch/ai";

const bot = agent({ name: "abortable", model: mock([{ text: "hi" }]) });
const ctrl = new AbortController();

const pending = bot.stream("long task", { signal: ctrl.signal });
ctrl.abort();

try {
  for await (const _ of pending) {
    // may not yield if aborted early
  }
} catch (e) {
  if (e instanceof AiError && e.code === "ABORTED") {
    // run_end status was "aborted"
  }
}

Provider timeout

timeout.ts
import { openai } from "@monorch/ai/openai";

const model = openai("gpt-4.1-mini", { timeoutMs: 15_000 });
// or per call:
await model.generate({
  messages: [{ role: "user", content: "hi" }],
  timeoutMs: 5_000,
});

Notes

Default openai timeout is 60s. Pre-aborted signals throw before model I/O. Full catalog: Errors & failure modes.

FAQ