The obvious suggestion is: Count out loud and practice.
If I understand your question correctly, IMO that's not the obvious suggestion, although maybe in the end it amounts to what I'm going to say: If you're improvising and you lose your place, it's because you don't know the tune well. I had a teacher who told me many times: You need to know the tune well enough so that no matter how bad you screw up, by the time the next bar starts, you're back in place.
To improvise freely, the changes have to be automatic to you - reflex. That's what Charlie Parker meant when he said "learn the changes and then forget them": Get to the point where the changes are out of your conscious mind entirely - they have become reflex. Then you are free to 'play what you hear'.
So the solution is to listen carefully to yourself, and learn the changes well, so that know right away when you've drifted astray, and "get back to where you once belonged". Ultimately you get to a point where you know the changes so well that it's virtually impossible for you to lose your place, because you're on 'auto pilot'. I'm a bass player, so I have to learn the changes well. It has to be automatic for me unless I want to sit and stare at a sheet all the time, which is boring. But it's the same for anyone who wants to be able to improvise well.
An expert musician can analyze and digest changes on the spot and not lose their place. You're not up to that - you need to put work in.
To ingrain the changes in your head, a beginner must practice playing the piece plenty, and understand it theoretically as much as possible. As a rule, the better you understand a tune, the easier it is to remember. And when you practice, you must play the tune simply - the basics - the main melody line and the fundamental changes, paying plenty of attention to what your left hand is up to, because the bass end of a tune and its chords will usually outline its fundamental structure. You can always fool around a little to keep it interesting - develop the beginnings of improvisational ideas - but stick very close to the basics.
However, if I let myself improvise freely, I quickly (or immediately)
lose my place in the music... What are some mental tricks to avoid this?
Note: There is a problem with your question:
The solution is not mental tricks - the solution is work.
I'm hearing from your question two things.
- You don't know the tune well - as mentioned:
- You're getting filled with energy and inspiration when you go to solo and you want to get it all out -that's not a bad thing, but only if you can keep your place - otherwise you are courting disaster:
There is nothing worse than losing your place in the changes, except for a drummer or bass player losing the groove: If you screw up - play a lick or two that are off in la-la land - as long as you know you are off and know your place, you can recover quickly, the band can move on and nobody will notice very much. But if you lose your place and keep soloing, or you stop and stutter and hesitate, the wheels start to come off.
Suggestions:
- When you go improvise, you need to take it gradually. Put the
reins on yourself: Control your improvisation - keep it cool and
close to the changes so you don't lose your place. Dig in and stay
close to the basics - you don't have to play like Herbie Hancock in one day.
As you become more comfortable with a tune, you can expand - but
never lose control - always know your place.
- One of the things I sometimes do when I practice is I'll play along
with something and then move the tune suddenly to a new place - hit the
forward button with my elbow or something - and then make sure I can
pick it up wherever it happens to come out.
- Really just an expansion of the first point: The biggest mistake
beginning soloists and improvisers make is trying to do too much.
That causes lots of bad things to happen, among them is losing
your place. Try listening to some great musicians - pianists and
others - who were noted minimalists: Count Basie, Thelonious Monk
(in certain periods and certain tunes...) Ray Charles - even some of
the great blues artists like Muddy Waters and Shaky Walter Horton, or
someone like Keith Richards (for many years a great musician that never got the recognition he deserved - lately that's changed, thanks to his book). From listening to musicians like that,
you learn that making great, expressive music - playing a "great
solo" - doesn't require you to DO A LOT. It's more important to be focused and articulate.
For the sake of music, here are some great minimalists at work:
Monk on a GREAT recording from 1959 - 5x5, featuring Thad Jones on cornet. The tune is Jackie-Ing. It's Monk's tune and Monk's session, but his solo is only one chorus, starting around 4:45 in, and is compromised of a few very Monkish abstractions built off of Jones's solo and then a stylized reprisal of the theme - that's his whole solo - it's less than a minute long:
Here's Ray Charles in 1957 covering Horace Silver's Doodlin - his solo starts around 1:20 in. Two quick choruses, less than a minute all told - but so clearly thought out and crafted.
I know you're not Monk or Charles and you may have entirely different tendencies, but if you're having trouble keeping your place, starting with a minimalist approach is the way to go IMO.
(Note in particular that in both of those solos there is a great deal of silence/space. I attribute that to two factors: 1 - those guys didn't play until they had a clear idea in their head/ears of what they were going to play - they took time to think and listen to themselves and the rest of band as they played [Contrast that with so many less astute musicians, who think that a great solo means blasting as many notes as possible into as little space/time as possible, while oblivious to their surroundings.]; 2- to make good music you don't just play notes, you also make judicious use the time and space between the notes - the silence is part of the music.)
And just for the "record", here's Muddy Waters with Little Walter on an early 50s recording: Sad Letter - featuring a guitar solo (@1:35) we can all learn a lot from:
And yes, the Stones: The Last Time - 1965 - Lip Synced from the recording. Guitar solo starts at about 1:35 in. Another great lesson to be learned from it this one: Simple, clear, well constructed and very tasteful and effective, IMO:
Less is more.
That same teacher told me 'when you start to think about how good it's sounding, you're heading for trouble'. Yes - that is for bass, and it's too true. But the concept is the same always, for any instrument - until you're a virtuoso, you need be mindful and not get lost in what you're playing. Pretend you're flying a supersonic jet in tight formation::

Lose track of what you're doing for a half a second, you could end up like this:
