The rise in cyberattacks targeting critical network infrastructure has spurred an increased emphasis on the development of robust cybersecurity measures. In this context, there is a growing exploration of effective Intrusion Detection Systems (IDS) that leverage Machine Learning (ML) and Deep Learning (DL), with a particular emphasis on autoencoders. Recognizing the pressing need to mitigate cyber threats, our study underscores the crucial importance of advancing these methodologies. Our study aims to identify the optimal architecture for an Intrusion Detection System (IDS) based on autoencoders, with a specific focus on configuring the number of hidden layers. To achieve this objective, we designed four distinct sub-models, each featuring a different number of hidden layers: Test 1 (one hidden layer), Test 2 (two hidden layers), Test 3 (three hidden layers), and Test 4 (four hidden layers).We subjected our models to rigorous training and testing, maintaining consistent neuron counts of 30 and 60. The outcomes of our experimental study reveal that the model with a single hidden layer consistently outperformed its counterparts, achieving an accuracy of 95.11% for NSL-KDD and an impressive 98.6% for CIC-IDS2017. The findings of our study indicate that our proposed system is viable for implementation on critical network infrastructure as a proactive measure against cyber-attacks.