# Multi-Agent Coordination When you have multiple LLM agents working on related tasks, Synapse provides the coordination layer — shared memory, task assignment, and async chat. ## Patterns ### Pattern 1: Shared Mind (Single Source of Truth) All agents share one Mind Key. They read/write the same memory store. ``` ┌──────────┐ ┌──────────┐ ┌──────────┐ │ Agent A │ │ Agent B │ │ Agent C │ └────┬─────┘ └────┬─────┘ └────┬─────┘ │ │ │ └─────────────┼─────────────┘ ▼ ┌──────────────┐ │ Shared Mind │ │ (one key) │ └──────────────┘ ``` **Use case:** Small team of agents working on one project. **Setup:** ```bash # All agents use the same Mind Key export SYNAPSE_MIND_KEY=mk_shared_key... ``` **Coordination via tasks:** ```python # Agent A creates a task create_task("Review PR #42", priority="high") # Agent B picks it up tasks = list_tasks(status="pending") if tasks: task = tasks[0] update_task(task["id"], status="in_progress") # ... do work ... update_task(task["id"], status="done") ``` ### Pattern 2: Specialized Minds (Isolated Contexts) Each agent has its own mind. They communicate via a shared "coordination" mind. ``` ┌──────────┐ ┌──────────┐ ┌──────────┐ │ Coder │ │ Reviewer │ │ Deployer │ │ Agent │ │ Agent │ │ Agent │ └────┬─────┘ └────┬─────┘ └────┬─────┘ │ │ │ ▼ ▼ ▼ ┌─────────┐ ┌─────────┐ ┌─────────┐ │ Mind C │ │ Mind R │ │ Mind D │ └─────────┘ └─────────┘ └─────────┘ │ │ │ └─────────────┼─────────────┘ ▼ ┌──────────────────┐ │ Coordination Mind│ │ (shared) │ └──────────────────┘ ``` **Use case:** Agents with different specialties (coding, review, deployment). **Setup:** ```bash # Coder agent SYNAPSE_MIND_KEY=mk_coder... MCP_TRANSPORT=stdio npx synapse-mcp-api@latest # Reviewer agent SYNAPSE_MIND_KEY=mk_reviewer... MCP_TRANSPORT=stdio npx synapse-mcp-api@latest # Deployer agent SYNAPSE_MIND_KEY=mk_deployer... MCP_TRANSPORT=stdio npx synapse-mcp-api@latest ``` **Coordination via shared mind:** ```python # Coder stores "ready for review" COORDINATION_KEY = "mk_coordination..." requests.post(f"{URL}/memory", headers={"Authorization": f"Bearer {COORDINATION_KEY}"}, json={ "category": "project", "key": "pr_42_ready", "content": "PR #42 is ready for review. Branch: feature/docs-system", "tags": ["review", "pr-42"], "priority": "high" }) # Reviewer polls for review requests r = requests.get(f"{URL}/memory/search?q=ready+for+review", headers={"Authorization": f"Bearer {COORDINATION_KEY}"}) ``` ### Pattern 3: Hub-and-Spoke (Orchestrator) A central orchestrator agent assigns tasks to worker agents. ``` ┌──────────────┐ │ Orchestrator │ │ Agent │ └──────┬───────┘ │ ┌──────────┼──────────┐ ▼ ▼ ▼ ┌──────┐ ┌──────┐ ┌──────┐ │Worker│ │Worker│ │Worker│ │ A │ │ B │ │ C │ └──────┘ └──────┘ └──────┘ ``` **Use case:** Complex workflows with parallel work. **Implementation:** ```python # Orchestrator class Orchestrator: def assign_task(self, worker_id, task_description): # Store task in worker's mind (or shared coordination mind) create_task(task_description, priority="high") # Notify worker via chat reply(f"@{worker_id}: New task — {task_description}") def check_progress(self): tasks = list_tasks(status="in_progress") for t in tasks: print(f"{t['title']}: {t['status']}") # Workers poll for assigned tasks class Worker: def run(self): while True: tasks = list_tasks(status="pending") for t in tasks: if assigned_to_me(t): update_task(t["id"], status="in_progress") result = do_work(t) update_task(t["id"], status="done") reply(f"Completed: {t['title']}") time.sleep(60) ``` ## Coordination via Chat Agents can communicate via the chat system: ```python # Agent A sends to Agent B reply("@agent-b: Can you review my PR?") # Agent B polls and responds for msg in poll_messages(): if "@agent-b" in msg["content"]: reply(f"@agent-a: Sure, looking at it now.") ``` > [!NOTE] > Chat messages are role-tagged. Set role=agent for agent-to-agent messages, > role=human for human-to-agent. ## Coordination via Variables Use variables for lightweight coordination (locks, flags): ```python # Acquire a lock def acquire_lock(name): r = requests.post(f"{URL}/var", headers={"Authorization": f"Bearer {KEY}"}, json={"key": f"lock_{name}", "value": "acquired"}) return True def release_lock(name): requests.delete(f"{URL}/var/lock_{name}", headers={"Authorization": f"Bearer {KEY}"}) # Use if acquire_lock("deploy"): try: deploy_to_production() finally: release_lock("deploy") ``` ## Best Practices > [!TIP] > - **Use separate minds for separate concerns** — don't mix coder and reviewer memory > - **Tag agents in chat** — `@agent-name` for clear addressing > - **Use tasks for work assignment** — not chat (chat is for discussion) > - **Implement idempotency** — agents may retry failed operations > - **Log everything** — store decisions in memory for auditability ## Next Steps - [Persistent LLM Agent](/docs/guides/persistent-llm-agent) - [LLM Cookbook](/docs/llm-cookbook/session-start-pattern) - [Webhook Automation](/docs/guides/webhook-automation)