Skip to main content

Overview

Use the Agent Completions endpoint (/v1/agent/completions) to run a single, well‑configured agent for a specific task.
This example shows a Research Analyst agent that uses the new agent_config shape and returns a structured JSON response.
This example is a companion to the main reference at /docs/documentation/capabilities/agent.
It focuses on a minimal, production‑ready Python script using plain requests.

Prerequisites

  • Python 3.9+
  • A valid Swarms API key
Create a .env file in your project root:
SWARMS_API_KEY=your_api_key_here
Install dependencies:
pip install python-dotenv requests

Run a Single Agent Completion

import os
import requests
from dotenv import load_dotenv
import json

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",
}


def run_single_agent():
    """Run a single agent with the Agent Completions format"""
    payload = {
        "agent_config": {
            "agent_name": "Research Analyst",
            "description": "An expert in analyzing and synthesizing research data",
            "system_prompt": (
                "You are a Research Analyst with expertise in data analysis and synthesis. "
                "Your role is to analyze provided information, identify key insights, "
                "and present findings in a clear, structured format. "
                "Focus on accuracy, clarity, and actionable recommendations."
            ),
            "model_name": "gpt-4.1",
            "role": "worker",
            # For simple, single-shot tasks you can keep max_loops at 1.
            # Use 'auto' for autonomous multi-step behavior (see autonomous tutorial).
            "max_loops": 1,
            "max_tokens": 8192,
            "temperature": 0.7,
            "auto_generate_prompt": False,
            "dynamic_temperature_enabled": True,
        },
        "task": "What are the best ways to find samples of diabetes from blood samples?",
    }

    response = requests.post(
        f"{BASE_URL}/v1/agent/completions",
        headers=headers,
        json=payload,
        timeout=60,
    )
    response.raise_for_status()
    return response.json()


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

What This Example Shows

  • New agent_config format matching the Agent Completions reference
  • A single, stateless request to /v1/agent/completions
  • How to inspect the full JSON response, including outputs and usage

Next Steps

  • Add history or search_enabled for more advanced behaviors
  • Use max_loops="auto" with the higher‑level Agent class for fully autonomous workflows
    → See “Autonomous Agents with max_loops="auto" in the examples section.