Networks
Combine one or more agents into a Network.
Networks are Systems of Agents. Use Networks to create powerful AI workflows by combining multiple Agents.
A network contains three components:
- The Agents that the network can use to achieve a goal
- A State including past messages and a key value store, shared between Agents and the Router
- A Router, which chooses whether to stop or select the next agent to run in the loop
Here’s a simple example:
By calling run()
, the network runs a core loop to call one or more agents to find a suitable answer.
How Networks work
Networks can be thought of as while loops with memory (State) that call Agents and Tools until the Router determines that there is no more work to be done.
Create the Network of Agents
You create a network with a list of available Agents. Each Agent can use a different model and inference provider.
Provide the staring prompt
You give the network a user prompt by calling run()
.
Core execution loop
The network runs its core loop:
Call the Network router
The Router decides the first Agent to run with your input.
Run the Agent
Call the Agent with your input. This also runs the agent’s lifecycles, and any Tools that the model decides to call.
Store the result
Stores the result in the network’s State. State can be accessed by the Router or other Agent’s Tools in future loops.
Call the the Router again ↩️
Return to the top of the loop and calls the Router with the new State. The Router can decide to quit or run another Agent.
Model configuration
A Network must provide a default model which is used for routing between Agents and for Agents that don’t have one:
A Network not defining a defaultModel
and composed of Agents without model will throw an error.
Combination of multiple models
Each Agent can specify it’s own model to use so a Network may end up using multiple models. Here is an example of a Network that defaults to use an OpenAI model, but the summaryAgent
is configured to use an Anthropic model:
Routing & maximum iterations
Routing
A Network can specify an optional defaultRouter
function that will be used to determine the next Agent to run.
Refer to the Router documentation for more information about how to create a custom Router.
Maximum iterations
A Network can specify an optional maxIter
setting to limit the number of iterations.
Specifying a maxIter
option is useful when using a Default Routing Agent or a Hybrid Router to avoid infinite loops.
A Routing Agent or Hybrid Router rely on LLM calls to make decisions, which means that they can sometimes fail to identify a final condition.
Combining maxIter
and defaultRouter
You can combine maxIter
and defaultRouter
to create a Network that will stop after a certain number of iterations or when a condition is met.
However, please note that the maxIter
option can prevent the defaultRouter
from being called (For example, if maxIter
is set to 1, the defaultRouter
will only be called once).
Providing a default State
A Network can specify an optional defaultState
setting to provide a default State.
Providing a defaultState
can be useful to persist the state in database between runs or initialize your network with external data.