Welcome
- Overview
- Agency Swarm vs Other Frameworks
- Get Started
Core Framework
- Tools
- Agents
- Agencies
- State Management
Additional Features
Contributing
FAQ
FAQ
Find answers to common questions about Agency Swarm.
Set your API key in your code:
from agency_swarm import set_openai_key
set_openai_key("YOUR_API_KEY")
Or use a .env
file:
OPENAI_API_KEY=sk-1234...
Then load it with:
from dotenv import load_dotenv
load_dotenv()
There are two ways to create agents with AI:
-
Genesis Agency: A simple command-line tool that helps you create basic agent structures. Great for getting started or simple use cases. Just run
agency-swarm genesis
and follow the prompts. -
Cursor AI Code Editor: Use the
.cursorrules
file in Cursor IDE to create agents. This is the best option for both beginners and experienced developers since it gives you more control over the agent creation process.
Yes—you can use open source models for simple, non–mission-critical tasks (usually one or two tools per agent). See Open Source Models for more information. Keep in mind that many open source models currently struggle with function calling.
To persist threads between application restarts, implement thread callbacks that save and load thread IDs from a local file. For example, define your callback functions:
import os
import json
def load_threads(chat_id):
if os.path.exists(f"{chat_id}_threads.json"):
with open(f"{chat_id}_threads.json", "r") as file:
return json.load(file)
return []
def save_threads(new_threads, chat_id):
with open(f"{chat_id}_threads.json", "w") as file:
json.dump(new_threads, file)
Then, pass these callbacks during your agency initialization to resume conversations:
from agency_swarm.agency.genesis import GenesisAgency
agency = GenesisAgency(
...
threads_callbacks={
'load': lambda: load_threads(chat_id),
'save': lambda new_threads: save_threads(new_threads, chat_id)
}
)
This setup preserves your conversation context between runs.
To support multiple users/chats, you need to load and save thread IDs in your database accordingly. Each chat/user should have unique thread IDs. Ensure to check out our Deployment to Production guide for more information.
There are two ways to transfer data between tools and agents:
- Use shared state inside your tools. Read more: Shared State
- Create a tool (or modify an existing one) that uploads files to storage and outputs the file ID. This file ID can then be used by other tools or agents.
When file types like .json
, .docx
, or .pptx
are uploaded, CodeInterpreter is auto-added to process them. To change the agent’s behavior, update its instructions or create a custom file-handling tool.
Embed your agency within a FastAPI endpoint. For example:
@app.post("/chat")
async def chat(user_request: UserRequest):
chat_id = user_request.chat_id or str(uuid4())
agency = Agency([...],
threads_callbacks={
'load': lambda: load_threads(chat_id),
'save': lambda new_threads: save_threads(new_threads, chat_id)
})
response = agency.get_completion(user_request.message)
return {"chat_id": chat_id, "response": response}
Build a dedicated API backend (FastAPI is recommended) that manages authentication and persists thread state using callbacks. For more details, refer to our Deployment to Production guide.