Zero-dependency implementation of the ReAct (Reasoning and Acting) pattern from scratch using pure python and object-oriented programming. This agent can organize messy directories by reasoning about file organization and taking actions through filesystem tools.
Building this agent without relying on any third-party library or framework demonstrates a deep, hands-on understanding of agentic AI fundamentals and architectures. By implementing every component myself, I gained true insight into the LLM capabilities and limitations without abstract barriers.
- Python 3.11
- OpenAI API Key
- No Dependencies: Fully implemented using Python's standard library. No LangChain, CrewAI, or third-party frameworks.
- ReAct Pattern: Uses an LLM as the reasoning engine of the agent and makes decisions in a Think -> Action -> Observe loop.
- Filesystem Organization: Automatically organizes files by type, purpose, or custom criteria.
- Transparent Reasoning: Outputs the agent’s thinking process at every step, making debugging and agent logic easy to follow for users and developers.
At its core, CoT (Chain of Thought) prompt engineering technique is used to guide the agent's thinking process. It guides the agent to generate thoughts on the problem and then to take actions based on the thoughts to solve the problem in an iterative loop.
Here’s what the agent loop looks like:
- Think: Agent analyzes the context and reasons about how it can solve it.
- Action: Takes action (tool use) based on thoughts.
- Observe : Receives and evaluates the action results.
- Repeat: Continues until the problem is solved.
- get_working_directory(): Returns current working directory and structure.
- create_directory(paths: List[str]): Creates a directory with the given paths.
- move_files(mapping: Dict[str, str]): Moves a file to a new path.`
git clone https://github.com/PeymanKh/react-agent-from-scratch.git
cd react-agent-from-scratch
python -m venv .venv
source .venv/bin/activate # activate the environment
pip install -r requirements.txtCopy .env.example to .env as follows and add your OPENAI_API_KEY.
cp .env.example .env
# Next edit .envYou can create two variations of a messy directory for testing purposes:
- personal: A messy directory with personal files.
- developer: A messy directory with developer files.
# Create a messy personal directory
python setup_test_directory.py --scenario personal
# OR create a messy developer directory
python setup_test_directory.py --scenario developerBoth create a working_directory/ folder with unorganized files. You can explore it before running the agent by running cd working_directory && ls. here is an example of personal directory:
working_directory/
├── invoice_2024.pdf
├── vacation_photo.jpg
├── project_proposal.docx
├── budget_spreadsheet.xlsx
└── ... (15 unorganized files)
# Run the agent to organize the directory
python demo.pyAgent reasons and takes action in a loop of thinking -> Action -> Observation to organize the directory:
working_directory/
├── documents/
│ ├── invoice_2024.pdf
│ ├── project_proposal.docx
│ └── ...
├── media/
│ ├── vacation_photo.jpg
│ └── family_video.mp4
└── ...
Contributions welcome! Please submit a Pull Request.
MIT License - see LICENSE file.
- Name: Peyman Khodabandehlouei
- Website: https://www.peymankh.dev
- Email: peymankhodabandehlouei@gmail.com
Inspired by ReAct: Synergizing Reasoning and Acting in Language Models
