Swarms Logo

Swarms for Logistics

Shipment exception handling, rerouting, carrier communication, customs documentation and route planning review, run as agent teams on the Swarms API.

  • Planner agents decompose each exception into tasks that workers pull in parallel
  • Customs declarations, HS codes and carrier messages drafted in your document formats
  • Per-run token usage and latency in Swarms Cloud, so the cost of an exception is known
Network · shipments and exceptionsPaused
LAXORDJFKAMSDXBSINSHA

Exception feed

  • Missed connectionLAX→SIN

    MSKU-31268

    Reroute Worker: rebook LAX to SIN via next hub, +14h, 2 options

    draft · ops approval

Architecture

An exception desk as a planner and worker swarm

Planner agents read each exception and put tasks on a queue. Worker agents pull tasks as they free up and return drafts. The diagram shows one run; the queue monitor beneath it shows the mechanism that makes the pattern fit a quiet night and a storm equally well.

swarm_typePlannerWorkerSwarmSimulation paused
PLANNERSWORKERSTaskPOST /v1/swarmRoute Plannergpt-5.4Customs Plannerclaude-sonnet-5-5Reroute Workergpt-5.4-miniDocuments Workerclaude-haiku-5-5Notify Workergemini-2.5-flashDrafts200 OK · JSON
  • 01run #1041 started
PlannerWorkerSwarm · task queue1 tasks plannedPaused

Planners

  • Route Planner

    gpt-5.4

    reading exception

  • Customs Planner

    claude-sonnet-5-5

    queued a task

Queue

depth 0
  1. Queue empty, workers idle.

Workers

  • Reroute Workerbusy

    gpt-5.4-mini

    #1 draft customer delay notice

  • Documents Workeridle

    claude-haiku-5-5

    waiting on queue

  • Notify Workeridle

    gemini-2.5-flash

    waiting on queue

No tasks returned yet.

Planners write the task list

A planner never fixes anything itself. It reads the exception and writes tasks a worker can finish in one pass: evaluate this routing, draft that message, check this tariff line. Planners run on stronger models because they decide what work exists.

Workers pull, they are not assigned

Workers take the next task when they finish the current one, so three exceptions and thirty use the same request. Workers run on fast models because each task is short and well specified.

Everything comes back as a draft

Reroute options, carrier messages and document corrections return marked for approval. The ops desk sends what it agrees with through the systems it already uses.

Planner to workers

One exception, four tasks, three workers

What a planner's output looks like for a single missed connection, and the drafts the workers returned against it. The task text is what each worker actually received; the result is what the ops desk reads before approving.

Exception

Route Planner

gpt-5.4

AWB 176-44821093, 12 pallets of server components, SIN to AMS via DXB. Missed connection at DXB; next direct flight in 31 hours. Consignee has a production line start on Thursday.

  1. T1

    Evaluate alternative routings DXB to AMS that arrive before Thursday 06:00 local, including one via FRA or CDG with a road leg.

    Reroute Worker

    gpt-5.4-mini

    Two options: DXB to FRA tonight plus road to AMS, arrives Wednesday 19:00; or DXB to AMS direct in 31h, arrives Thursday 11:00 and misses the line start.

  2. T2

    Draft the consignee notice with the current status, both routing options and the time of the next update.

    Notify Worker

    gemini-2.5-flash

    Draft notice written, 110 words, references AWB and the Thursday 06:00 deadline, next update 18:00 SGT.

  3. T3

    Draft the carrier rebooking request for option 1 and list the documents that must travel with the split.

    Documents Worker

    claude-haiku-5-5

    Rebooking request drafted; house AWB and packing list need the new routing, no change to the commercial invoice.

  4. T4

    Prepare the ETA milestone update for the TMS, marked as proposed until ops confirms the routing.

    Reroute Worker

    gpt-5.4-mini

    Proposed ETA Wednesday 19:00 CET if option 1 is approved; current ETA left unchanged.

4 tasks planned · 4 drafts returnedops desk approves option 1 before the rebooking is sent

Use cases

Where logistics teams put agents to work

Each card names the Swarms API swarm type it maps to and the agents you would send in the request.

PlannerWorkerSwarm

Exception desk

Holds, delays and missed connections arrive from carrier feeds and tracking events. Planner agents split each exception into tasks, workers pull them from the queue and draft the reroute, the messages and the document fixes. The ops desk approves before anything is sent.

  • Route Planner
  • Customs Planner
  • Reroute Worker
  • Documents Worker
  • Notify Worker
SequentialWorkflow

Customs document pack

An invoice reader pulls parties, Incoterms, values and line items, an HS code agent classifies each line and flags uncertain ones, and a declaration writer fills the entry in the broker's format with the open questions listed on top.

  • Invoice Reader
  • HS Code Agent
  • Declaration Writer
ConcurrentWorkflow

Route plan review

Cost, transit time and risk agents each review a proposed routing for a lane on the same data, and their three views are returned together so the planner sees the trade-offs side by side rather than a single blended score.

  • Cost Agent
  • Transit Time Agent
  • Risk Agent
MixtureOfAgents

Carrier tender comparison

Rate, service and capacity agents read each carrier's response to a tender, and an aggregator writes the comparison table with the exceptions in each quote (fuel surcharge basis, accessorials, transit guarantees) called out.

  • Rate Agent
  • Service Agent
  • Capacity Agent
  • Tender Aggregator
MultiAgentRouter

Inbound message triage

Booking requests, tracking questions, damage claims and invoice disputes arrive in one inbox. A router reads each message and sends it to the specialist for that type, which drafts the reply and the system update for a coordinator to confirm.

  • Router
  • Booking Agent
  • Tracking Agent
  • Claims Agent
HierarchicalSwarm

Weekly network report

A director agent assigns lane performance, carrier scorecards and cost variance to worker agents, then integrates their sections into the weekly operations report with the lanes that missed their service target listed first.

  • Ops Director
  • Lane Agent
  • Carrier Agent
  • Report Writer

Code

Run the desk from Python, TypeScript or cURL

The Swarms API examples share one request shape: a name, a swarm_type, a task and a list of agents. Set SWARMS_API_KEY from your Swarms Cloud account before running. Every output is a draft; nothing is booked, filed or sent.

exception_desk.py
import os
from swarms_client import SwarmsClient

client = SwarmsClient(api_key=os.environ["SWARMS_API_KEY"])

# A PlannerWorkerSwarm: planners read the exceptions and fill a task queue,
# workers pull tasks from the queue and return their drafts. The ops desk
# approves every message and booking change before it leaves the system.
EXCEPTIONS = """
1. AWB 176-44821093, 12 pallets of server components, SIN -> AMS via DXB.
   Missed connection at DXB; next direct flight is in 31 hours.
   Consignee has a production line start on Thursday.
2. Container MSKU7781204, 40ft, Shanghai -> Rotterdam. Customs hold at
   Rotterdam: HS code on the invoice (8471.49) does not match the packing
   list description (storage units). Broker needs a corrected invoice.
3. Truck load TL-88213, Lyon -> Munich. Driver reports A8 closed by snow
   near Ulm; delivery window at the DC is 06:00 to 08:00 tomorrow.
"""

response = client.swarms.run(
    name="Exception Desk",
    description="Planners decompose shipment exceptions, workers draft the fixes.",
    swarm_type="PlannerWorkerSwarm",
    task=(
        "Work the three exceptions below. For each one produce the reroute or "
        "schedule option, the customer and partner messages, and any document "
        "corrections. Mark every item as DRAFT for ops approval.\n" + EXCEPTIONS
    ),
    agents=[
        {
            "agent_name": "Route Planner",
            "description": "Breaks transport exceptions into tasks for the workers.",
            "system_prompt": (
                "You are a senior freight planner. For each transport exception, list "
                "the tasks needed: alternative routings to evaluate, parties to notify, "
                "milestones to update. Be specific about airports, ports and cut-offs."
            ),
            "model_name": "gpt-5.4",  # the planner decides the work, so a stronger model
            "max_loops": 1,
            "role": "planner",
        },
        {
            "agent_name": "Customs Planner",
            "description": "Breaks customs and documentation exceptions into tasks.",
            "system_prompt": (
                "You are a customs compliance lead. For each documentation or customs "
                "exception, list the checks, corrections and broker communications "
                "needed, and the regulation or tariff heading each one depends on."
            ),
            "model_name": "claude-sonnet-5-5",
            "max_loops": 1,
            "role": "planner",
        },
        {
            "agent_name": "Reroute Worker",
            "system_prompt": "You evaluate alternative routings: give two options with transit time, added cost drivers and the new ETA for each. Prefer options that keep the consignee's deadline.",
            "model_name": "gpt-5.4-mini",  # many short tasks, fast model
            "max_loops": 1,
            "role": "worker",
        },
        {
            "agent_name": "Documents Worker",
            "system_prompt": "You check HS classifications and correct commercial documents. State the tariff heading you chose and why, and list the exact invoice lines to change.",
            "model_name": "claude-haiku-5-5",
            "max_loops": 1,
            "role": "worker",
        },
        {
            "agent_name": "Notify Worker",
            "system_prompt": "You draft messages to customers, brokers and carriers: short, factual, with the reference numbers and the next update time. Never promise a delivery time that is not in a worker's reroute option.",
            "model_name": "gemini-2.5-flash",
            "max_loops": 1,
            "role": "worker",
        },
    ],
    max_loops=1,
)

print(response)
exception_desk.sh
curl -X POST https://api.swarms.world/v1/swarm/completions \
  -H "x-api-key: $SWARMS_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "Exception Desk",
    "description": "Planners decompose shipment exceptions, workers draft the fixes.",
    "swarm_type": "PlannerWorkerSwarm",
    "task": "Work these exceptions and mark every item DRAFT for ops approval. 1) AWB 176-44821093, 12 pallets SIN to AMS via DXB, missed connection at DXB, next direct flight in 31 hours, consignee line start Thursday. 2) Container MSKU7781204 Shanghai to Rotterdam, customs hold: invoice HS 8471.49 does not match packing list description, broker needs a corrected invoice. 3) Truck TL-88213 Lyon to Munich, A8 closed by snow near Ulm, DC delivery window 06:00 to 08:00 tomorrow.",
    "agents": [
      {
        "agent_name": "Route Planner",
        "system_prompt": "You are a senior freight planner. For each transport exception list the tasks: routings to evaluate, parties to notify, milestones to update.",
        "model_name": "gpt-5.4",
        "max_loops": 1,
        "role": "planner"
      },
      {
        "agent_name": "Customs Planner",
        "system_prompt": "You are a customs compliance lead. For each documentation exception list the checks, corrections and broker communications needed.",
        "model_name": "claude-sonnet-5-5",
        "max_loops": 1,
        "role": "planner"
      },
      {
        "agent_name": "Reroute Worker",
        "system_prompt": "Evaluate alternative routings: two options each with transit time, cost drivers and new ETA.",
        "model_name": "gpt-5.4-mini",
        "max_loops": 1,
        "role": "worker"
      },
      {
        "agent_name": "Documents Worker",
        "system_prompt": "Check HS classifications and correct commercial documents. State the heading chosen and the invoice lines to change.",
        "model_name": "claude-haiku-5-5",
        "max_loops": 1,
        "role": "worker"
      },
      {
        "agent_name": "Notify Worker",
        "system_prompt": "Draft short factual messages to customers, brokers and carriers with reference numbers and the next update time.",
        "model_name": "gemini-2.5-flash",
        "max_loops": 1,
        "role": "worker"
      }
    ],
    "max_loops": 1
  }'
buildCustomsPack.ts
// Customs document pack as a SequentialWorkflow: read the invoice, classify
// each line, write the declaration in the broker's format. A broker reviews
// and files it; the swarm never submits anything to a customs authority.
const SWARMS_API = "https://api.swarms.world/v1/swarm/completions"

type InvoiceLine = { description: string; qty: number; unitValueUsd: number; origin: string }
type Shipment = { reference: string; shipper: string; consignee: string; incoterm: string; lines: InvoiceLine[] }

type SwarmResponse = {
  output: unknown
  usage: { input_tokens: number; output_tokens: number; total_tokens: number }
}

export async function buildCustomsPack(s: Shipment): Promise<SwarmResponse> {
  const lines = s.lines.map((l, i) => `${i + 1}. ${l.description}, qty ${l.qty}, $${l.unitValueUsd} each, origin ${l.origin}`).join("\n")

  const res = await fetch(SWARMS_API, {
    method: "POST",
    headers: {
      "x-api-key": process.env.SWARMS_API_KEY!,
      "Content-Type": "application/json",
    },
    body: JSON.stringify({
      name: `Customs Pack ${s.reference}`,
      swarm_type: "SequentialWorkflow",
      task:
        `Shipment ${s.reference}. Shipper ${s.shipper}, consignee ${s.consignee}, ${s.incoterm}. ` +
        `Invoice lines:\n${lines}\n\nProduce the entry data for the import declaration and list every line where the classification is uncertain.`,
      agents: [
        {
          agent_name: "Invoice Reader",
          system_prompt: "You extract parties, Incoterm, currency, total value and each line item from a commercial invoice into structured fields. Flag missing fields.",
          model_name: "claude-haiku-5-5", // extraction, fast and cheap
          max_loops: 1,
        },
        {
          agent_name: "HS Code Agent",
          system_prompt: "You classify each line to a 6-digit HS heading, state the General Rule of Interpretation you applied, and mark any line where two headings are plausible as UNCERTAIN with both candidates.",
          model_name: "gpt-5.4", // classification mistakes are expensive, stronger model
          max_loops: 1,
        },
        {
          agent_name: "Declaration Writer",
          system_prompt: "You fill the import entry fields (parties, Incoterm, value per line, HS per line, origin per line) as JSON and write a short cover note for the broker listing the UNCERTAIN lines first. Mark the output DRAFT.",
          model_name: "claude-sonnet-5-5",
          max_loops: 1,
        },
      ],
      max_loops: 1,
    }),
  })

  if (!res.ok) throw new Error(`Swarms API ${res.status}: ${await res.text()}`)
  return (await res.json()) as SwarmResponse
}

buildCustomsPack({
  reference: "MSKU7781204",
  shipper: "Hualian Storage Technology Co.",
  consignee: "Nordwind Data Centers BV",
  incoterm: "FOB Shanghai",
  lines: [
    { description: "Solid-state storage units, 7.68 TB, for servers", qty: 240, unitValueUsd: 410, origin: "CN" },
    { description: "Rack rail kits, steel", qty: 60, unitValueUsd: 38, origin: "CN" },
    { description: "Power cables C13 to C14, 2 m", qty: 480, unitValueUsd: 3.2, origin: "VN" },
  ],
}).then((r) => {
  console.log(r.output)
  console.log("tokens:", r.usage.total_tokens)
})
nightly_lane_review.py
import json
import os
import sys
import requests

BASE = "https://api.swarms.world"
HEADERS = {"x-api-key": os.environ["SWARMS_API_KEY"], "Content-Type": "application/json"}

# The nightly route plan review runs one ConcurrentWorkflow per active lane.
# Before submitting, the job checks the account's credit balance in Swarms
# Cloud and refuses to start if the run would leave the desk without credits
# for the daytime exception work.
MIN_BALANCE_AFTER_RUN = 25.0
EST_COST_PER_LANE = 0.06  # from last week's usage in Swarms Cloud logs

lanes = [
    {"lane": "SHA-RTM", "mode": "ocean", "plan": "40ft via Singapore, 34 days, carrier A"},
    {"lane": "SIN-AMS", "mode": "air", "plan": "direct, 2 flights per week, carrier B"},
    {"lane": "LYS-MUC", "mode": "road", "plan": "single driver, A8 via Stuttgart, 11 hours"},
    {"lane": "LAX-ORD", "mode": "rail", "plan": "intermodal, 5 days door to door"},
]

credits = requests.get(f"{BASE}/v1/account/credits", headers=HEADERS, timeout=30).json()
print("credit balance:", json.dumps(credits, indent=2))

estimated = EST_COST_PER_LANE * len(lanes)
balance = float(next((v for v in credits.values() if isinstance(v, (int, float))), 0.0))
if balance - estimated < MIN_BALANCE_AFTER_RUN:
    print(f"skip: {len(lanes)} lanes at ~${estimated:.2f} would leave ${balance - estimated:.2f}")
    sys.exit(0)

REVIEWERS = [
    {"agent_name": "Cost Agent", "system_prompt": "Review the lane plan for cost: rate basis, surcharges, accessorials, and where a different mode or carrier would be cheaper.", "model_name": "gpt-5.4-mini", "max_loops": 1},
    {"agent_name": "Transit Time Agent", "system_prompt": "Review the plan for transit time and schedule reliability, and name the connection most likely to slip.", "model_name": "claude-haiku-5-5", "max_loops": 1},
    {"agent_name": "Risk Agent", "system_prompt": "Review the plan for weather, congestion, customs and single-point-of-failure risk, with a mitigation for each.", "model_name": "gemini-2.5-flash", "max_loops": 1},
]

batch = [
    {
        "name": f"Lane Review {lane['lane']}",
        "swarm_type": "ConcurrentWorkflow",
        "task": f"Lane {lane['lane']} ({lane['mode']}). Current plan: {lane['plan']}. Review it.",
        "agents": REVIEWERS,
        "max_loops": 1,
    }
    for lane in lanes
]

res = requests.post(f"{BASE}/v1/swarm/batch/completions", headers=HEADERS, data=json.dumps(batch), timeout=600)
res.raise_for_status()
for lane, result in zip(lanes, res.json()):
    print("=" * 72)
    print(lane["lane"], "·", result["usage"]["total_tokens"], "tokens")
    print(result["output"])

summary = requests.get(f"{BASE}/v1/metrics/summary", headers=HEADERS, timeout=30).json()
print("account summary after run:", summary)

The Python SDK (pip install swarms-client) runs the exception desk as a PlannerWorkerSwarm. Two planners on stronger models turn three exceptions into a task queue; three workers on fast models pull the tasks and return reroute options, document fixes and message drafts marked for ops approval.

Swarms Cloud

The cost of an exception, per shipment

Runs from the API land in Swarms Cloud with their inputs, each agent's output, token usage and latency. Name each run after the shipment reference and the spend on agents can be allocated to a shipment, a lane or a customer.

Swarms Cloudexception-desk · PlannerWorkerSwarmSimulation paused

Runs / min

41

▲ last 30s

p50 latency

2.3s

▼ last 30s

Tokens / min

268.5k

▲ last 30s

Spend / hour

$9.33

▲ last 30s

Every run on the Swarms API appears in Swarms Cloud with its logs, token usage, cost and context use.

Open Swarms Cloud

Overnight runs with a credit check

The credits endpoint returns the account balance, so a nightly lane review can refuse to start if it would leave the desk short for daytime exception work. The Swarms Cloud example on this page does exactly that before submitting the batch.

Where the humans are

Coordinators approve reroutes and messages, brokers file declarations, planners sign off on lane changes. The agents draft and compare; the TMS, the carrier portals and the customs systems are still driven by the people who drive them today.

Questions logistics teams ask

What operations managers, customs leads and platform engineers ask before they put agents on an exception desk.

Why a PlannerWorkerSwarm for exceptions rather than a fixed pipeline?

Exceptions do not arrive in a fixed shape. A missed air connection needs reroute options and two messages; a customs hold needs a tariff check, a corrected invoice and a broker note. Planners decide the task list per exception, workers pull whatever is on the queue, and the same request handles one exception or thirty without changing the agent roster.

Swarm types on the Swarms API

Do the agents rebook freight or send messages to carriers?

Not by themselves. Every worker output in the examples is a draft marked for ops approval. Booking changes, customs filings and customer messages go out through the systems and people that send them today. If you want an agent to take an action, you give it a tool with its own limits, and that is a separate decision from the drafting.

How does the swarm get carrier events, tracking data and documents?

You put them in the task. The examples paste the exception text, invoice lines and lane plans into the request; a production integration reads them from your TMS, carrier EDI or tracking feeds and builds the task string. Agents can also call tools you expose, such as a schedule lookup, under limits you set.

Can we see what each exception cost to work?

Yes. Each response carries a usage block with input, output and total tokens, and every run is stored in Swarms Cloud with the same data plus latency. Naming runs after the shipment reference, as the examples do, lets you allocate agent spend to a shipment, a lane or a customer.

Model pricing

Which models should planners and workers use?

Planners decide what work gets done, so they run on stronger models such as gpt-5.4 or claude-sonnet-5-5. Workers handle many short, well-specified tasks, so fast models such as gpt-5.4-mini, claude-haiku-5-5 and gemini-2.5-flash keep the cost per exception low. The one exception in the examples is HS classification, where a mistake costs more than the tokens, so it runs on a stronger model.

Put a planner on your next exception

Create a Swarms Cloud account, copy the exception desk example above, and paste in the open exceptions from your board. The run shows up in your logs with every task and its cost.

New Swarms Cloud accounts start with $5 in API credits. Enterprise teams can talk to us about volume pricing, data handling and deployment options.