Implanted electrodes
There's a real-life parallel that is used for patients with intractable epilepsy, at least in the sense that it uses electrodes implanted in the brain to record activity. In this case, the implanted device detects the onset of seizure activity and applies electrical pulses to prevent the seizure. In the long run, this may reduce the need for epilepsy surgery in which the part of the brain from which seizures originate is removed. This device is somewhat less invasive than surgery, and has the advantage that it could be removed (the changes from surgery are most certainly permanent).
https://www.neuropace.com/patients/neuropace-rns-system/#how-it-works
Other devices include deep brain stimulators, which stimulate but don't record electrical activity. They're on all the time, though they can be controlled externally to change the stimulus parameters (frequency, amplitude) or turn them off if necessary. They're used mainly for Parkinson disease and dystonia.
https://www.hopkinsmedicine.org/health/treatment-tests-and-therapies/deep-brain-stimulation