Skip to main content

Orchestrate micro-agent swarms using Markov transition matrices as handoff patterns

Project description

AgentSwarm

License: MIT Python 3.10+ Tests

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fableforge_agent_swarm-0.1.0.tar.gz (23.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fableforge_agent_swarm-0.1.0-py3-none-any.whl (25.1 kB view details)

Uploaded Python 3

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

Hashes for fableforge_agent_swarm-0.1.0.tar.gz
Algorithm Hash digest
SHA256 f29703af6c92b94a3d604f056e7d4cd2b708850d569b14c2f6c45098e73c24d5
MD5 a8cebf32a37270ccd6347fe67eba8d3e
BLAKE2b-256 4aac8894c762be90da8049ff563e827ba2013c87702549f95c90c44e6f0ac323

See more details on using hashes here.

Provenance

The following attestation bundles were made for fableforge_agent_swarm-0.1.0.tar.gz:

Publisher: release.yml on KingLabsA/agent-swarm

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fableforge_agent_swarm-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for fableforge_agent_swarm-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f6ccddf28e75f5d4414f8bcc0c75c78f38c0d1dc9410593804c2e6be828edd56
MD5 7ce1297ec15ac46f4cc0d41d6aaa089a
BLAKE2b-256 f60e56a1bddfe05861614794c1da3fcf89f495e1c966e3919686a6eb763ede86

See more details on using hashes here.

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

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page