Deterministic state routing
State based routing in Agent Networks
State based routing is a deterministic approach to managing agent workflows, allowing for more reliable, testable, and maintainable AI agent systems. This documentation covers the core concepts and implementation details based on the Inngest AgentKit framework.
Core Concepts
State based routing models agent workflows as a state machine where:
- Each agent has a specific goal within a larger network
- The network combines agents to achieve an overall objective, with shared state modified by each agent
- The network’s router inspects state and determines which agent should run next
- The network runs in a loop, calling the router on each iteration until all goals are met
- Agents run with updated conversation history and state on each loop iteration
Benefits
Unlike fully autonomous agents that rely on complex prompts to determine their own actions, state based routing:
- Makes agent behavior more predictable
- Simplifies testing and debugging
- Allows for easier identification of failure points
- Provides clear separation of concerns between agents
Implementation Structure
A state based routing system consists of:
- State Definition
Define structured data that represents the current progress of your workflow:
- Network and router implementation
Create a router function that inspects state and returns the appropriate agent:
A router has the following definition:
The router has access to:
input
: The original input string passed to the networknetwork
: The current network run instance with statestack
: Array of pending agents to be executedcallCount
: Number of agent invocations madelastResult
: The most recent inference result from the last agent execution
- Agent Definition
Define agents with specific goals and tools. Tools modify the network’s state. For example, a classification agent
may have a tool which updates the state’s classification
property, so that in the next network loop we can
determine which new agent to run for the classified request.
Execution Flow
When the network runs:
- The network router inspects the current state
- It returns an agent to run based on state conditions (or undefined to quit)
- The agent executes with access to previous conversation history, current state, and tools
- Tools update the state with new information
- The router runs again with updated state and conversation history
- This continues until the router returns without an agent (workflow complete)
Best Practices
- Keep agent goals focused and specific: Each agent should have a specific goal, and your network should combine agents to solve a larger problem. This makes agents easy to design and test, and it makes routing logic far easier.
- Design state to clearly represent workflow progress: Moving state out of conversation history and into structured data makes debugging agent workflows simple.
- Use tools to update state in a structured way: Tools allow you to extract structured data from agents and modify state, making routing easy.
- Implement iteration limits to prevent infinite loops: The router has a
callCount
parameter allowing you to quit early.
Error Handling
When deployed to Ingnest, AgentKit provides built-in error handling:
- Automatic retries for failed agent executions
- State persistence between retries
- Ability to inspect state at any point in the workflow
- Tracing capabilities for debugging