Customize how calls are routed between Agents in a Network.
Agent
)undefined
)defaultRouter
function returning either an instance of an Agent
object or undefined
.
import { createNetwork } from "@inngest/agent-kit";
// classifier and writer Agents definition...
const network = createNetwork({
agents: [classifier, writer],
router: ({ lastResult, callCount }) => {
// retrieve the last message from the output
const lastMessage = lastResult?.output[lastResult?.output.length - 1];
const content =
lastMessage?.type === "text" ? (lastMessage?.content as string) : "";
// First call: use the classifier
if (callCount === 0) {
return classifier;
}
// Second call: if it's a question, use the writer
if (callCount === 1 && content.includes("question")) {
return writer;
}
// Otherwise, we're done!
return undefined;
},
});
defaultRouter
function receives a number of arguments:
interface RouterArgs {
network: Network; // The entire network, including the state and history
stack: Agent[]; // Future agents to be called
callCount: number; // Number of times the Network has called agents
lastResult?: InferenceResult; // The the previously called Agent's result
}
undefined
stops the network’s execution loopimport { createNetwork } from "@inngest/agent-kit";
// classifier and writer Agents definition...
const network = createNetwork({
agents: [classifier, writer],
router: ({ lastResult, callCount }) => {
// retrieve the last message from the output
const lastMessage = lastResult?.output[lastResult?.output.length - 1];
const content =
lastMessage?.type === "text" ? (lastMessage?.content as string) : "";
// First call: use the classifier
if (callCount === 0) {
return classifier;
}
// Second call: if it's a question, use the writer
if (callCount === 1 && content.includes("question")) {
return writer;
}
// Otherwise, we're done!
return undefined;
},
});
defaultRouter
defined, the network will use the “Default Routing Agent” to decide which agent to call next.
The “Default Routing Agent” is a Routing Agent provided by Agent Kit to handle the default routing logic.
You can create your own Routing Agent by using the createRoutingAgent
helper function:
import { createRoutingAgent } 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 }) => {
// custom logic...
},
},
});
// classifier and writer Agents definition...
const network = createNetwork({
agents: [classifier, writer],
router: routingAgent,
});
onRoute
lifecycle method.import { createNetwork, getDefaultRoutingAgent } from "@inngest/agent-kit";
// classifier and writer Agents definition...
const network = createNetwork({
agents: [classifier, writer],
router: ({ callCount }) => {
// Always start with the classifier
if (callCount === 0) {
return classifier;
}
// Then let the routing agent take over
return getDefaultRoutingAgent();
},
});
import { createNetwork } from '@inngest/agent-kit';
// mathAgent and contextAgent Agents definition...
const network = createNetwork({
agents: [mathAgent, contextAgent],
router: ({ network, lastResult }): Agent | undefined => {
// Check if we've solved the problem
const solution = network.state.data.solution;
if (solution) {
// We're done - return undefined to stop the network
return undefined;
}
// retrieve the last message from the output
const lastMessage = lastResult?.output[lastResult?.output.length - 1];
const content = lastMessage?.type === 'text' ? lastMessage?.content as string : '';
// Check the last result to decide what to do next
if (content.includes('need more context')) {
return contextAgent;
}
return mathAgent;
};
});