Events

error

{ type: "error", message, code }

The run is about to fail. It is emitted just before run() rejects — so it is a notice, not a recoverable state.

the run then rejectsraretool failures are not this
Using Python?

Switch any code block to Python: the choice applies to every page. Python SDK guide and resource reference.

Fields

messagestring

The error's message.

codestring

Its code, when it has one — context_too_large, turn_failed, unauthorized.

What is not an error event

SituationReported as
A tool threwtool_end with ok: false — the loop continues
A tool was refusedtool_end with refused: true
A turn failed and is being retriedretry
A model gave up and the next is usedfallback
A ceiling was reacheddone with stopReason
Almost everything that goes wrong is handled inside the loop

An error event means the loop itself could not continue — the turn failed on every model after every retry, the context will not fit, the session is not authorised. Each of those is a condition your code has to react to, not one the agent can work around.

Handling it

import { IaGenifyError, NotEnoughCredits } from "iagenify";

try {
  await agent.run({ inputs, onEvent });
} catch (err) {
  if (err instanceof NotEnoughCredits) {
    // Never retried, on any model. Top up.
  } else if (err instanceof IaGenifyError && err.code === "context_too_large") {
    await agent.compact();
  } else {
    throw err;
  }
}
from iagenify import IaGenifyError, NotEnoughCredits

try:
    agent.run(inputs=inputs, on_event=on_event)
except NotEnoughCredits:
    pass  # Never retried, on any model. Top up.
except IaGenifyError as error:
    if error.code != "context_too_large":
        raise
    agent.compact()

The event and the rejection carry the same thing — the event exists so an interface can show it without waiting for the promise to unwind.

A subagent that fails does not fail the run

Its failure is emitted as an error carrying parentId, and the task call ends with ok: false. The parent carries on with that as the tool's result — a delegate that could not do its job is information, not a crash.