Cognitive control is the ability to withhold a default, prepotent response in favor of a more adaptive choice. Control deficits are common across mental disorders, including depression, anxiety, and addiction. Thus, a method for improving cognitive control could be broadly useful in disorders with few effective treatments. Here, we demonstrate closed-loop enhancement of cognitive control by direct brain stimulation in humans. We stimulated internal capsule/striatum in participants undergoing intracranial epilepsy monitoring as they performed a cognitive control task. Stimulation enhanced performance, with the strongest effects from dorsal capsule/striatum stimulation.We then developed a framework to detect control lapses and stimulate in response. This closed-loop approach was more effective than open-loop stimulation. Participants who self-reported difficulties with self-control reported that stimulation made them more able to shift attention away from internal distress. Finally, we decoded cognitive control directly from activity on a small number of electrodes, using features compatible with existing closed-loop brain implants. Our findings suggest a new approach to treating severe mental disorders, by directly remediating underlying cognitive deficits.