Electrical neurostimulation is effective in treating neurological disorders, but associated recording artefacts generally limit applications to ‘open-loop’ stimuli. Since light does not prevent concurrent electrical recordings, optogenetics enables real-time, continuous ‘closed-loop’ control of brain activity. Here we show that closed-loop optogenetic stimulation with excitatory opsins (CLOSe) affords precise manipulation of neural dynamics, both in vitro, in brain slices from transgenic mice, and in vivo, with anesthetised monkeys. We demonstrate the generation of oscillations in quiescent tissue, enhancement or suppression of endogenous patterns in active tissue, and modulation of seizure-like bursts elicited by 4-aminopyridine. New network properties, emergent under CLOSe, depended on the phase-shift imposed between neural activity and optical stimulation, and could be modelled with a nonlinear dynamical system. In particular, CLOSe could stabilise or destabilise limit cycles associated with seizure oscillations, evident from systematic changes in the variability and entropy of seizure trajectories that correlated with their altered duration and intensity. Furthermore, CLOSe was achieved using intracortical optrodes incorporating light-emitting diodes, paving the way for translation of closed-loop optogenetics towards therapeutic applications in humans.