Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 55 additions & 2 deletions docs/user-guide/concepts/multi-agent/graph.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
# Graph Multi-Agent Pattern

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.
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.

- **Deterministic execution order** based on DAG structure
- **Output propagation** along edges between nodes
- **Clear dependency management** between agents
- **Supports nested patterns** (Graph as a node in another Graph)
- **Custom node types** for deterministic business logic and hybrid workflows
- **Conditional edge traversal** for dynamic workflows
- **Multi-modal input support** for handling text, images, and other content types

## How Graphs Work

The Graph pattern operates on the principle of structured, deterministic workflows where:

1. Nodes represent agents or multi-agent systems
1. Nodes represent agents, custom nodes, or multi-agent systems
2. Edges define dependencies and information flow between nodes
3. Execution follows a topological sort of the graph
4. Output from one node becomes input for dependent nodes
Expand Down Expand Up @@ -160,6 +161,57 @@ result = graph("Research the impact of AI on healthcare and create a comprehensi
print(f"\n{result}")
```

## Custom Node Types

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.

```python
from strands.multiagent.base import MultiAgentBase, NodeResult, Status, MultiAgentResult
from strands.agent.agent_result import AgentResult
from strands.types.content import ContentBlock, Message

class FunctionNode(MultiAgentBase):
"""Execute deterministic Python functions as graph nodes."""

def __init__(self, func, name: str = None):
super().__init__()
self.func = func
self.name = name or func.__name__

async def invoke_async(self, task, **kwargs):
# Execute function and create AgentResult
result = self.func(task if isinstance(task, str) else str(task))

agent_result = AgentResult(
stop_reason="end_turn",
message=Message(role="assistant", content=[ContentBlock(text=str(result))]),
# ... metrics and state
)

# Return wrapped in MultiAgentResult
return MultiAgentResult(
status=Status.COMPLETED,
results={self.name: NodeResult(result=agent_result, ...)},
# ... execution details
)

# Usage example
def validate_data(data):
if not data.strip():
raise ValueError("Empty input")
return f"✅ Validated: {data[:50]}..."

validator = FunctionNode(func=validate_data, name="validator")
builder.add_node(validator, "validator")
```

Custom nodes enable:

- **Deterministic processing**: Guaranteed execution for business logic
- **Performance optimization**: Skip LLM calls for deterministic operations
- **Hybrid workflows**: Combine AI creativity with deterministic control
- **Business rules**: Implement complex business logic as graph nodes

## Multi-Modal Input Support

Graphs support multi-modal inputs like text and images using [`ContentBlocks`](../../../api-reference/types.md#strands.types.content.ContentBlock):
Expand Down Expand Up @@ -374,3 +426,4 @@ builder.add_edge("business_specialist", "business_report")
6. **Consider parallelism**: Independent branches can execute concurrently
7. **Nest multi-agent patterns**: Use Swarms within Graphs for complex workflows
8. **Leverage multi-modal inputs**: Use ContentBlocks for rich inputs including images
9. **Create custom nodes for deterministic logic**: Use `MultiAgentBase` for business rules and data processing