L'esprit d'escalier (literally, stairway wit) is a French term used in English that describes the predicament of thinking of the right retort too late. Originally a witticism of Denis Diderot, the French encyclopedist, in his Paradoxe sur le Comédien.
The phrase can be used to describe a riposte to an insult, or any witty, clever remark that comes to mind too late to be useful—when one is on the "staircase" leaving the scene. A seldom-used English translation exists: "staircase wit".
As in the French counterpart above, treppenwitz literally means 'the wit [or 'joke'] of the stairs'. It is the striking reply that crosses one's mind belatedly when already leaving, on the stairs. People are often angry because they did not have the fitting answer directly during a conversation.
The German term is old, but it was made popular by W. Lewis Hertslet who published his book in 1882 entitled 'Treppenwitz der Weltgeschichte'. In that book, he writes: "Like to a petitioner who is just leaving after an audience, a piquant, striking words occurs to history almost always delayed." (German language Source)