Skip to content

feat(multiagent): introduce Swarm multi-agent orchestrator #416

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
3 changes: 3 additions & 0 deletions src/strands/agent/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,9 @@ def __init__(
self.hooks.add_hook(hook)
self.hooks.invoke_callbacks(AgentInitializedEvent(agent=self))

# When True, force stops the agent's event loop
self.stop_event_loop = False
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a big fan of this existing on the agent - it's sort of a per-request state living off of the agent that complicates complicates restarting the agent.

Can we use exceptions for this case instead? StopAgentException?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good call, will do in a following commit


@property
def tool(self) -> ToolCaller:
"""Call tool as a function.
Expand Down
2 changes: 1 addition & 1 deletion src/strands/event_loop/event_loop.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ def tool_handler(tool_use: ToolUse) -> ToolGenerator:
tracer = get_tracer()
tracer.end_event_loop_cycle_span(span=cycle_span, message=message, tool_result_message=tool_result_message)

if invocation_state["request_state"].get("stop_event_loop", False):
if agent.stop_event_loop or invocation_state["request_state"].get("stop_event_loop", False):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we remove the request_state stop_event_loop logic here? I don't like that there is a special, undocumented key to enable this behavior.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah I will replace this with a StopAgentException

agent.event_loop_metrics.end_cycle(cycle_start_time, cycle_trace)
yield {"stop": (stop_reason, message, agent.event_loop_metrics, invocation_state["request_state"])}
return
Expand Down
4 changes: 4 additions & 0 deletions src/strands/multiagent/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@
from . import a2a
from .base import MultiAgentBase, MultiAgentResult
from .graph import GraphBuilder, GraphResult
from .swarm import Swarm, SwarmConfig, SwarmResult

__all__ = [
"a2a",
"GraphBuilder",
"GraphResult",
"MultiAgentBase",
"MultiAgentResult",
"Swarm",
"SwarmConfig",
"SwarmResult",
]
10 changes: 8 additions & 2 deletions src/strands/multiagent/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,15 @@ def get_agent_results(self) -> list[AgentResult]:

@dataclass
class MultiAgentResult:
"""Result from multi-agent execution with accumulated metrics."""
"""Result from multi-agent execution with accumulated metrics.

results: dict[str, NodeResult]
The status field represents the outcome of the MultiAgentBase execution:
- COMPLETED: The execution was successfully accomplished
- FAILED: The execution failed or produced an error
"""

status: Status = Status.PENDING
results: dict[str, NodeResult] = field(default_factory=lambda: {})
accumulated_usage: Usage = field(default_factory=lambda: Usage(inputTokens=0, outputTokens=0, totalTokens=0))
accumulated_metrics: Metrics = field(default_factory=lambda: Metrics(latencyMs=0))
execution_count: int = 0
Expand Down
10 changes: 2 additions & 8 deletions src/strands/multiagent/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,8 @@ class GraphState:

@dataclass
class GraphResult(MultiAgentResult):
"""Result from graph execution - extends MultiAgentResult with graph-specific details.
"""Result from graph execution - extends MultiAgentResult with graph-specific details."""

The status field represents the outcome of the graph execution:
- COMPLETED: The graph execution was successfully accomplished
- FAILED: The graph execution failed or produced an error
"""

status: Status = Status.PENDING
total_nodes: int = 0
completed_nodes: int = 0
failed_nodes: int = 0
Expand Down Expand Up @@ -468,12 +462,12 @@ def _build_node_input(self, node: GraphNode) -> str:
def _build_result(self) -> GraphResult:
"""Build graph result from current state."""
return GraphResult(
status=self.state.status,
results=self.state.results,
accumulated_usage=self.state.accumulated_usage,
accumulated_metrics=self.state.accumulated_metrics,
execution_count=self.state.execution_count,
execution_time=self.state.execution_time,
status=self.state.status,
total_nodes=self.state.total_nodes,
completed_nodes=len(self.state.completed_nodes),
failed_nodes=len(self.state.failed_nodes),
Expand Down
Loading
Loading