Cogeneration systems are widely acknowledged as a viable solution to reduce energy consumption and costs, and CO2 emissions. Nonetheless, their performance is highly dependent on their capacity and operational strategy, and optimization methods are required to fully exploit their potential. Among the available technical possibilities to maximize their performance, the integration of thermal energy storage is recognized as one of the most effective solutions. The introduction of a storage device further complicates the identification of the optimal equipment capacity and operation. This work presents a cutting-edge methodology for the optimal design and operation of cogeneration systems with thermal energy storage. A two-level algorithm is proposed to reap the benefits of the mixed integer linear programming formulation for the optimal operation problem, while overcoming its main drawbacks by means of a genetic algorithm at the design level. Part-load effects on nominal efficiency, variation of the unitary cost of the components in relation to their size, and the effect of the storage volume on its thermal losses are considered. Moreover, a novel formulation of the optimization problem is proposed to better characterize the heat losses and operation of the thermal energy storage. A rolling-horizon technique is implemented to reduce the computational time required for the optimization, without affecting the quality of the results. Furthermore, the proposed methodology is adopted to design a cogeneration system for a secondary school in San Francisco, California, which is optimized in terms of the equivalent annual cost. The results show that the optimally sized cogeneration unit directly meets around 70% of both the electric and thermal demands, while the thermal energy storage additionally covers 16% of the heat demands.