There are two main types of time loops, each with their own subcategories that often overlap. In one type, things don't change from cycle to cycle. In the other, they do. Simple as that. From what I have been able to gather, they can last anywhere from a few hours to a year.
Now, unlike in fiction, it's not usually as simple as figuring out the code to get out, be it some lesson learned or some life saved. It's not unheard of for a trickster of sorts to be the cause of the loop. Furthermore, the loop may not even be meant for you. It then raises the question of what to do.
To be put plainly, there is no one size fits all solution. Sometimes it's a matter of trial and error-- which, when the second type of loop, can be risky. If things can change, death will likely stick. Therefore, the first step is to figure out which sort of loop it is.
Both types come with their unique challenges. With the first type, you will lose any and all progress you make day to day-- written notes, drawings, anything other than your own knowledge. With the second, as mentioned before, death and injury may stick. In addition, people may begin to grow suspicious if you begin to behave strangely.
Once you know what sort, you can begin to work on solutions. Again, as there are many different causes, often all you can do is trial and error. Search for any patterns, suspicious figures in your life. Look at how long these things have been going on for, and figure out what the best course of option is to remedy the issue at hand.
TL;DR: Time loops are a complicated and confusing matter, and the best way to escape one is to figure out what exactly is going on.