Update: I had originally posted this before, but decided to make a few changes to the template: namely, adding functions and putting everything into a class. This is the new version.
CrewAI enables developers to orchestrate teams of AI agents that work together to solve complex problems. Instead of managing multiple files and complex project structures, I wanted to create a single-file template that contains everything you need to get started quickly.
Why a Single-File Template?
Simplicity: Everything is in one place; no need to navigate between multiple files or remember complex import paths.
Quick Setup: Copy one file, add your API keys, and you’re ready to go.
Easy Sharing: Share your entire CrewAI project by sending a single Python file.
Perfect for Prototyping: Ideal for testing ideas, demos, or small projects without overhead.
Complete Template Structure
Our ultra-simple CrewAI template consists of just two files:
crewai-project/
├── template.py
└── .env
The Complete Template
template.py
import os
from typing import Any, Dict, List
from crewai import Agent, Crew, Process, Task
from crewai_tools import (
CSVSearchTool,
DirectoryReadTool,
FileReadTool,
RagTool,
SerperDevTool,
)
from dotenv import load_dotenv
class CrewManager:
"""A manager class for organizing and controlling CrewAI workflows."""
def __init__(self):
"""Initialize the CrewManager with environment setup and empty containers."""
self.load_environment()
self.tools = self.initialize_tools()
self.agents = []
self.tasks = []
self.crew = None
def load_environment(self):
"""Load environment variables from .env file and set OpenAI configuration."""
load_dotenv()
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
os.environ["OPENAI_MODEL_NAME"] = os.getenv("OPENAI_MODEL_NAME")
def initialize_tools(self) -> Dict[str, Any]:
"""Initialize and return a dictionary of available CrewAI tools."""
return {
"file_tool": FileReadTool(),
"search_tool": SerperDevTool(),
"csv_search_tool": CSVSearchTool(),
"rag_tool": RagTool(),
}
def create_agent(
self,
role: str,
goal: str,
backstory: str,
tools: List[Any] = None,
verbose: bool = False,
) -> Agent:
"""Create a new agent with specified parameters and add it to the agents list."""
if tools is None:
tools = []
agent = Agent(
role=role, goal=goal, backstory=backstory, verbose=verbose, tools=tools
)
self.agents.append(agent)
return agent
def create_task(self, name: str, description: str, tools: List[Any] = None) -> Task:
"""Create a new task with specified parameters and add it to the tasks list."""
if tools is None:
tools = []
task = Task(name=name, description=description, tools=tools)
self.tasks.append(task)
return task
def create_crew(
self,
process: Process = Process.hierarchical,
manager_llm: str = "gpt-4o",
verbose: bool = True,
planning: bool = True,
) -> Crew:
"""Create a crew from existing agents and tasks with specified configuration."""
self.crew = Crew(
agents=self.agents,
tasks=self.tasks,
verbose=verbose,
planning=planning,
process=process,
manager_llm=manager_llm,
)
return self.crew
def run_crew(self):
"""Execute the crew workflow and return the results."""
if self.crew is None:
raise ValueError("Crew not created. Call create_crew() first.")
return self.crew.kickoff()
def get_user_input(self) -> str:
"""Display welcome message and prompt user for input."""
print("Welcome!")
return input("What is your question? ")
def main():
"""Main function for CrewManager usage with agents and tasks."""
crew_manager = CrewManager()
# Create agents
crew_manager.create_agent(
role="Agent1",
goal="Describe Goal",
backstory="Write the backstory for Agent1",
tools=[crew_manager.tools["search_tool"], crew_manager.tools["file_tool"]],
verbose=False,
)
crew_manager.create_agent(
role="Agent2",
goal="Describe Goal",
backstory="Write the backstory for Agent2",
tools=[crew_manager.tools["file_tool"]],
verbose=False,
)
crew_manager.create_agent(
role="Agent3",
goal="Describe Goal",
backstory="Write the backstory for Agent3",
tools=[crew_manager.tools["file_tool"]],
verbose=False,
)
# Create tasks
crew_manager.create_task(
name="Task1",
description="Give a brief description",
tools=[crew_manager.tools["search_tool"], crew_manager.tools["rag_tool"]],
)
crew_manager.create_task(
name="Task2",
description="Give a brief descriptiont",
tools=[crew_manager.tools["file_tool"]],
)
# Create and run crew
crew_manager.create_crew()
question = crew_manager.get_user_input()
crew_manager.run_crew()
if __name__ == "__main__":
main()
Environment Configuration
.env
# Required API Keys
OPENAI_API_KEY=your_openai_api_key_here
# Optional API Keys (for additional tools)
SERPER_API_KEY=your_serper_api_key_here
BROWSERLESS_API_KEY=your_browserless_api_key_here
# Optional Configuration
OPENAI_MODEL_NAME=gpt-4
OPENAI_TEMPERATURE=0.7
Use Case Examples
Content Creation Pipeline
Perfect for creating research-backed articles, blog posts, or reports. The default workflow handles research → writing → quality analysis.
Market Research
Modify the researcher agent to focus on market analysis, competitor research, or trend identification.
Technical Documentation
Customize agents for technical research, documentation writing, and technical review processes.
Educational Content
Adapt for creating educational materials, course content, or training documentation.
Benefits of This Approach
Zero Setup Time: Copy one file and you’re ready to go.
Complete Control: Everything is visible and modifiable in one place.
Easy Debugging: No need to trace through multiple files to understand the flow.
Perfect for Learning: See how all CrewAI components work together in one view.
Rapid Prototyping: Quickly test ideas without project setup overhead.
Portable: Share your entire CrewAI project by sending one Python file.
Conclusion
This single-file CrewAI template removes all the complexity of multi-file project structures while providing a complete, functional multi-agent system. It’s perfect for:
- Learning CrewAI concepts
- Rapid prototyping
- Small to medium projects
- Sharing and collaboration
- Quick demos and experiments
Start with this template, modify it for your specific needs, and scale up to more complex structures only when necessary. Sometimes the simplest solution is the best solution!
If you’re new to multi-agent systems, you might want to check out my post CrewAI: Multi-Agent Systems Made Simple first.
Leave a Reply