You won't avoid it in the future. You will always get that one extra request, if not by your student, then by your head of department who needs a last-minute input, your colleague that finds that one proposal deadline in two days, etc.
Many people suggest to just say no to taking things on. This is a common oversimplification and it needs to be considered judiciously. Saying no is particularly difficult for a junior academic.
You can deny the student, but you might deprive them of a vital career opportunity. You can may say no the head of department, but that's costing you political capital. You can say no to your colleague and, next time, they will ask someone else to join their project.
As consequence, you need to evaluate if that's a cost you are ready to bear. As you become more senior, this may be easier to do. However, as long as you are in a junior phase of your career, you may be reluctant to do so, since you still need to build up your reputation.
One part of the response is, partly, to keep a few buffers in your schedule, as other answers suggest. But, I'll be frank, that may simply not be possible. No matter how much the mantra of work/life balance is repeated today - the life of an academic, especially a junior one, has extremely busy stretches. Not all the time, and there also is a lot of flexibility; but, let's face it, over phases, you may be drastically oversubscribed. It's just the way it is.
Therefore, you need to look to alternative trade-offs, and the most important one is to learn to work to the available time slot and trade in quality for time.
Note that, with your researcher hat on, you want to ensure the highest degree of quality for your scientific work, especially the carrying out of the research and the write-up of the papers. These require (ideally) the highest degree of clean-up, consolidation and polishedness and can in general not be rushed. Here, you need to take your time and give every detail its due respect.
For non-scientific routine work, however, it is often well possible to accept an effective less-than-perfect approach.
For a LOR, for instance, you can hack one out by writing how you met the student, your experience with them, a general evaluation based on this evidence, and your conclusions about whether they are suitable for the job. A pithy and well-written 3-4 paragraphs LOR is, for a talented undergrad, a perfectly sufficient space to help them into a strong program. It's far easier to write a LOR for a very bright student than for a generally support-worthy, but not quite as strong one.
With a bit of experience, a strong LOR can be written well in 20-30 minutes (without AI!). But you need to accept that it will probably not be as powerful a LOR as you could devise if you had 2 or 3 hours. That's the price that the student needs to pay for coming in the last minute; not because you wish that price to be paid, but simply by force of the circumstances.
But at the same time, knowing you can trade this off gives you the elasticity that you need to add this one more thing into your packed plate.