How the engine can detect the 3-fold repetition using FEN input? When I run the engine (UCI interface), I usually feed the last position from the game using position fen ... command and get the best move back. The FEN encodes the possibility of the castlings, en passant moves and keeps the move count since the last piece capture or pawn move to enforce to 50-move draw rule.

How can the engine detect the 3-fold repetition and avoid it while playing the game? I've seen a few occasions when the engine is about 5-6 pawns ahead (a clear victory), but draws because it keeps repeating giving check to the opponent's king again and again.

Clarification: I DON'T program my own engine (well, I do, but this is totally unrelated to this question). In this question I use existing engine, think StockFish or Fruit or anything else you like. This existing engine is given the FEN and returns the "best" move it can think of. Sometimes this leads to a 3-fold repetition in a clearly won positions. How can I tell the StockFish, that it has already made this move before and should do better?
https://en.wikipedia.org/wiki/Forsyth%E2%80%93Edwards_Notation

"FEN does not represent sufficient information to decide whether a draw by threefold repetition may be legally claimed or a draw offer may be accepted; for that, a different format such as Extended Position Description is needed."

QED.