Most cortical synapses are local and excitatory. Local recurrent circuits could implement amplification, allowing pattern completion and other computations1,2,3,4. Cortical circuits contain subnetworks that consist of neurons with similar receptive fields and increased connectivity relative to the network average5,6. Cortical neurons that encode different types of information are spatially intermingled and distributed over large brain volumes5,6,7, and this complexity has hindered attempts to probe the function of these subnetworks by perturbing them individually8. Here we use computational modelling, optical recordings and manipulations to probe the function of recurrent coupling in layer 2/3 of the mouse vibrissal somatosensory cortex during active tactile discrimination. A neural circuit model of layer 2/3 revealed that recurrent excitation enhances sensory signals by amplification, but only for subnetworks with increased connectivity. Model networks with high amplification were sensitive to damage: loss of a few members of the subnetwork degraded stimulus encoding. We tested this prediction by mapping neuronal selectivity7 and photoablating9,10 neurons with specific selectivity. Ablation of a small proportion of layer 2/3 neurons (10–20, less than 5% of the total) representing touch markedly reduced responses in the spared touch representation, but not in other representations. Ablations most strongly affected neurons with stimulus responses that were similar to those of the ablated population, which is also consistent with network models. Recurrence among cortical neurons with similar selectivity therefore drives input-specific amplification during behaviour.