During chronic viral infection, virus-specific CD8+ T cells become exhausted, exhibit poor effector function and lose memory potential1,2,3,4. However, exhausted CD8+ T cells can still contain viral replication in chronic infections5,6,7,8,9, although the mechanism of this containment is largely unknown. Here we show that a subset of exhausted CD8+ T cells expressing the chemokine receptor CXCR5 has a critical role in the control of viral replication in mice that were chronically infected with lymphocytic choriomeningitis virus (LCMV). These CXCR5+ CD8+ T cells were able to migrate into B-cell follicles, expressed lower levels of inhibitory receptors and exhibited more potent cytotoxicity than the CXCR5+ subset. Furthermore, we identified the Id2-E2A signalling axis as an important regulator of the generation of this subset. In patients with HIV, we also identified a virus-specific CXCR5+ CD8+ T-cell subset, and its number was inversely correlated with viral load. The CXCR5+ subset showed greater therapeutic potential than the CXCR5+ subset when adoptively transferred to chronically infected mice, and exhibited synergistic reduction of viral load when combined with anti-PD-L1 treatment. This study defines a unique subset of exhausted CD8+ T cells that has a pivotal role in the control of viral replication during chronic viral infection.