Behavioral flexibility is the ability to adjust behavioral strategies in response to changing environmental contingencies. A major hypothesis in the field posits that the activity of neurons in the locus coeruleus (LC) plays an important role in mediating behavioral flexibility. To test this hypothesis, we developed a tactile-based rule-shift detection task in which mice responded to left and right whisker deflections in a context-dependent manner and exhibited varying degrees of switching behavior. Recording spiking activity from optogenetically tagged neurons in the LC at millisecond precision during task performance revealed a prominent graded correlation between baseline LC activity and behavioral flexibility, where higher baseline activity following a rule change was associated with faster behavioral switching to the new rule. Increasing baseline LC activity with optogenetic activation accelerated task switching and improved task performance. Overall, our study provides important evidence to reveal the link between LC activity and behavioral flexibility.