There are ICs that do that job for you.
As an example, the SA9102C IC (from SAMES) measures a value proportional do the kWh (or kW, maybe) and outputs an pulse for every given amount of energy consumed. All that you have to do is to count its pulses and multiply them by a constant given by the manufacturer, so you get the kWh (or kW).