Skip to content

@Router

The @Router decorator defines a control-flow decision node (routers/*.router.ts) in AGBoss. Routers decouple orchestration from domain agents and come in two forms discriminated by decidedBy: DecidedBy.Model (where a fast classifier model like typesafe/jev-1.13 picks the target route from prompt criteria) and DecidedBy.Code (where deterministic TypeScript in route(input) with constructor DI picks the target at zero LLM cost and zero latency).

Complete @Router examples (DecidedBy.Model and DecidedBy.Code)

Section titled “Complete @Router examples (DecidedBy.Model and DecidedBy.Code)”

Every @Router class name ends with Router and its name option ends with -router:

Option Applies To Type Required Description
name Both string Yes Unique kebab-case identifier ending with -router (e.g., "main-router").
description Both string Yes Describes what decision this router makes.
decidedBy Both DecidedBy.Model | DecidedBy.Code Yes Selects whether an LLM/decision model or a TypeScript route(input) method chooses the branch.
input Both InputFields Yes Non-empty map of DTOs read from Path Scope ([router.empty-input] if empty).
routes Both readonly [RouterRoute, ...] Yes List of allowed target components; must match the targets in .chooseOne(route(...)) or .toParallelAgents(...) ([router.routes-mismatch]).
maxVisits Both number No Maximum visits per execution before throwing VisitsLimitExceededError.
model DecidedBy.Model string No Decision model ("typesafe/jev-1.13") or chat model; inherits defaults.router.model when omitted.
prompt / promptUrls DecidedBy.Model string / readonly string[] No Instructions telling the model how to evaluate the input and select a route.
promptVariables DecidedBy.Model Readonly<Record<string, string>> No Static {{key}} variables interpolated into the router’s prompt.
piiPolicies / excludePiiPolicies DecidedBy.Model readonly Class[] No @PiiPolicy classes masking inputs before they reach the router model.
guardrails / excludeGuardrails DecidedBy.Model readonly Class[] No @InputGuardrail classes evaluated before the router model runs.
compaction / uncompactedTurns DecidedBy.Model Class / number | TurnLimit No Overrides thread compaction or recent turn window for this router.
deps DecidedBy.Code readonly Token[] No Constructor dependencies (ExecutionContext, @Injectable services) injected into the code router class.

Each item in routes configures a branch target for .chooseOne(route(...)) or .toParallelAgents(...):

Route Field Type Description
target ComponentClass Target @Agent, @Action, @Router, or @WorkflowFinish class.
prompt string Required on DecidedBy.Model routes with SpawnPolicy.OnDemand; describes when the model should select target. Forbidden on DecidedBy.Code routes.
spawn SpawnPolicy.Always | SpawnPolicy.OnDemand Used before .toParallelAgents(...): Always launches unconditionally alongside chosen branches; OnDemand launches only when chosen by the router.
failure FailurePolicy.FailFast | FailurePolicy.Skip Used before .toParallelAgents(...): whether a failure in this target fails the parallel join (FailFast, default) or drops out (Skip).
Related Entity Import Path Purpose
DecidedBy, SpawnPolicy, FailurePolicy "@agboss/core" Enums controlling decision mode (Model vs Code), parallel launch selection (Always vs OnDemand), and branch failure handling (FailFast vs Skip).
RouterHandler<Self> & Route<Self> "@agboss/core" Contract and return type for DecidedBy.Code routers (route(input) returns one target for .chooseOne() or one/many targets for .toParallelAgents()).
.toRouter(R).chooseOne(route(Target)...) "@agboss/core" Connects a router to exclusive branch paths in @Workflow({ flow }).
.toRouter(R).toParallelAgents(...) "@agboss/core" Uses a router to dynamically select which subset of SpawnPolicy.OnDemand agents to launch in parallel alongside SpawnPolicy.Always agents.
RouterFailedError & RouterDecisionError "@agboss/core" Errors thrown when a router cannot select a valid route; catchable in the flow via .onError(RouterFailedError, sidePath).
testRouter(RouterClass, options?) "@agboss/core/testing" Unit runner that tests a model or code router in isolation without booting the workflow.