Skip to main content

What This Example Shows

  • A HierarchicalSwarm with a Lead Reviewer (director) coordinating four specialist workers: Security, Style/Lint, Test Coverage, and Architecture
  • Tightly scoped system prompts that force each reviewer into a single lens with an explicit output format
  • A Lead Reviewer prompt that compresses every specialist’s findings into one structured Markdown PR comment with a hard verdict
  • How to wire the swarm into a real GitHub PR — fetch the diff via the GitHub REST API and post the review back as a comment
  • A realistic cost comparison against a senior engineer reviewing the same PR
This tutorial uses HierarchicalSwarm on /v1/swarm/completions — included in every paid Swarms tier. For teams reviewing hundreds of PRs a day across multiple repos, upgrade at https://swarms.world/platform/account for higher rate limits and parallel execution.

Why This Matters

A single human reviewer cannot hold security, style, test coverage, and architecture in their head on the same pass — they pick one lens, miss the others, and the PR sits for two days while the author context-switches onto something else. That latency is what actually kills shipping speed: not the reviewing, the waiting. The job here is not to replace your senior engineer’s signoff — it is to make sure that by the time they open the PR, every obvious finding is already in the thread, every missing test is already called out, and the only thing left for the human is the judgement call. One reviewer cannot catch everything. Four specialists running in parallel can.

Step 1: Setup

Install the dependencies and grab your API key from https://swarms.world/platform/api-keys. You will also need a GitHub personal access token with repo scope to post the review back.

Step 2: Define the Reviewer Team

The Lead Reviewer owns the final comment. Each specialist owns exactly one lens and is told to ignore everything outside it. The output format is rigid on purpose — the Lead has to merge four streams into one PR comment in a single pass.
HierarchicalSwarm always builds its own director from director_model_name (default gpt-5.4) that decomposes the task and dispatches it to every agent in agents — the Lead Reviewer above runs as one of the director’s workers, not as a privileged synthesizer, and with the default max_loops: 1 and parallel worker execution, no agent (Lead Reviewer included) sees another specialist’s findings before writing its own. extract_lead_comment below reads whatever the Lead Reviewer produced on its own pass, which may not actually be the compressed four-way summary the prompt asks for. For a guaranteed synthesis pass, either set "max_loops": 2 on the swarm (the framework’s director is handed every first-round finding on its second pass and can route the synthesis to the Lead Reviewer by name/description), or collect the four specialists’ findings client-side and issue one more /v1/agent/completions call using the Lead Reviewer’s system prompt before you post to GitHub.
The Lead Reviewer’s output is meant to be the comment you post to GitHub, and the four specialist outputs are the audit trail — every flagged issue is fully traceable back to the reviewer who raised it — but see the warning above for what it takes to make that reliable.

Step 3: Review a Single Diff

Start with a single diff pasted in directly. This is the loop you will wire to GitHub in the next step.
The Lead Reviewer should come back with REQUEST CHANGES, a CRITICAL finding for the SQL injection in both queries, and a Test Gap entry for reset_password. That is the comment you post.

Step 4: Wire It Into a GitHub PR

In production you do not paste diffs — you point the swarm at a PR number. GitHub exposes both the unified diff and the issue-comments endpoint on every PR.
Running this from a GitHub Actions workflow on the pull_request event gives every PR a structured review comment within ~30 seconds of being opened. The author fixes the obvious findings before a human ever looks at the PR — which is the entire point.

Real Cost vs. a Senior Reviewer

The swarm is not a replacement for your senior engineer’s signoff — it is the reviewer who is always available at 2 AM, never misses the SQL injection, and never lets a PR sit for two days without a first pass.

Next Steps

  • See Tools in Swarms to give the reviewers a real linter, AST parser, or test runner as a tool call
  • Read MCP Integration to connect the swarm to your internal code-search or SAST server over MCP
  • Browse the Hierarchical Workflow Example for the director-and-workers pattern with a deeper dive on routing