Three central themes in the study of the phenomenon of resistive switching are the nature of the conducting phase, why it forms, and how it forms. In this study, the answers to all three questions are provided by performing switching experiments in situ in a transmission electron microscope on thin films of the model system polycrystalline SrTiO3 . On the basis of high-resolution transmission electron microscopy, electron-energy-loss spectroscopy and in situ current-voltage measurements, the conducting phase is identified to be SrTi11 O20 . This phase is only observed at specific grain boundaries, and a Ruddlesden-Popper phase, Sr3 Ti2 O7 , is typically observed adjacent to the conducting phase. These results allow not only the proposal that filament formation in this system has a thermodynamic origin-it is driven by electrochemical polarization and the local oxygen activity in the film decreasing below a critical value-but also the deduction of a phase diagram for strongly reduced SrTiO3 . Furthermore, why many conducting filaments are nucleated at one electrode but only one filament wins the race to the opposite electrode is also explained. The work thus provides detailed insights into the origin and mechanisms of filament generation and rupture.