Agency Swarm Documentation
repository·main·Indexed 26 days ago
https://github.com/vrsen/agency-swarmA framework for building multi-agent applications that extends the OpenAI Agents SDK. It enables the creation and orchestration of collaborative AI agent swarms using organizational structures. Key features include custom tool definition via @function_tool or BaseTool, communication flow configuration using the Agency class, and multiple interaction interfaces including Web UI, TUI, and programmatic async/sync methods. It also provides integrations for FastAPI, OpenClaw, and Twilio Realtime API, as well as a centralized Agency Context for state management.
What's inside Agency Swarm
- Agencii Platform is a deployment and management layer built on top of the Agency Swarm framework. It is designed to simplify the deployment, management, and integration of AI agents, allowing developers to focus on agent logic rather than infrastructure.
Overview of Agency Swarm
mainAgency Swarm is an open-source agent orchestration framework built on the OpenAI Agents SDK / Responses API. It is designed to simplify the creation of collaborative swarms of agents (Agencies) where each agent has distinct roles and capabilities. The framework models automation using real-world entities like agencies and specialized roles to make agent interaction more intuitive.Key Features of Agency Swarm
mainAgency Swarm provides several core capabilities for building agentic workflows:
- Customizable Agent Roles: Define specific roles (e.g., CEO, virtual assistant, developer) and their functionalities.
- Full Control Over Prompts: Complete customization of prompts to avoid the restrictions of pre-defined templates.
- Error Correction: Uses Pydantic-based type validation to prevent hallucinations and ensure data integrity.
- Efficient Communication: Agents communicate using only their own provided descriptions.
- Custom Tools: Ability to build custom Python-based tools to connect agents to external APIs and new capabilities.
- Production Ready: Designed for reliability and deployment in production environments.
Compare Agency Swarm Pricing Plans
mainAgency Swarm offers two main subscription tiers based on agency count and credit capacity:
- Free Tier: $0/month. Includes 25 daily credits, no monthly credits, and supports up to 5 agencies.
- Pro: $20/month. Includes 25 daily credits, 2,000 monthly credits, and supports unlimited agencies.
Compare Agency Swarm with other multi-agent frameworks
mainAgency Swarm is designed for real-world business use cases, prioritizing control, reliability, and lightweight architecture. Key differentiators include:
- No Predefined Prompts: Unlike CrewAI or AutoGen, Agency Swarm does not write prompts for you, providing full control over agent behavior.
- Automatic Error Correction: Uses automatic type checking and validation to prevent hallucinations.
- Uniform Communication Flows: Allows for custom communication flow definitions without rigid constraints.
- Reliability: Employs robust type checking and validation for all tools using Pydantic, integrated with a guardrails system.
- Scalability: Adding agents is as simple as passing them to the
Agencyclass. - Model Support: Supports over a dozen models via the LiteLLM SDK.
Comparison Table
Criteria Agency Swarm AutoGen CrewAI Origins Real AI agency production Research experiment Funding vehicle Architecture Lightweight, minimal abstractions, built on OpenAI Assistants/Responses API Event-driven, supports ChatCompletions/Assistants Built on LangChain with high abstraction Reliability Robust Pydantic type checking & guardrails Type hints but no validation Limited/inconvenient validation via BaseTool Flexibility No predefined prompts; uniform communication Overridable predefined prompts; GroupChat orchestration Numerous predefined prompts; limited customization Scalability High (add agents to Agencyclass)Moderate (complex graphs need custom orchestration) Moderate (Flows enable conditional orchestration) Deployability Easy via callback functions and open-source templates Requires self-hosted SDK deployment Enterprise platform deployment Model Support High (via LiteLLM) Moderate Full open-source support Understand Agency Swarm credit consumption
mainCredits power all agent activities, including replies and multi-step workflows. Usage is based on task complexity and sandbox runtime.
Key consumption factors:
- AI Tokens: LLM processing, image generation, etc.
- Compute: Running agents in sandboxes and execution environments.
- Agent Builds: Creating, configuring, and deploying agents.
- File Storage: Managing files created during tasks.
- Web Searches: Internet searches for task completion.
- Third-Party Services: Accessing external APIs.
Important Notes:
- AI token usage stops when a task completes, but compute usage continues until the sandbox reaches its timeout.
- Storage and downloads may consume small amounts of credits while agents are idle.
- Using your own API keys prevents credits from being deducted for AI tokens or third-party services.
Understand Guardrail types in Agency Swarm
mainGuardrails are checkpoint functions used to ensure agent behavior remains safe and predictable. They function at two distinct stages of the agent lifecycle:
- Input Guardrails: Executed before the agent processes input.
- Non-strict mode: Returns guidance to the agent.
- Strict mode: Raises an
InputGuardrailTripwireTriggeredexception.
- Output Guardrails: Executed after the agent drafts an output but before delivery.
- If validation fails, the agent will retry up to the configured
validation_attempts. - If retries are exhausted, it raises an
OutputGuardrailTripwireTriggeredexception.
- If validation fails, the agent will retry up to the configured
- Input Guardrails: Executed before the agent processes input.
Import third-party agents into Agency Swarm
mainAgency Swarm allows you to import external agent runtimes that operate outside the standard Agency Swarm Python stack. This pattern is used when you want Agency Swarm to maintain control over orchestration while delegating specific tasks to a different agent runtime.
One supported implementation is the
OpenClawAgent, which can be used as a delegated worker within an Agency Swarm agency.Understand the Agency class and its benefits
mainAn
Agencyin Agency Swarm is a collection ofAgentinstances that collaborate to complete tasks. Using an agency instead of a single agent provides:- Fewer Hallucinations: Agents can supervise each other to reduce mistakes.
- Complex Task Completion: Multiple agents allow for longer sequences of actions.
- Scalability: You can scale solutions by adding more agents as complexity grows.
Best Practice: Start with a minimal number of agents and fine-tune them before adding more to avoid debugging difficulties caused by complex interactions.
Configure required environment variables for production
mainBefore deploying to a production environment, ensure the following environment variables are configured:
Variable Required Description OPENAI_API_KEYYes Your OpenAI API key APP_TOKENRecommended Authentication token for FastAPI endpoints Note: Thread persistence is managed via callbacks that allow you to store conversation history in your chosen database.
Combine multiple multimodal outputs in a single tool
mainTo return multiple different types of content (e.g., images, text, and files) from a single tool execution, return a
listfrom therunmethod. This allows the agent to receive a rich set of feedback in one turn.from agency_swarm import BaseTool, ToolOutputFileContent, ToolOutputImage, ToolOutputText class PrepareShowcase(BaseTool): """Return rich media and a short description.""" teaser_a: str = "https://example.com/teaser-a.png" teaser_b: str = "https://example.com/teaser-b.png" report_id: str = "file-report-123" def run(self) -> list: return [ ToolOutputImage(image_url=self.teaser_a), ToolOutputImage(image_url=self.teaser_b), ToolOutputText(text="Gallery updated: Teaser A and Teaser B now live."), ToolOutputFileContent(file_id=self.report_id), ]Transfer data between tools and agents
mainData can be transferred between tools and agents using two primary methods:
- Agency Context: Access the agency context directly inside your tools.
- File Uploads: Create or modify a tool to upload files to storage and return a file ID. This ID can then be consumed by other tools or agents.