As someone who gets tired around other people, I usually don't know in advance when I will want to leave because I'm tired. This has lead me to use the following technic over the years.
A warning beforehand
When I go to a party and I'm a bit tired even before getting there, I warn the host in advance (or at the beginning of the party) with something like:
I'm sorry but I will probably need to leave early (because I have other stuff to do/because I need to be well rested for tomorrow).
I usually take care of not giving a specific hour so that I can leave when I want. If it turns out that I wasn't that tired and decide to stay longer than I thought, it's not a problem. If my host is surprised to see me stay that long, I just say something like:
I'm having too much fun, my chores will have to wait.
So, now your host is pleased because you decided to stay the regular length of time!
Time to leave
When I get tired of any the social gathering and I want to leave, no matter if I warn the other people that I might leave early, I use the following:
I watch my watch
I wait a bit (so you won't seem rude)
I watch my watch again and say something like: "I'm sorry but I will need to leave soon"
I wait again (again, in order to not be perceived as rude)
After some time, if the other people don't put a stop to the conversation, I:
watch my watch a few time
start to fidget on my chair
finally say: "It was nice spending time with you but I need to go now"
All those steps allow the other person to mentally prepare themselves for the fact that you will be leaving soon. This way, your departure won't seem abrupt.
edit in response to comment:
The second part of these step is to make the other person feels rude if they don't put an end to the conversation.
If you are afraid that this approach will make you seem rude, as I'm not in other people mind I can't know for sure, but no one ever told me I was being rude (but they might have think I was anxious/nervous and didn't want to be late for whatever was coming next).