multi-agent-patterns-advanced

Advanced multi-agent patterns — capability registry, durable state (Redis/DynamoDB), task decomposition, testing multi-agent systems, pattern quick-selection guide, failure handling, cost management, and worktree isolation. Extends multi-agent-patterns.

8 stars

Best use case

multi-agent-patterns-advanced is best used when you need a repeatable AI agent workflow instead of a one-off prompt.

Advanced multi-agent patterns — capability registry, durable state (Redis/DynamoDB), task decomposition, testing multi-agent systems, pattern quick-selection guide, failure handling, cost management, and worktree isolation. Extends multi-agent-patterns.

Teams using multi-agent-patterns-advanced should expect a more consistent output, faster repeated execution, less prompt rewriting.

When to use this skill

  • You want a reusable workflow that can be run more than once with consistent structure.

When not to use this skill

  • You only need a quick one-off answer and do not need a reusable workflow.
  • You cannot install or maintain the underlying files, dependencies, or repository context.

Installation

Claude Code / Cursor / Codex

$curl -o ~/.claude/skills/multi-agent-patterns-advanced/SKILL.md --create-dirs "https://raw.githubusercontent.com/marvinrichter/clarc/main/skills/multi-agent-patterns-advanced/SKILL.md"

Manual Installation

  1. Download SKILL.md from GitHub
  2. Place it in .claude/skills/multi-agent-patterns-advanced/SKILL.md inside your project
  3. Restart your AI agent — it will auto-discover the skill

How multi-agent-patterns-advanced Compares

Feature / Agentmulti-agent-patterns-advancedStandard Approach
Platform SupportNot specifiedLimited / Varies
Context Awareness High Baseline
Installation ComplexityUnknownN/A

Frequently Asked Questions

What does this skill do?

Advanced multi-agent patterns — capability registry, durable state (Redis/DynamoDB), task decomposition, testing multi-agent systems, pattern quick-selection guide, failure handling, cost management, and worktree isolation. Extends multi-agent-patterns.

Where can I find the source code?

You can find the source code on GitHub using the link provided at the top of the page.

SKILL.md Source

# Multi-Agent Patterns — Advanced

> See the core `multi-agent-patterns` skill for basics: orchestration vs choreography, intent classification routing, in-memory state, handoffs, fan-out/fan-in, Claude SDK loop, and observability.

## When to Activate

- Building a capability registry for dynamic agent dispatch
- Implementing durable cross-agent state (Redis, DynamoDB event log)
- Decomposing complex goals into parallel and sequential sub-tasks
- Writing deterministic tests for multi-agent workflows
- Selecting the right pattern from the quick-selection guide
- Managing failure modes across agent boundaries
- Controlling token cost in large multi-agent systems
- Isolating parallel file-editing agents with git worktrees

---

## Advanced Tool Routing: Capability Registry

```typescript
interface AgentCapability {
  id: string;
  description: string;
  inputSchema: z.ZodSchema;
  agent: (input: unknown) => Promise<AgentResult>;
}

const capabilities: AgentCapability[] = [
  {
    id: 'analyze_code',
    description: 'Review code for bugs, style, and security issues',
    inputSchema: z.object({ code: z.string(), language: z.string() }),
    agent: codeReviewAgent,
  },
  {
    id: 'generate_tests',
    description: 'Generate unit tests for a function or class',
    inputSchema: z.object({ code: z.string(), framework: z.string() }),
    agent: tddAgent,
  },
];

// Pass registry to orchestrator as tools
const tools = capabilities.map(cap => ({
  name: cap.id,
  description: cap.description,
  input_schema: zodToJsonSchema(cap.inputSchema),
}));
```

---

## Durable State

### External Store (Redis)

```typescript
// Redis for durable cross-agent state
import { createClient } from 'redis';

const redis = createClient();

async function saveAgentState(workflowId: string, state: WorkflowState): Promise<void> {
  await redis.setEx(
    `workflow:${workflowId}`,
    3600,  // 1 hour TTL
    JSON.stringify(state)
  );
}

async function loadAgentState(workflowId: string): Promise<WorkflowState | null> {
  const raw = await redis.get(`workflow:${workflowId}`);
  return raw ? JSON.parse(raw) : null;
}
```

### Event Log (Append-Only, Auditable)

```typescript
// DynamoDB event log for full audit trail
async function appendEvent(workflowId: string, event: WorkflowEvent): Promise<void> {
  await dynamodb.put({
    TableName: 'workflow-events',
    Item: {
      workflowId,
      timestamp: Date.now(),
      sequenceNumber: await getNextSequence(workflowId),
      event,
    },
  });
}

// Reconstruct state by replaying events
async function replayWorkflow(workflowId: string): Promise<WorkflowState> {
  const events = await queryAllEvents(workflowId);
  return events.reduce(applyEvent, initialState());
}
```

---

## Task Decomposition Handoff

```typescript
interface SubTask {
  id: string;
  description: string;
  dependsOn: string[];  // IDs of tasks that must complete first
  agent: string;
}

async function decomposeTasks(goal: string): Promise<SubTask[]> {
  const response = await claude.messages.create({
    model: 'claude-opus-latest',  // Opus for complex planning
    system: 'Decompose the goal into parallel and sequential sub-tasks. Output JSON.',
    messages: [{ role: 'user', content: goal }],
    max_tokens: 2048,
  });
  return JSON.parse(response.content[0].text);
}
```

---

## Testing Multi-Agent Systems

```typescript
// Mock sub-agents for deterministic tests
class MockAgent {
  constructor(private responses: Map<string, string>) {}

  async run(input: string): Promise<string> {
    const key = [...this.responses.keys()].find(k => input.includes(k));
    if (!key) throw new Error(`No mock response for input: ${input}`);
    return this.responses.get(key)!;
  }
}

describe('Orchestrator', () => {
  it('routes code review tasks to code-review agent', async () => {
    const mockCodeReviewer = new MockAgent(new Map([
      ['review this function', '{ "issues": [] }'],
    ]));

    const orchestrator = new Orchestrator({
      agents: { 'code-review': mockCodeReviewer },
    });

    const result = await orchestrator.run('Please review this function: ...');
    expect(result).toContain('issues');
  });

  it('handles partial failures gracefully', async () => {
    const failingAgent = { run: () => Promise.reject(new Error('timeout')) };
    const fallbackAgent = new MockAgent(new Map([['', 'fallback result']]));

    const result = await runWithFallback(
      () => failingAgent.run(),
      () => fallbackAgent.run(''),
      1
    );
    expect(result).toBe('fallback result');
  });
});
```

---

## Worktree Isolation

When multiple agents edit files in parallel, use git worktrees to prevent conflicts:

```bash
# Create isolated worktrees for parallel agents
git worktree add ../feature-agent-1 -b agent/feature-1
git worktree add ../feature-agent-2 -b agent/feature-2

# Each agent works in its own directory — no conflicts
# After completion, merge results back
git merge agent/feature-1
git merge agent/feature-2

# Clean up
git worktree remove ../feature-agent-1
git worktree remove ../feature-agent-2
```

**When to use:** Any orchestration pattern where 2+ agents modify files concurrently (parallel feature development, multi-file refactors).

---

## Pattern Quick-Selection & Token Budget

| Task Type | Pattern | Agents | Context per agent |
|-----------|---------|--------|-------------------|
| Multi-file review | Fan-Out → Fan-In | 3–10 | Minimal (target-specific) |
| Architecture decision | Split-Role | 2–4 | Full task context |
| Unknown codebase research | Explorer + Validator | 2 | Explorer: broad; Validator: targeted |
| Parallel feature development | Worktree Isolation | 2–5 | Feature-specific only |
| Full feature TDD cycle | Sequential Pipeline | 3–7 | Output of previous stage |
| Security + quality + tests | Parallel Fan-Out | 3 | Specialist context only |

---

## Failure Handling & Result Synthesis

| Scenario | Action |
|----------|--------|
| Agent timeout | Retry with reduced scope; split task into smaller chunks |
| Conflicting results | Pass both to a tiebreaker agent; apply: security > quality > style |
| Partial failures | Complete successful agents; re-run failed with error context |
| Context overflow | Summarize intermediate results before passing to next stage |

---

## Cost Management

- Use **Haiku** for routing, summarization, and classification (high-frequency, low-complexity)
- Use **Sonnet** for main agent work (default)
- Use **Opus** only for planning and architectural decisions
- Pass minimal context per agent — compress handoffs with Summary Handoff pattern
- Set `max_tokens` budgets per agent tier to cap runaway costs
- Use `parallelWithConcurrencyLimit` (default: 5) to avoid rate-limit overages

> For the base patterns (orchestrator loop, fan-out, error handling, observability) see `multi-agent-patterns`.

Related Skills

zero-trust-patterns

8
from marvinrichter/clarc

Zero-Trust security patterns — mTLS between microservices (Istio/SPIFFE), SPIRE workload identity, OPA/Envoy authorization, NetworkPolicy default-deny-all, short-lived credentials, service mesh security, and Kubernetes RBAC hardening.

webrtc-patterns

8
from marvinrichter/clarc

WebRTC patterns — peer connection setup, ICE/STUN/TURN configuration, signaling server design, SFU vs mesh topology, screen sharing, media track management, and reconnect/ICE restart handling.

webhook-patterns

8
from marvinrichter/clarc

Webhook patterns for receiving, verifying (HMAC), and idempotently processing third-party events. Covers Stripe, GitHub, and generic webhook patterns, delivery guarantees, retry handling, and testing.

wasm-patterns

8
from marvinrichter/clarc

WebAssembly patterns: wasm-pack, wasm-bindgen (JS↔Wasm interop), WASI, Component Model, wasm-opt, Rust-to-WASM compilation, JS integration (web workers, streaming instantiation), and production deployment (CDN, Content-Type headers).

ux-micro-patterns

8
from marvinrichter/clarc

UX micro-patterns for every product state: Empty States, Loading States (skeleton screens, spinners, optimistic UI), Error States, Success States, Confirmation Dialogs, Onboarding Flows, and Progressive Disclosure. These patterns apply to every feature — done wrong, they're the biggest source of user confusion.

typescript-patterns

8
from marvinrichter/clarc

TypeScript patterns — type system best practices, strict mode, utility types, generics, discriminated unions, error handling with Result types, and module organization. Core patterns for production TypeScript.

typescript-patterns-advanced

8
from marvinrichter/clarc

Advanced TypeScript — mapped types, template literal types, conditional types, infer, type guards, decorators, async patterns, testing with Vitest/Jest, and performance. Extends typescript-patterns.

typescript-monorepo-patterns

8
from marvinrichter/clarc

TypeScript monorepo patterns with Turborepo + pnpm workspaces. Covers package structure, shared configs, task pipeline caching, build orchestration, and publishing strategy.

terraform-patterns

8
from marvinrichter/clarc

Infrastructure as Code with Terraform — project structure, remote state, modules, workspace strategy, AWS/GCP patterns, CI/CD integration, and security hardening. The standard for managing production infrastructure.

tdd-workflow-advanced

8
from marvinrichter/clarc

TDD anti-patterns — writing code before tests, testing implementation details instead of behavior, using waitForTimeout as a sync strategy, chaining tests that share state, mocking the system under test instead of its dependencies.

swiftui-patterns

8
from marvinrichter/clarc

SwiftUI architecture patterns, state management with @Observable, view composition, navigation, performance optimization, and modern iOS/macOS UI best practices.

swift-patterns

8
from marvinrichter/clarc

Core Swift patterns — value vs reference types, protocols, generics, optionals, Result, error handling, Codable, and module organization. Foundation for all Swift development.