HIV-associated neurocognitive disorder (HAND) affects approximately half of HIV-infected patients. Loss of synaptic connections is a hallmark of many neurocognitive disorders, including HAND. The HIV-1 protein transactivator of transcription (Tat) disrupts synaptic connections both and and has been linked to impaired neurocognitive function in humans. studies have shown that ifenprodil, an antagonist selective for GluN2B-containing NMDARs, reverses synapse loss when applied after Tat. Here, we tested the hypothesis that Tat-induced loss and ifenprodil-mediated rescue of synaptic spines would predict impairment and rescue of cognitive function. Using intracranial multiphoton imaging, we found that infusion of 100 ng of HIV-1 Tat into the lateral ventricle of yellow fluorescent protein-expressing transgenic mice produced a 17 ± 1% loss of dendritic spines in layer 1 of retrosplenial cortex. Repeated imaging of the same dendrites over 3 weeks enabled longitudinal experiments that demonstrated sustained spine loss after Tat infusion and transient rescue after ifenprodil administration (10 mg/kg, i.p.). Parallel trace fear conditioning experiments showed that spine loss predicted learning deficits and that the time course of ifenprodil-induced rescue of spine density correlated with restoration of cognitive function. These results show for the first time that, during exposure to an HIV-1 neurotoxin , alteration of GluN2B-containing NMDAR signaling suppresses spine density and impairs learning. Pharmacological inhibition of these NMDARs rescued spines and restored cognitive function. Drugs that rescue synapses may improve neurocognitive function in HAND. Synaptodendritic damage correlates with cognitive decline in HIV-associated neurocognitive disorder (HAND) patients. We developed an imaging approach for longitudinal tracking of spine density that enabled correlation of synaptic changes with behavioral outcomes in a model of HAND. We show for the first time that spine loss after exposure to an HIV-1 protein can be reversed pharmacologically and that loss and recovery of dendritic spines predict impairment and restoration of cognitive function, respectively. Therefore, synapse loss, the hallmark of cognitive decline in HAND, is reversible. Drugs that restore spine density may have broad application for improving cognitive function during the early phases of neurodegenerative diseases.