We investigate the cooperation among energy prosumers (unified energy provider and consumer) through the energy packet network (EPN) paradigm, which represents both the flow of work that requires energy, and the flow of energy itself, in terms of discrete units. This paper details a stochastic model of EPNs, which is inspired from a branch of queuing theory called G-networks. The model allows us to compute the equilibrium state of a system that includes energy storage units, energy transmission networks, and energy consumers, together with the intermittent energy sources. The model is then used to show how the flow of work and energy in the system can be optimized for certain utility functions that consider both the needs of the consumers, and the desire to maintain some reserve energy for potential future needs.