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

A function router is provided to the defaultRouter option in createNetwork.

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

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

Return TypeDescription
AgentSingle agent to execute next
Agent[]Multiple agents to execute in sequence
RoutingAgentDelegate routing decision to another routing agent
undefinedStop network execution

createRoutingAgent()

Creates a new routing agent that can be used as a defaultRouter in a network.

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

name
string
required
The name of the routing agent.
description
string
Optional description of the routing agent’s purpose.
lifecycle
object
required
model
AiAdapter.Any
Optional model to use for routing decisions. If not provided, uses the network’s defaultModel.

Returns

Returns a RoutingAgent instance that can be used as a network’s defaultRouter.