Well, you need to do the value proposition analysis basically. When a company is hiring a software developer, what value do they expect to bring in (i.e. what would change if the position was never filled) and why would they prefer an older candidate over a younger one? As long as you can definitively answer this question (and find employers that share the same point of view), you will stay employed.
In reality there's one problem though. I don't know if this is specific to software engineering, or just a part of the human nature, but the reasoning behind many hires I've seen is dead simple: get someone at the bottom of the food chain so that the other participants can move higher. What would change if the position would not get filled? Nothing business-wise, the product would still be around and would solve exactly the same problems in exactly the same way, just Alice the Department head would have fewer head count and Bob the Developer would be still writing the code himself instead of mentoring the new hire. As you can see, in this scenario there's no benefit at all of hiring an experienced person - instead you need to hire someone who would desperately need mentoring and would be just smart enough to follow orders.
So if you want to stay in the industry past the age of a cute aspiring busboy, you need to learn how to spot job opportunities where the experience you would bring in is actually beneficial to the business. But if you learn to do that, you may be much better off starting your own consulting business, or growing into product management.
Of course, many of us (me included) are introverts and absolutely hate dealing with people, preferring solving the logical puzzles in solitude. The trouble is - a huge part of your puzzle solving skill is specific to the type of the puzzle, and this knowledge tends to get quickly outdated when the technology changes. On the other hand, experience in being nice to people and convincing them to do something that would benefit the business (a.k.a. management) is pretty universal (as well as the political skills) and won't get outdated when a company switches from scrum to kanban or vice versa.
P.S. Yes, the general ability to think structurally, break down complex problems, etc. is portable between different tech stacks, but you would never convince a non-technical person about it, unless you have invested a lot into your persuasion skills.