Unlike most psalm superscriptions, which name only a genre and sometimes a musical instruction, Psalm 142's heading ties the prayer to a specific moment in the David narrative, his time hiding in a cave from King Saul, an episode told twice in the books of Samuel, at Adullam and again at En-gedi. Modern scholarship generally treats psalm superscriptions as later editorial additions connecting older liturgical poetry to narrative moments in David's life rather than as reliable authorial records, a caution that applies here as much as anywhere in the Psalter, and the psalm's own text supports that caution: nothing in its wording specifically requires a cave, a king, or a named pursuer, and its language of persecution and abandonment reads just as naturally as a general complaint usable by any petitioner in trouble as it does as a specific autobiographical episode.
What the cave setting does is give an otherwise generic lament-petition a narrative anchor that shaped how later readers approached it, inviting them to read David's isolation, hiding underground with nowhere left to turn and no human ally, as the concrete situation behind the psalm's abstract language of having no refuge and no one who cares. That anchoring effect, a specific story lending emotional weight to language general enough to fit almost any distress, recurs across several of the Davidic psalms and is part of why they proved so durable outside their original setting.