How to stop a step2step generation model while streaming

Hello everyone, I want my model generates sequences. To make this nice I want to do it on stream.
But sometimes it acts bad (mainly repetition of specific tokens) So I would like to stop the generation/stream if I see these tokens.
Can I do this ?
Thanks,