This paper presents a solution to the energy planning problem in buildings by implementing the net Zero Energy cost Building (nZEcB), a new concept that refers to buildings with zero or almost zero annual energy costs, using Artificial Intelligence (AI) techniques such as bidirectional long short-term memory, ordinary least squares linear regression, K-means, Pearson’s correlation, decision tree, and binary gravitational search algorithm. AI techniques are used to design the optimal structure of a distributed generation system. The system includes wind and photovoltaic renewable energy sources, a battery bank, and an automated capacitor bank for power factor compensation. A case study was conducted in a real public building with an annual consumption of 1.748 GWh, which resulted in the specification of a distributed generation system with a generation of 2.805 GWh per year. This system meets approximately 160.5% of the building’s electrical demand and 99.999% of annual energy costs, i.e., attending the nZEcB concept. The excess production of approximately 60% of the energy is necessary because the exported energy (feed-in tariff) has a lower value than that imported from the network, having to deduct costs with demand, in addition to having to compensate for losses in the battery banks. The project has a payback period of 6.79 years. This novel study demonstrates the feasibility and effectiveness of using AI techniques to achieve nZEcBs more efficiently, economically and sustainably.