Orchestrate micro-agent swarms using Markov transition matrices as handoff patterns
Project description
AgentSwarm
Orchestrate micro-agent swarms using Markov transition matrices derived from real Fable5 trace data.
Overview
AgentSwarm models agent coordination as a Markov chain: each agent's next tool call is predicted by transition probabilities learned from real coding sessions. Instead of hardcoded orchestration logic, the swarm uses probabilistic handoff patterns that mirror how skilled developers switch between reading, editing, running commands, and verifying.
Key Transition Probabilities (Fable5 Data)
| Transition | Probability | Interpretation |
|---|---|---|
| Bash → Bash | 0.59 | Agents loop on shell commands |
| Bash → Edit | 0.18 | Shell work leads to file edits |
| Read → Bash | 0.37 | Reading triggers command execution |
| Read → Edit | 0.22 | Reading precedes editing |
| Edit → Bash | 0.34 | Edits trigger verification |
| Edit → Read | 0.28 | Edits lead to re-reading |
Architecture
┌─────────────────────────────────────────────┐
│ SwarmOrchestrator │
│ ┌──────────┐ TransitionMatrix ┌──────┐ │
│ │ Planner │ ──────────────────→ │Reader│ │
│ └────┬─────┘ └──┬───┘ │
│ │ │ │
│ ▼ ▼ │
│ ┌──────────┐ handoff() ┌──────┐ │
│ │ Editor │ ←──────────────── │Bash │ │
│ └────┬─────┘ └──┬───┘ │
│ │ │ │
│ ▼ │ │
│ ┌──────────┐ │ │
│ │Verifier │ ←──────────────────────┘ │
│ └──────────┘ │
└─────────────────────────────────────────────┘
Quick Start
Installation
pip install agent-swarm
For development:
git clone https://github.com/example/agent-swarm.git
cd agent-swarm
pip install -e ".[dev]"
Run a Task
swarm run "Fix the authentication bug in auth.py"
Check Status
swarm status
Visualize the Swarm
swarm visualize
Build Custom Transition Matrix
swarm build-matrix traces.jsonl -o my_matrix.json
Programmatic Usage
Basic Usage
from agent_swarm import SwarmOrchestrator, TransitionMatrix
# Use the default matrix (derived from Fable5 data)
orchestrator = SwarmOrchestrator()
# Or load from trace data
tm = TransitionMatrix.from_traces("my_traces.jsonl")
orchestrator = SwarmOrchestrator(transition_matrix=tm)
# Run a task through the swarm
result = orchestrator.run("Implement user authentication")
print(result.summary())
print(f"Total handoffs: {result.total_handoffs}")
print(f"Final agent: {result.final_agent}")
Spawn and Coordinate Agents
from agent_swarm import SwarmOrchestrator
orchestrator = SwarmOrchestrator()
# Spawn individual agents
reader = orchestrator.spawn_agent("reader")
editor = orchestrator.spawn_agent("editor")
# Coordinate a task
task = orchestrator.coordinate("Fix the login bug")
# Predict the next agent
next_agent = orchestrator.predict_next_agent("reader", current_tool="read")
# → "editor" or "bash" (based on transition probabilities)
Handoffs with Transition Data
# Hand off between agents with context enrichment
handoff = orchestrator.handoff(
from_agent="reader",
to_agent="editor",
context={"findings": "Auth bug is in token validation", "files": ["auth.py"]},
)
# The handoff record includes transition data
print(handoff.context["handoff_probability"]) # e.g., 0.35
print(handoff.context["handoff_pattern"]) # Tool call sequence
Agent Execution
from agent_swarm.agents import create_agent
# Create and execute with an agent
reader = create_agent("reader")
result = reader.execute("Find the authentication module")
print(result["plan"]) # Planned tool calls
print(result["recommended_handoff"]) # Next agent suggestion
Transition Matrix API
Predict Next Tool
from agent_swarm import TransitionMatrix
tm = TransitionMatrix()
# Top-3 predictions after "read"
predictions = tm.next_tool("read", top_k=3)
# → [ToolCall(name='bash', confidence=0.37),
# ToolCall(name='edit', confidence=0.22),
# ToolCall(name='grep', confidence=0.20)]
# Get specific transition probability
prob = tm.get_transition_prob("bash", "bash")
# → 0.59
Get Handoff Patterns
# Get the tool-call sequence for a reader→editor handoff
pattern = tm.get_handoff_pattern("reader", "editor")
# → [ToolCall(name='read', confidence=0.92),
# ToolCall(name='edit', confidence=0.88)]
# Get the probability of this handoff
prob = tm.get_handoff_probability("reader", "editor")
# → 0.35
# Get all handoff probabilities from a role
probs = tm.get_all_handoff_probabilities("planner")
# → {"reader": 0.25, "editor": 0.30, "bash": 0.15, ...}
Build from Traces
# Build from a JSONL trace file
tm = TransitionMatrix.from_traces("agent_traces.jsonl", min_occurrences=5)
# Save for later use
tm.to_json("my_matrix.json")
# Load later
tm = TransitionMatrix.from_json("my_matrix.json")
Micro-Agents
| Agent | Role | Tools | Handoff Targets | Key Transition |
|---|---|---|---|---|
| ReaderAgent | Explore & understand code | read, grep, glob |
editor, bash, verifier, planner | Read→Edit=0.22 |
| EditorAgent | Write & modify code | edit, write |
reader, bash, verifier, planner | Edit→Bash=0.34 |
| BashAgent | Execute commands | bash |
reader, editor, verifier, planner | Bash→Bash=0.59 |
| VerifierAgent | Test & validate changes | bash, read, grep |
reader, editor, bash, planner | Verify→Edit=0.25 |
| PlannerAgent | Plan & coordinate | question, glob, read |
reader, editor, bash, verifier | Plan→Read=0.25 |
Pydantic Models
The models module provides Pydantic v2 models for serialization and validation:
- AgentConfig — Configuration for spawning agents (role, tools, prompt, model settings)
- SwarmResult — Result of swarm execution with handoff history and output
- HandoffEvent — Record of an agent handoff with probability and pattern
- AgentMessage — Message in the agent conversation
Testing
# Run all tests
pytest tests/
# Run with verbose output
pytest tests/ -v
# Run specific test class
pytest tests/test_orchestrator.py::TestTransitionMatrix -v
License
MIT
Ecosystem
Part of the FableForge ecosystem — 21 open-source projects built from 210K real agent traces:
| Project | Description |
|---|---|
| Anvil | Self-verified coding agent |
| VerifyLoop | Plan→Execute→Verify→Recover framework |
| ErrorRecovery | Self-healing middleware (3,725 error patterns) |
| FableForge-14B | The fine-tuned 14B model (4-stage training) |
| ShellWhisperer | 1.5B edge agent (phone/RPi, 50ms) |
| ReasonCritic | Verification model (130 benchmark tasks) |
| TraceCompiler | Compile traces → LoRA skills |
| AgentRuntime | Persistent agent daemon (systemd for AI) |
| AgentSwarm | Multi-agent from real trace transitions |
| AgentTelemetry | Datadog for agents (token tracking, costs) |
| BenchAgent | HumanEval for tool-use (107 tasks) |
| AgentDev | VSCode extension with verification |
| TraceViz | Trace replay visualizer (Next.js) |
| AgentSkills | npm for agent behaviors |
| AgentCurriculum | 5-stage progressive training |
| AgentFuzzer | Adversarial testing for agents |
| AgentConstitution | Safety guardrails from traces |
| CostOptimizer | Token cost reduction (50-80%) |
| AgentProfiler | Behavioral fingerprinting |
| TrajectoryDistiller | Trace→training data pipeline |
| Fable5-Dataset | HuggingFace dataset release |
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fableforge_agent_swarm-0.1.0.tar.gz.
File metadata
- Download URL: fableforge_agent_swarm-0.1.0.tar.gz
- Upload date:
- Size: 23.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f29703af6c92b94a3d604f056e7d4cd2b708850d569b14c2f6c45098e73c24d5
|
|
| MD5 |
a8cebf32a37270ccd6347fe67eba8d3e
|
|
| BLAKE2b-256 |
4aac8894c762be90da8049ff563e827ba2013c87702549f95c90c44e6f0ac323
|
Provenance
The following attestation bundles were made for fableforge_agent_swarm-0.1.0.tar.gz:
Publisher:
release.yml on KingLabsA/agent-swarm
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
fableforge_agent_swarm-0.1.0.tar.gz -
Subject digest:
f29703af6c92b94a3d604f056e7d4cd2b708850d569b14c2f6c45098e73c24d5 - Sigstore transparency entry: 1819791481
- Sigstore integration time:
-
Permalink:
KingLabsA/agent-swarm@9a25d9b48227e3795ec6198b9bd63834b3507fe9 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/KingLabsA
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@9a25d9b48227e3795ec6198b9bd63834b3507fe9 -
Trigger Event:
push
-
Statement type:
File details
Details for the file fableforge_agent_swarm-0.1.0-py3-none-any.whl.
File metadata
- Download URL: fableforge_agent_swarm-0.1.0-py3-none-any.whl
- Upload date:
- Size: 25.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f6ccddf28e75f5d4414f8bcc0c75c78f38c0d1dc9410593804c2e6be828edd56
|
|
| MD5 |
7ce1297ec15ac46f4cc0d41d6aaa089a
|
|
| BLAKE2b-256 |
f60e56a1bddfe05861614794c1da3fcf89f495e1c966e3919686a6eb763ede86
|
Provenance
The following attestation bundles were made for fableforge_agent_swarm-0.1.0-py3-none-any.whl:
Publisher:
release.yml on KingLabsA/agent-swarm
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
fableforge_agent_swarm-0.1.0-py3-none-any.whl -
Subject digest:
f6ccddf28e75f5d4414f8bcc0c75c78f38c0d1dc9410593804c2e6be828edd56 - Sigstore transparency entry: 1819791650
- Sigstore integration time:
-
Permalink:
KingLabsA/agent-swarm@9a25d9b48227e3795ec6198b9bd63834b3507fe9 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/KingLabsA
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@9a25d9b48227e3795ec6198b9bd63834b3507fe9 -
Trigger Event:
push
-
Statement type: