how to protect udp packets from packets loss?

UDP is considered a "connectionless" protocol. There is no guarantee of delivery like there is for TCP.

"UDP does not provide error correction and is therefore an unreliable protocol. In other words, delivery of packets is not guaranteed. UDP datagrams are transmitted without provision for an acknowledgment. Because there is no virtual connection between sender and receiver, UDP is also said to be connectionless."

This is by design, and it is usually used for traffic that needs to be delivered/switched as fast as possible.

Using UDP as a measure for packet loss isn't good practice.

Quote is a reference from here: https://www.sciencedirect.com/topics/computer-science/user-datagram-protocol.
Emphasis is my own.