Network Router
The defaultRouter option in createNetwork defines how agents are coordinated within a Network. It can be either a Function Router or a Routing Agent.
Function Router
Section titled “Function Router”A function router is provided to the defaultRouter option in createNetwork.
Example
Section titled “Example”const network = createNetwork({ agents: [classifier, writer], router: ({ lastResult, callCount, network, stack, input }) => { // First call: use the classifier if (callCount === 0) { return classifier; }
// Get the last message from the output const lastMessage = lastResult?.output[lastResult?.output.length - 1]; const content = lastMessage?.type === "text" ? (lastMessage?.content as string) : "";
// Second call: if it's a question, use the writer if (callCount === 1 && content.includes("question")) { return writer; }
// Otherwise, we're done! return undefined; },});Parameters
Section titled “Parameters”input string The original input provided to the network.
network Network The network instance, including its state and history.
See Network.State for more details.
stack Agent[] The list of future agents to be called. (internal read-only value)
callCount number The number of agent calls that have been made.
lastResult InferenceResult The result from the previously called agent.
See InferenceResult for more details.
Return Values
Section titled “Return Values”| Return Type | Description |
|---|---|
Agent | Single agent to execute next |
Agent[] | Multiple agents to execute in sequence |
RoutingAgent | Delegate routing decision to another routing agent |
undefined | Stop network execution |
createRoutingAgent()
Section titled “createRoutingAgent()”Creates a new routing agent that can be used as a defaultRouter in a network.
Example
Section titled “Example”import { createRoutingAgent, createNetwork } from "@inngest/agent-kit";
const routingAgent = createRoutingAgent({ name: "Custom routing agent", description: "Selects agents based on the current state and request", lifecycle: { onRoute: ({ result, network }) => { // Get the agent names from the result const agentNames = result.output .filter((m) => m.type === "text") .map((m) => m.content as string);
// Validate that the agents exist return agentNames.filter((name) => network.agents.has(name)); }, },});
// classifier and writer Agents definition...
const network = createNetwork({ agents: [classifier, writer], router: routingAgent,});Parameters
Section titled “Parameters”name string required The name of the routing agent.
description string Optional description of the routing agent’s purpose.
lifecycle object required onRoute function required Called after each inference to determine the next agent(s) to call.
Arguments:
{ result: InferenceResult; // The result from the routing agent's inference agent: RoutingAgent; // The routing agent instance network: Network; // The network instance}Returns: string[] - Array of agent names to call next, or undefined to stop execution
model AiAdapter.Any Optional model to use for routing decisions. If not provided, uses the
network’s defaultModel.
Returns
Section titled “Returns”Returns a RoutingAgent instance that can be used as a network’s defaultRouter.