This project implements a version of the Mafia party game where language model agents (LLMs) play the roles of players, using their natural language processing capabilities to interact, discuss, and deduce the identities of other players.
Mafia is a social deduction party game (similar to Werewolf) where multiple players take on different roles and try to identify the Mafia members among them. This project implements a Mafia game where language model agents (LLMs) play different roles and interact with each other. The game alternates between Day and Night phases, with players working to identify the Mafia members or eliminate the Villagers, depending on their role.
- Villager: No special powers, can vote and discuss
- Mafia: Works secretly to eliminate one player per night
- Doctor: Can choose one player to protect each night
- Detective: Can investigate one player per night
- Godfather: Mafia leader who can override decisions and may appear innocent when investigated
- Players are randomly assigned roles
- The game alternates between Day and Night phases:
- Day Phase: All players discuss and vote to eliminate a suspected Mafia member
- Night Phase: Mafia members choose a player to eliminate, while the Doctor and Detective use their special abilities
- The game continues until either:
- All Mafia members are eliminated (Village team wins)
- Mafia members outnumber Villagers (Mafia team wins)
- Multiple LLM agents playing different roles (OpenAI, Anthropic, Google, Llama, etc.)
- Support both online models (through API) and local models (using OLlama)
- Agent memory system to track game events and discussions
- Role-specific behaviors and actions
- Day/Night phase transitions
- Voting and elimination mechanics
- Web UI for visualizing the game
- Clone the repository:
git clone https://github.com/mahbodnr/LLMafia.git
cd mafia-game- Create and activate a virtual environment:
python3 -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate- Install dependencies:
pip install -r requirements.txt- Set up API keys for LLM providers:
export OPENAI_API_KEY="your_openai_key"
export ANTHROPIC_API_KEY="your_anthropic_key"
export GOOGLE_API_KEY="your_google_key"To run the game with default settings:
python -m src.gameWith custom settings:
python -m src.game --players 10 --mafia 2 --godfather True --doctor True --detective True --rounds 2To start the web interface:
python -m ui.appThen open your browser and navigate to http://localhost:5000.
To run the test suite:
python -m unittest discover testsThe game can be configured through the config.py file or by passing command-line arguments. Key configuration options include:
- Number of players
- Role distribution
- Discussion rounds per day
- Game mechanics (e.g., whether the Godfather appears innocent)
To add a new role:
- Add the role to the
PlayerRoleenum inmodels.py - Update the team alignment logic in the
Playerclass - Implement role-specific behavior in the agent classes
- Add role-specific actions to the night phase controller
The game supports multiple LLM providers through the agent factory pattern. To add a new provider:
- Create a new agent class that inherits from
BaseAgent - Implement the required methods (
initialize_llm,generate_response, etc.) - Add the new provider to the
create_agentfactory function
This project is licensed under the MIT License - see the LICENSE file for details.
Contributions are very welcome! Please open an issue or submit a pull request, or just reach out to me directly. If you want to contribute but don't know where to start, you can take a look at the To-Do list below.
- Add User Agent (for human players)
- Debug and improve the replay system
- Add parallelization for LLM calls (Batch input?)
- Fix the bugs with the web UI:
- Votes and Actions do not appear at the correct round in the agent's history
- Extend the memory of the agents to several games
- Train LLM models with Unsupervised Reinforcement Learning based on the game outcomes




