|
1 | 1 | # Graph Multi-Agent Pattern |
2 | 2 |
|
3 | | -A Graph is a deterministic Directed Acyclic Graph (DAG) based agent orchestration system where agents or other multi-agent systems (like [Swarm](./swarm.md) or nested Graphs) are nodes in a graph. Nodes are executed according to edge dependencies, with output from one node passed as input to connected nodes. |
| 3 | +A Graph is a deterministic Directed Acyclic Graph (DAG) based agent orchestration system where agents, custom nodes, or other multi-agent systems (like [Swarm](./swarm.md) or nested Graphs) are nodes in a graph. Nodes are executed according to edge dependencies, with output from one node passed as input to connected nodes. |
4 | 4 |
|
5 | 5 | - **Deterministic execution order** based on DAG structure |
6 | 6 | - **Output propagation** along edges between nodes |
7 | 7 | - **Clear dependency management** between agents |
8 | 8 | - **Supports nested patterns** (Graph as a node in another Graph) |
| 9 | +- **Custom node types** for deterministic business logic and hybrid workflows |
9 | 10 | - **Conditional edge traversal** for dynamic workflows |
10 | 11 | - **Multi-modal input support** for handling text, images, and other content types |
11 | 12 |
|
12 | 13 | ## How Graphs Work |
13 | 14 |
|
14 | 15 | The Graph pattern operates on the principle of structured, deterministic workflows where: |
15 | 16 |
|
16 | | -1. Nodes represent agents or multi-agent systems |
| 17 | +1. Nodes represent agents, custom nodes, or multi-agent systems |
17 | 18 | 2. Edges define dependencies and information flow between nodes |
18 | 19 | 3. Execution follows a topological sort of the graph |
19 | 20 | 4. Output from one node becomes input for dependent nodes |
@@ -160,6 +161,57 @@ result = graph("Research the impact of AI on healthcare and create a comprehensi |
160 | 161 | print(f"\n{result}") |
161 | 162 | ``` |
162 | 163 |
|
| 164 | +## Custom Node Types |
| 165 | + |
| 166 | +You can create custom node types by extending [`MultiAgentBase`](../../../api-reference/multiagent.md#strands.multiagent.base.MultiAgentBase) to implement deterministic business logic, data processing pipelines, and hybrid workflows. |
| 167 | + |
| 168 | +```python |
| 169 | +from strands.multiagent.base import MultiAgentBase, NodeResult, Status, MultiAgentResult |
| 170 | +from strands.agent.agent_result import AgentResult |
| 171 | +from strands.types.content import ContentBlock, Message |
| 172 | + |
| 173 | +class FunctionNode(MultiAgentBase): |
| 174 | + """Execute deterministic Python functions as graph nodes.""" |
| 175 | + |
| 176 | + def __init__(self, func, name: str = None): |
| 177 | + super().__init__() |
| 178 | + self.func = func |
| 179 | + self.name = name or func.__name__ |
| 180 | + |
| 181 | + async def invoke_async(self, task, **kwargs): |
| 182 | + # Execute function and create AgentResult |
| 183 | + result = self.func(task if isinstance(task, str) else str(task)) |
| 184 | + |
| 185 | + agent_result = AgentResult( |
| 186 | + stop_reason="end_turn", |
| 187 | + message=Message(role="assistant", content=[ContentBlock(text=str(result))]), |
| 188 | + # ... metrics and state |
| 189 | + ) |
| 190 | + |
| 191 | + # Return wrapped in MultiAgentResult |
| 192 | + return MultiAgentResult( |
| 193 | + status=Status.COMPLETED, |
| 194 | + results={self.name: NodeResult(result=agent_result, ...)}, |
| 195 | + # ... execution details |
| 196 | + ) |
| 197 | + |
| 198 | +# Usage example |
| 199 | +def validate_data(data): |
| 200 | + if not data.strip(): |
| 201 | + raise ValueError("Empty input") |
| 202 | + return f"✅ Validated: {data[:50]}..." |
| 203 | + |
| 204 | +validator = FunctionNode(func=validate_data, name="validator") |
| 205 | +builder.add_node(validator, "validator") |
| 206 | +``` |
| 207 | + |
| 208 | +Custom nodes enable: |
| 209 | + |
| 210 | +- **Deterministic processing**: Guaranteed execution for business logic |
| 211 | +- **Performance optimization**: Skip LLM calls for deterministic operations |
| 212 | +- **Hybrid workflows**: Combine AI creativity with deterministic control |
| 213 | +- **Business rules**: Implement complex business logic as graph nodes |
| 214 | + |
163 | 215 | ## Multi-Modal Input Support |
164 | 216 |
|
165 | 217 | Graphs support multi-modal inputs like text and images using [`ContentBlocks`](../../../api-reference/types.md#strands.types.content.ContentBlock): |
@@ -374,3 +426,4 @@ builder.add_edge("business_specialist", "business_report") |
374 | 426 | 6. **Consider parallelism**: Independent branches can execute concurrently |
375 | 427 | 7. **Nest multi-agent patterns**: Use Swarms within Graphs for complex workflows |
376 | 428 | 8. **Leverage multi-modal inputs**: Use ContentBlocks for rich inputs including images |
| 429 | +9. **Create custom nodes for deterministic logic**: Use `MultiAgentBase` for business rules and data processing |
0 commit comments