I'm 46. I think this question is going to be different depending on who you are, where you are, your background, network, etc. I've always been a freelance programmer and a lot of times actually just scraping by.
At the moment I have a good contract.
Ageism definitely becomes a factor. One thing that is important is not to contribute to it. For example, part of your post implies that as you get older you will struggle to keep up with relevant technologies.
The real concern for every software engineer and actually every job is the powerful AI that we have to anticipate within the next 0-10 years.
OpenAIs newest model, although amazing, still has some weird brittleness to it, so it's not going to literally replace an actual software engineer easily. But it is a significant step closer than the day before yesterday. Probably at least 15-20% better, or a lot more depending on how you measure it.
I keep seeing people reacting to the current state of the art AI and saying that they are not concerned. This is incredibly short-sighted. The history of technology is hundreds of major innovations breaking through performance wall after performance wall.
There are not only upgrades but also new paradigms on the way for increasing AI performance, in the areas of hardware, ML model architecture, and software. And the reality is that with this latest OpenAI model and the right feedback loop and constrained platform/domain, we can actually automate quite a lot of software engineering work.
As AI is integrated better with systems, we will definitely need fewer people doing what currently is considered software engineering. And again, don't think about what the current model can do. Look at the trajectory of history over the last 60+ years of computing. Engineers always break through barriers and consistently increase performance by orders of magnitude.
This is not just a concern for software engineering but for every job. But it can be a huge improvement if society is able to adapt and take advantage of it in an equitable way.
I think the best option is to start leveraging AI and robotics to build out goods and services. Individuals without capital or very little capital can take advantage of AI and robotics to become entrepreneurs.