Swarm

Swarm pipelines are sequential multi-agent workflows where each agent runs its own full agentic loop (LLM + tools) and passes its output forward to the next agent in the chain. Swarms replace cron-based Telegram-style polling coordination with direct programmatic orchestration -- agents execute in dependency order within a single, budget-controlled pipeline.


Endpoint

POST /internal/agent/swarm

This is an internal OIDC-authenticated endpoint on the agent executor service. It is not directly accessible to end users. The main API enqueues swarm requests via Cloud Tasks, and the agent executor processes them asynchronously.


Concept

A swarm is an ordered pipeline of autonomous agents. Each agent operates independently with its own model, system prompt, tools, and integrations, but receives the accumulated context from all preceding agents. Unlike ensemble consensus (which refines a single output through multiple models), swarm pipelines chain distinct stages of a workflow where each stage has a fundamentally different job.

+-------------------+     +-------------------+     +-------------------+
|    Agent 1        |     |    Agent 2        |     |    Agent 3        |
|  "Researcher"     |     |  "Writer"         |     |  "Editor"         |
|                   |     |                   |     |                   |
|  system_prompt    |     |  system_prompt    |     |  system_prompt    |
|  task_prompt      |     |  + CONTEXT from   |     |  + CONTEXT from   |
|  model + tools    |     |    Agent 1        |     |    Agent 2        |
|                   |     |  model + tools    |     |  model + tools    |
|  [agentic loop]   |---->|  [agentic loop]   |---->|  [agentic loop]   |
|                   |     |                   |     |                   |
+-------------------+     +-------------------+     +-------------------+
        |                         |                         |
        v                         v                         v
   Output A               Output A + B              Output A + B + C
   (research)             (draft post)              (final polished post)

Why Swarms Replace Cron-Based Coordination

Before swarms, multi-step workflows required cron-based scheduling with message-passing through Telegram or webhook chains. Each agent ran independently on its own schedule, with no guaranteed ordering, no shared budget, and no automatic context passing. Swarms solve this by:

  • Guaranteed ordering via depends_on dependency resolution (topological sort)

  • Automatic context injection from predecessor outputs into successor system prompts

  • Unified credit budget enforced across the entire pipeline (max_total_credits)

  • Atomic failure handling -- the pipeline stops cleanly when an agent fails, preserving partial results


Request Body

Parameter Reference

Parameter
Type
Required
Constraints
Description

user_id

string

Yes

--

Authenticated user ID for credit billing.

task_id

string

No

--

Parent task ID for Firestore tracking and credential loading.

swarm_id

string

Yes

--

Identifier for this swarm pipeline. Used in Firestore records and logs.

plan

string

Yes

guru or pro

User plan. Determines rate limits and iteration caps per agent.

agents

array

Yes

1--10 agents

Ordered list of agents in the pipeline.

max_total_credits

float

No

>= 1

Credit budget for the entire swarm. Pipeline halts if exceeded. Default: 2000.

context

string

No

--

Additional context injected into the first agent's system prompt.

Agent Object

Field
Type
Required
Constraints
Description

name

string

Yes

Unique within swarm

Human-readable agent name. Used in context markers and Firestore records.

system_prompt

string

Yes

--

System-level instructions for this agent's role and behavior.

task_prompt

string

Yes

--

The specific task this agent must accomplish. Sent as the user message.

model

string

No

Valid model ID

LLM model for this agent. Default: claude-sonnet.

temperature

float

No

0.0--2.0

Model temperature. Default: 0.7.

max_tokens

integer

No

256--65536

Maximum tokens per LLM call. Default: 4096.

max_iterations

integer

No

1--25

Maximum agentic loop iterations. Default: 10.

tools

array

No

--

Tools available to this agent (e.g., http_get, api_call, http_post, webhook). Default: ["http_get", "api_call"].

integrations

array

No

--

Platform integration IDs for URL validation (e.g., newsapi, slack).

webhook_urls

array

No

HTTPS only, max 3

Webhook URLs this agent can call via the webhook tool.

depends_on

string

No

Valid agent name

Name of the agent whose output this agent requires. If omitted, agents execute in array order.


Dependency Resolution

The executor resolves agent execution order using topological sort on the depends_on graph. Agents without a depends_on field execute in array order, with each implicitly depending on the previous agent.

Algorithm

The _resolve_order() method walks the dependency tree recursively:

  1. Build a name-to-agent map from the agents list.

  2. For each agent, recursively visit its depends_on dependency first.

  3. Track the current visit path to detect circular dependencies.

  4. Append each agent to the ordered list once all its dependencies are resolved.

Cycle Detection

If the depends_on graph contains a cycle (e.g., A depends on B, B depends on A), the executor raises a ValueError with the message "Circular dependency detected: {name}" and the swarm fails immediately before any agent executes.


Context Injection Between Agents

The core mechanism that connects agents in a swarm is context injection. When an agent has a depends_on dependency, the predecessor's complete output is injected into the successor's system prompt:

Injection Rules

Condition
Behavior

First agent in pipeline

Receives global context field (if provided) as ADDITIONAL CONTEXT

Agent with depends_on

Receives predecessor's output between --- CONTEXT FROM PREVIOUS AGENT --- markers

Predecessor failed

No context injected (empty string). The dependent agent runs without predecessor context.

Predecessor completed

Full output text injected

No depends_on, not first

No context injection (runs independently)

Context injection happens at the system-prompt level so the agent treats predecessor output as authoritative background information, not as a user instruction. This prevents prompt injection from one agent's output affecting the next agent's core behavior.


Pipeline Mechanics

Agentic Loop Per Agent

Each agent runs a full agentic loop, identical to the standard single-agent execution:

  1. LLM receives system prompt (with injected predecessor context) and task prompt as user message.

  2. LLM may return tool_use content blocks.

  3. Tools are executed server-side with DNS pinning, URL validation, and credential injection.

  4. Tool results are fed back to the LLM as tool_result messages.

  5. Loop repeats until the LLM produces a final text answer (no tool calls) or max_iterations is reached.

If max_iterations is exhausted without a final answer, the agent's last assistant text output is extracted and the agent completes with status max_iterations. The pipeline continues to the next agent.

Credit Budget Enforcement

Credits are enforced at the swarm level via max_total_credits, not per agent:

  • Before each agent starts, the executor checks the remaining budget.

  • Each LLM call within an agent's loop is charged through the main API's standard credit reservation system.

  • Credits accumulate across all agents.

  • If the budget runs out, the pipeline halts. Completed agents' results are preserved.

  • Credits consumed before exhaustion are not refunded.

Failure Handling

The pipeline stops immediately when any agent fails. This is a deliberate design choice -- downstream agents depend on predecessor context, so continuing after a failure would produce unreliable output.

Failure Mode
Behavior

LLM call error

Agent marked failed with error detail. Pipeline stops.

Tool execution error

Error returned to LLM as tool result. Agentic loop continues (LLM decides next step).

Budget exhausted

Pipeline halts before next agent. Completed agents preserved.

Max iterations

Agent completes with status max_iterations. Pipeline continues to next agent.

Unhandled exception

Agent marked failed. Pipeline stops. Error logged.

Final Status

The SwarmResult status is determined by agent completion counts:

Condition
Status

All agents completed successfully

completed

Some agents completed, pipeline did not finish

partial

No agents completed successfully

failed

The content field in the response contains the last completed agent's output -- this is the final product of the pipeline.


Firestore Storage

swarm_executions/{execution_id}

Each swarm run creates a top-level execution record:

swarm_executions/{execution_id}/agent_outputs/{agent_name}

Each agent's result is stored in a subcollection, providing a complete audit trail:


SSE Events

When triggered via a streaming-capable endpoint, the swarm executor emits Server-Sent Events to report pipeline progress in real time. This enables frontends to show per-agent progress indicators.

agent_start

Emitted when an agent begins execution.

agent_done

Emitted when an agent completes (successfully or with failure).

On agent failure:

swarm_done

Emitted when the entire pipeline finishes.

On pipeline failure:


Plan Limits

Plan
Max Agents per Swarm
Max Iterations per Agent
Max Credits per Execution
Notes

Free

Not available

--

--

Swarms are not available on the Free plan.

Guru

5

10

100

Agent iterations and credits use Guru limits.

Pro

10

25

500

Full access to all swarm configurations.


Active Swarm Configurations

The platform provides five pre-configured swarm templates for common business workflows:

1. Content Pipeline

A three-agent pipeline for producing research-backed blog content.

Order
Agent
Model
Tools
Task

1

Trend Researcher

gpt-5.2 (T=0.3)

http_get, api_call

Identify trending topics and gather source material.

2

Blog Writer

claude-sonnet (T=0.7)

--

Write a structured blog post based on research findings.

3

Editor

gemini-3.1-pro (T=0.3)

--

Edit for clarity, tone, SEO, and factual accuracy.

2. Customer Success

A three-agent pipeline for onboarding analysis and proactive customer engagement.

Order
Agent
Model
Tools
Task

1

Onboarding Analyst

gpt-5.2 (T=0.3)

http_get

Analyze customer onboarding data and identify friction points.

2

Success Strategist

claude-opus (T=0.5)

--

Develop a retention strategy with milestones and triggers.

3

Comms Drafter

claude-sonnet (T=0.6)

--

Draft personalized outreach emails for each milestone.

3. Partnership BD

A three-agent pipeline for business development outreach.

Order
Agent
Model
Tools
Task

1

Opportunity Scout

gpt-5.2 (T=0.4)

http_get, api_call

Research potential partners and evaluate strategic fit.

2

Pitch Drafter

claude-sonnet (T=0.6)

--

Draft partnership proposals tailored to each prospect.

3

Follow-up Manager

gemini-3.1-pro (T=0.4)

--

Create follow-up sequences and objection handling scripts.

4. GTM Marketing

A three-agent pipeline for go-to-market content and outreach.

Order
Agent
Model
Tools
Task

1

SEO Strategist

gpt-5.2 (T=0.3)

http_get

Perform keyword research and competitive SEO analysis.

2

Content Creator

claude-sonnet (T=0.7)

--

Produce SEO-optimized content based on keyword strategy.

3

Outbound SDR

gemini-3.1-pro (T=0.5)

--

Generate personalized outreach sequences for target accounts.

5. DevOps Pipeline

A three-agent pipeline for incident response automation.

Order
Agent
Model
Tools
Task

1

Incident Analyzer

gpt-5.2 (T=0.2)

http_get, api_call

Analyze incident logs and identify root cause.

2

Runbook Generator

claude-sonnet (T=0.4)

--

Generate remediation runbook with executable steps.

3

Status Updater

gemini-3.1-pro (T=0.3)

--

Draft stakeholder communications and status page updates.


Use Cases

Content Pipeline (Research, Write, Edit)

The most common swarm pattern. A researcher agent gathers data using web tools, a writer produces structured content from the research, and an editor polishes for publication. Each agent's expertise is distinct -- the researcher needs tool access, the writer needs creative temperature, and the editor needs low-temperature precision.

Operational Automation

Swarms excel at multi-step operational workflows: incident analysis followed by runbook generation followed by stakeholder communication. Each step requires different domain expertise and produces a different output format.

Business Development

Scout potential opportunities, draft tailored pitches, and create follow-up sequences -- all in a single pipeline with budget controls. The dependency chain ensures each agent builds on the previous agent's work.


Example Request

Example Response


Error Handling

Error
HTTP Status
Description

INSUFFICIENT_CREDITS

402

User does not have enough credits for the estimated swarm cost.

BUDGET_EXHAUSTED

402

max_total_credits reached mid-pipeline. Partial results returned.

TOO_MANY_AGENTS

400

More than 10 agents specified.

TOO_FEW_AGENTS

400

Fewer than 1 agent specified.

CIRCULAR_DEPENDENCY

400

depends_on graph contains a cycle.

INVALID_DEPENDENCY

400

depends_on references a non-existent agent name.

AGENT_FAILURE

502

An agent's model or tool call failed. Partial results returned.

INVALID_MODEL

400

An agent specifies a model not in the model registry.

On pipeline failure, the response includes all completed agent results and the failure detail for the failing agent. Credits consumed before the failure are not refunded.


Last updated