As social order collapses in the West, we will see a return to a relic of a former era: white flight. Much as they fled to suburbs, people with options will flee the decaying cities and companies will accommodate them by setting up places of business there.
This exemplifies competition in the capitalist sense. If you can offer your workers better homes, then it becomes worth the cost of moving shop in order to compete for those workers. Like some companies offer better benefits now, future companies will offer admission into city-states which will be small cities or clusters of large towns where membership is regulated, like in a co-op.
To support this order, regulatory takeover at the national level will be required. These city-states will need to be self-maintaining and, in exchange for a portion of their taxes, protected by the national infrastructure which will also hire out many of their locals as soldiers. This can only work if national governments do not plunder where convenient, which will happen if the majority in the megacities vote for it.
Disenfranchisement of the megacity base will require a transition from what we think of as democracy. In an ideal situation, the community will be unified by identity (culture, heritage, values, language, customs) and will choose its best members for an aristocracy. If people keep hoping for a magic bandaid to democracy, there will be for some time a democracy limited to localities or representatives of each city-state.
Without this type of national level control, the megacities will quickly vote any other cities into debt slavery through taxes to pay for social programs, and those will then collapse as their inhabitants flee to gated communities in the city with private security as we see in South America. Even more, governments will not hesitate to wage war on city-states which show a better way of life because it will offend national political consensus.
For this reason, city-states will not arise in a bottom-up fashion but a top-down one when a military dictatorship or other political intermediate salvages the failing democracy. This new stronger entity will default to libertarian principles as a means of both avoiding internal conflict and keeping its power intact. That entails allowing freedom of association, which will rapidly give rise to city-states or other areas for the privileged to separate them from the decline.
The great threat to this order is the potential collapse of the national government. If it goes, the larger numbers of the megacities will rule the day and the city-states will be sacrificed to protect that consensus. Most likely the city-states will protect themselves with espionage and calculated corruption, as happens in most of the world, buying off police and government forces, but this is a temporary fix as inevitably costs will rise beyond sustainability.
At some point, enough rage will accumulate to transfer those bribes and taxes into mercenary payments and overthrow the national government. At that point, a far-sighted leader will participate in a mass exile of the megacity populations, leaving only city-states. But that requires both wisdom and a population willing to support it, instead of succumbing to the human tendency to be morally lazy and apply a band-aid, sealing the wound but leaving mortal long term problems intact.