Skip to main content
Premium Tier Required: The /v1/agent/batch/completions endpoint is restricted to Pro, Ultra, and Premium plan subscribers. Free tier users will receive a 403 error. Upgrade your account to access batch processing capabilities.
Single Agent vs Multi-Agent: This example covers batch processing for single agents (/v1/agent/batch/completions). For batching multi-agent swarms, see Batch Swarm Completions (Multi-Agent).

What This Example Shows

  • Processing multiple agent tasks in a single request
  • Configuring different agents for different types of analysis
  • Efficient batch execution for multiple related tasks
  • Handling diverse task types with specialized agents

Quick Start

import os
import json
import requests
from dotenv import load_dotenv

load_dotenv()

API_KEY = os.getenv("SWARMS_API_KEY")
BASE_URL = "https://api.swarms.world"

headers = {
    "x-api-key": API_KEY,
    "Content-Type": "application/json",
}

# Define multiple batch requests (array of AgentCompletion)
batch_requests = [
    {
        "agent_config": {
            "agent_name": "Bloodwork Diagnosis Expert",
            "description": "Expert in blood work interpretation.",
            "system_prompt": (
                "You are a doctor who interprets blood work. Give concise, clear explanations and possible diagnoses."
            ),
            "model_name": "gpt-4.1",
            "max_loops": 1,
            "max_tokens": 1000,
            "temperature": 0.5,
        },
        "task": (
            "Blood work: Hemoglobin 10.2 (low), WBC 13,000 (high), Platelets 180,000 (normal), "
            "ALT 65 (high), AST 70 (high). Interpret and suggest diagnoses."
        ),
    },
    {
        "agent_config": {
            "agent_name": "Radiology Report Summarizer",
            "description": "Expert in summarizing radiology reports.",
            "system_prompt": (
                "You are a radiologist. Summarize the findings of radiology reports in clear, patient-friendly language."
            ),
            "model_name": "gpt-4.1",
            "max_loops": 1,
            "max_tokens": 1000,
            "temperature": 0.5,
        },
        "task": (
            "Radiology report: Chest X-ray shows mild cardiomegaly, no infiltrates, no effusion. Summarize the findings."
        ),
    },
]


def run_agent_batch_completions():
    response = requests.post(
        f"{BASE_URL}/v1/agent/batch/completions",
        headers=headers,
        json=batch_requests,
        timeout=600,
    )
    response.raise_for_status()
    return response.json()


if __name__ == "__main__":
    result = run_agent_batch_completions()
    print(json.dumps(result, indent=4))

Batch Processing Benefits

  • Efficiency: Process multiple tasks in parallel
  • Cost Optimization: Reduce API call overhead
  • Consistency: Apply similar processing across multiple items
  • Scalability: Handle large volumes of work efficiently

Use Cases

Batch processing is ideal for:
  • Document Analysis: Review multiple contracts, reports, or documents
  • Data Processing: Analyze large datasets with multiple perspectives
  • Content Generation: Create variations of content for different audiences
  • Quality Assurance: Review multiple code files, designs, or content pieces
  • Customer Support: Process multiple support tickets with specialized agents

Expected Output

The batch processor will return results for each task:
  • Bloodwork analysis with diagnosis and recommendations
  • Radiology report summary in patient-friendly language
  • Each result maintains the structure and quality of individual agent runs

Environment Setup

Create a .env file in your project directory:
SWARMS_API_KEY=your_api_key_here

Advanced Batch Processing

You can extend this pattern to:
  • Dynamic Batching: Group similar tasks automatically
  • Priority Processing: Assign importance levels to different tasks
  • Result Aggregation: Combine multiple results into unified insights
  • Error Handling: Gracefully handle failures in individual batch items

Next Steps

After mastering batch processing, explore:
  • Multi-agent swarms for complex collaborative workflows
  • Sequential workflows for dependent tasks
  • Concurrent workflows for parallel execution
  • Agent routing for intelligent task distribution