Skip to content

featherweight.runner

runner

AgentRunner: runs an agent definition through langgraph, validates the output, retries, and logs.

ModelSetupError

Bases: SetupError

The chat model could not be built (e.g. no API key). Retrying cannot help.

EarlyAnswerError

Bases: ValueError

The model gave its final answer in the same reply as calls to tools, so it had not seen what they returned.

AgentRunner

AgentRunner(
    db: Database,
    *,
    max_attempts: int = 3,
    retry_delay_s: float = 1.0,
    max_concurrent: int | None = None,
    max_depth: int | None = None,
    max_launches: int | None = None,
    max_background_jobs: int | None = None,
    background: JobClient | None = None,
    results: ResultStore
    | None
    | Literal["default"] = "default",
    observers: Sequence[Observer] = (),
    identity_type: Any = None,
)

max_concurrent caps how many attempts (model calls) run at once across this runner. Default: FTW_MAX_CONCURRENT_ATTEMPTS from the environment, else 10.

max_depth is the most agents in one chain of launches, the first agent included (A launches B launches C is 3). Default: FTW_MAX_AGENT_DEPTH, else 3. At 1 no agent can launch another. max_launches is the most launches one attempt of one agent may make. Default: FTW_MAX_AGENT_LAUNCHES, else 10.

max_background_jobs is the most children one attempt of one agent may have running in the background at one time (FTW_MAX_BACKGROUND_JOBS, else 3). A child runs in this process, as a job of its own, unless background is a JobClient: then it is submitted to the queue and a worker runs it (the worker must list the child's job). Agents with background_jobs need a result store.

results is where the outputs that resuming a job reads back are kept (a ResultStore), for the jobs created resumable=True. With a SqliteDatabase the default is a sqlite store in the same file; with any other database there is no default, so pass your own. None keeps nothing, and then a job that resumes another is a SetupError.

observers hear what the runner does (featherweight.observers): they are called inline, each with a timeout of FTW_OBSERVER_TIMEOUT seconds (1 by default), and what they raise is logged and ignored, so they never change or fail a run.

The settings that come from the environment are read here, once (docs/specs/configuration.md).

identity_type is the type of the identity of a run (a Pydantic model: who the run is for, for the tools; docs/specs/identity.md). A declared type makes identity= required on run, run_many, stream and run_job; a union with None (Teacher | None) allows runs without one; with nothing declared, passing one is a TypeError.

aclose async

aclose() -> None

Stop every MCP server this runner started. Safe to call twice.

run async

run(
    agent_cls: type[AgentBase],
    agent_input: AgentInput,
    *,
    job_id: str | None = None,
    identity: Any = None,
) -> AgentResult[Any]

Run an agent. Runtime failures come back as a FAILED result; they are not raised.

Programming errors still raise: an input that is not the agent's input_model, a job_id that does not exist, an identity that is missing, of the wrong type, or given when no identity_type is declared, and an identity that is not the one of the job (ValueError: a job is for one identity). A streaming agent is run with stream().

identity is who the run is for. The tools read it from their RunContext, a launched agent inherits it, and it is never shown to the model.

stream

stream(
    agent_cls: type[StreamingAgent],
    chat_input: ChatInput,
    *,
    job_id: str | None = None,
    identity: Any = None,
) -> StreamingRun

Run a streaming agent. Returns at once; the run starts when you loop over it, and its events (AG-UI's) come as the model writes. A failure is an RUN_ERROR event and a FAILED row, not an exception; and nothing is retried.

A conversation with more attachments or bytes than the agent allows raises AttachmentLimitError (a BadRequest: the client's request, not a mistake of the application). Programming errors raise, as for run(): an agent that is not a streaming agent, an input that is not a ChatInput, an identity that is missing or of the wrong type, or a job_id that does not exist or a job that is for another identity (raised when the loop starts).

cancel

cancel(run_id: str) -> bool

Stop a streaming run of this runner by its id (the run_id of its RUN_STARTED event). It ends as run.cancel() does. Returns False when no such run is going on here.

run_many async

run_many(
    agent_cls: type[AgentBase],
    agent_inputs: Sequence[AgentInput],
    *,
    job_id: str | None = None,
    identity: Any = None,
) -> list[AgentResult[Any]]

Run one agent over many inputs concurrently (fan-out), all for the same identity.

Results come back in input order. As with run, runtime failures are FAILED results, not exceptions, so the caller decides what a failure means. There is no limit of its own: the runner's concurrency limit bounds how many attempts run at once.

background_message async

background_message(job_id: str) -> HumanMessage

The user message that tells a model how a background job ended, for the application to start the next turn of a chat with (history + message). It is built from the job's status and the result store, and is what the runner adds to the loop of a parent that is still running (<background_result job_id=... agent=... status=...> and the output as JSON). It says what it can: a job that has not ended has its status and no output. LookupError for a job that does not exist.

is_non_retryable

is_non_retryable(exc: BaseException) -> bool

Auth / config style errors, where another attempt would fail the same way.