Agency Swarm has two major versions with different approaches. Choose the version that best fits your needs.
1

Set Your OpenAI Key

Begin by setting your OpenAI API key in the .env file. It will be loaded automatically on agency initialization
OPENAI_API_KEY=sk-...
2

Create Project Structure

Agent Folder Structure:When you run the create-agent-template command, it creates the following folder structure for your agent:
/your-agency-path/
└── AgentName/                    # Directory for the specific agent
    ├── files/                    # Directory for files that will be uploaded to openai
    ├── schemas/                  # Directory for OpenAPI schemas to be converted into tools
    ├── tools/                    # Directory for tools to be imported by default.
    ├── AgentName.py              # The main agent class file
    ├── __init__.py               # Initializes the agent folder as a Python package
    └── instructions.md or .txt   # Instruction document for the agent
This structure ensures that each agent has its dedicated space with all necessary files to start working on its specific tasks.Agency Folder Structure:The full structure of the project will look like this:
AgencyName/
├── AgentName/            # Agent folder created with the command above
├── AnotherAgent/         # Another agent folder
├── agency.py             # Main file where agents are imported and the agency is defined
├── agency_manifesto.md   # Shared instructions and guidelines for all agents
├── requirements.txt      # File listing all dependencies
└── ...
3

Create Tools

In 1.x versions of the framework, you have 2 ways of defining custom tools:By extending agency_swarm’s BaseTool class and implementing the run method.MyCustomTool.py:
from agency_swarm.tools import BaseTool
from pydantic import Field

class MyCustomTool(BaseTool):
    """
    A brief description of what the custom tool does.
    The docstring should clearly explain the tool's purpose and functionality.
    It will be used by the agent to determine when to use this tool.
    """

    # Define the fields with descriptions using Pydantic Field
    example_field: str = Field(
        ..., description="Description of the example field, explaining its purpose and usage for the Agent."
    )

    # Additional Pydantic fields as required
    # ...

    def run(self):
        """
        The implementation of the run method, where the tool's main functionality is executed.
        This method should utilize the fields defined above to perform the task.
        Doc string is not required for this method and will not be used by your agent.
        """

        # Your custom tool logic goes here
        do_something(self.example_field)

        # Return the result of the tool's operation as a string
        return "Result of MyCustomTool operation"
Or by using openai’s @function_tool decorator.my_custom_tool.py:
from agency_swarm import function_tool

@function_tool
def my_custom_tool(example_field: str) -> str:
    """
    A brief description of what the custom tool does.
    The docstring should clearly explain the tool's purpose and functionality.
    It will be used by the agent to determine when to use this tool.

    Args:
        example_field: Description of the example field, explaining its purpose and usage for the Agent.

    Returns:
        Result of the tool's operation as a string.
    """

    # Your custom tool logic goes here
    do_something(example_field)

    # Return the result of the tool's operation as a string
    return "Result of MyCustomTool operation"
4

Define Agent Roles

Adjust the parameters and instructions for each agent.Developer.py:
from agency_swarm import Agent, ModelSettings
from .tools.my_custom_tool import my_custom_tool

class Developer(Agent):
    def __init__(self):
        super().__init__(
            name="Developer",
            description="Responsible for executing tasks.",
            instructions="./instructions.md",
            tools=[my_custom_tool, MyCustomTool],  # Import tools directly
            model_settings=ModelSettings(
                temperature=0.3,
                max_completion_tokens=25000,
                model="gpt-4o"
            )
        )
Tools are imported directly into the agent instead of from a folder.instructions.md:
You are a Developer agent responsible for executing tasks.

# Role
You are responsible for writing clean, efficient, and reusable code.

# Process
1. How to handle incoming requests
2. When and how to use available tools
3. How to collaborate with other agents
5

Create Agency

Import your agents and initialize the Agency class.agency.py:
from agency_swarm import Agency
from .Developer import Developer
from .CEO import CEO

developer = Developer()
ceo = CEO()

agency = Agency(
    ceo,  # Entry point agent as positional argument
    communication_flows=[
        (ceo, developer),  # CEO can initiate communication with Developer
    ],
    shared_instructions='./agency_manifesto.md'  # shared instructions for all agents
)
The first positional argument is the entry point agent that users interact with directly. Communication flows are defined separately.
In Agency Swarm, communication flows are directional tuples. For instance, in the example above: (ceo, developer), the CEO can initiate a chat with the Developer, and the Developer can respond in this chat. However, the Developer cannot initiate a chat with the CEO.
6

Run Demo

There are multiple ways to run the demo. Add one of the following to your agency.py file:Web Interface:
agency.copilot_demo()
Copilot demo is currently minimal and will be updated in future patches
Terminal Version:
agency.terminal_demo()
Backend Version (Sync):
result = agency.get_response_sync("Please create a new website for our client.")
Backend Version (Async):
import asyncio

async def main():
    result = await agency.get_response("Please create a new website for our client.")
    print(result.final_output)

asyncio.run(main())

Next Steps