NylorunDocsBeta
BuildCompose

Switch

Run exactly one case, named by the previous stage's output.

.switch() takes its cases, default among them. The previous stage's output names the case: a string, or an object with a string route field. Exactly one case runs.

import { Agent } from "@nylorun/agents";
import { z } from "zod";

const triager = Agent({ id: "triager" })
  .instructions("Classify the ticket. Return its route and a short summary.")
  .output(
    z.object({
      route: z.enum(["bug", "billing", "other"]),
      summary: z.string(),
    }),
  );

export const support = Agent({ id: "support" })
  .pipe(triager)
  .switch({ bug: bugAgent, billing: billingAgent, default: generalAgent }, { id: "route" });

A name that matches no case runs default. Without a default case, the stage fails with switch.no-match. Use an enum in the previous agent's .output() schema so the model can only return a case name. Case names are part of the stage key, so use stable names.

The chosen case receives the same input the Switch did. Each case may be an agent, an HTTP tool, a flow agent or a flow():

.switch({
  bug: flow().pipe(fixer, tester),
  docs: docsWriter,
  default: general,
})

An agent may appear in only one case; reuse it under another id with .withId(). The switch output is the chosen case's output. The Progress view of the session's Events tab in Studio shows the chosen case and leaves the others inactive.

As a folder

In a flow manifest's flow.json, a switch is { "switch": { "cases", "default" } }:

support/flow.json
{
  "chain": [
    { "agent": "triager" },
    { "id": "route", "switch": { "cases": { "bug": { "agent": "bugs" }, "billing": { "agent": "billing" } }, "default": { "agent": "general" } } }
  ]
}

Each case names an agent of the folder's agents/. Give the agent before the switch an outputSchema whose route is an enum of the case names.

See the shipped switch example, then learn how to run fixed branches in Parallel.

On this page