The charging loads of electric vehicles (EVs) at residential premises are controlled through a tariff system based on fixed timing. The conventional tariff system presents the herding issue, such as with many connected EVs, all of them are directed to charge during the same off-peak period, which results in overloading the power grid and high charging costs. Besides, the random nature of EV users restricts them from following fixed charging times. Consequently, the real-time pricing scenarios are natural and can support optimizing the charging load and cost for EV users. This paper aims to develop charging cost optimization algorithm (CCOA) for residential charging of EVs. The proposed CCOA coordinates the charging of EVs by heuristically learning the real-time price pattern and the EV’s information, such as the battery size, current state-of-charge, and arrival departure times. In contrast to the holistic price, the CCOA determines a threshold price value for each arrival and departure sequence of EVs and accordingly coordinates the charging process with optimizing the cost at each scheduling period. The charging cost is captured at the end of each charging activity and the cumulative cost is calculated until the battery’s desired capacity. Various charging scenarios for individual and aggregated EVs with random arrival sequences of EVs against the real-time price pattern are simulated through MATLAB. The simulation results show that the proposed algorithm outperforms with a low charging cost while avoiding the overloading of the grid compared to the conventional uncoordinated, flat-rate, and time-of-use systems.