Looking at the state of things, there is something that does not sit well with spaceports.
They are a good population/tax boosts through creating happiness bonuses. The AI is not too shabby in spending money, at least while at war, but still does not grasp well the way money is heading due to bonus income. And in war they seem more prone to wanting ships than bases, even if I think base building also benefits from the spending sprees then.
The default policy is to have spaceport sizes set by population levels, which makes the AI put large spaceports everywhere after a while. To balance this the AI got a minimum distance between them, which puts things somewhat in check. The bad part about this is that major colonies end up with no spaceport. In my latest (current) game, I saw an AI regional capital lack spaceport, because a nearby (much poorer) colony had one.
I would think the game would benefit of rethinking what a spaceport is. At least as a colony happiness boost. The AI should also realize that it may be a good idea to do other things than spam only large spaceports after a while.
My current tech is approaching endgame items. Cost of relevant bases are:
Large spaceport: 53k to buy, 10.5k to maintain.
Medium spaceport: 32k to buy, 5.3k to maintain.
Small spaceport: 16k to buy, 3.1k to maintain.
Defensive base: 23k to buy, 4.4k to maintain.
A defensive base has firepower and defenses comparable to a medium spaceport (but a bit less shields). The Small spaceport got about half that.
The AI builds some spaceports, which all end up large (unless the colony is crap), and builds a host of defensive bases. One defensive base does nothing more than a small spaceport in single defense against an AI colony attack: nothing. Both deter pirates well, but the single defensive base is placed wrong and only protects from one direction. And troop transports are not killed that easily, I don't think even a large spaceport kills many at all, since nimble frigates arrives first to eat the shots (and die). The AI also likes to place strike fleets at colonies, which is good protection against small threats like normal pirates or enemy strike fleets hunting a small spaceport.
I would like to change the game so that the small spaceport is smaller, and widely used. It is built for every colony where the AI currently thinks a defensive base may be relevant, and even a bit more freely. More than one in each system should be OK. My small spaceports got 12 lasers, 6 torpedoes, 4 point defense. The numbers could be halved. The research labs could go. This reduces both purchase cost and maintenance. If one also removes one construction yard, one can easily remove a reactor too (and possibly habitation stuff). With these changes the maintenance (the important stat) goes to 2487 in my game, from 3100.
In my current game, the extra small spaceport (weakened) would cost 2500, and the gain in taxes is not easy to calculate (15 happiness), and any gain in population growth even harder (but I figure taxes would cancel that). Comparable maintenance is 3000 for cruiser, 1886 for destroyer, 1288 for frigate. The small spaceport cost the same as half an AI strike fleet. It is small change this far into the game.
The AI could also discover what the enlightened player knows: Cargo bays on spaceports are irrelevant. And they probably should be, because if not resources would be lost on a spaceport destruction, which would cripple an empire massively. I doubt a lot of maintenance is spent on 5 extra cargo bays, though.
Upgrade to medium spaceport at good colonies. And balance a large spaceport minimum distance and a minimum number of large spaceports, reserving them as ship building centers. Maybe a minimum distance for medium as well, since one hardly needs more than one in a system (I upgrade where I want some more ship building ability, defense and/or a long range scanner). Use "increased defense" as a criteria for retrofitting to larger size - if the AI wants a defensive base, prefer to upgrade the spaceport instead. If there is no further upgrade, build the base.
In my AAR game, I got 31 colonies in 25 systems. My AI currently have 8 large, 2 medium spaceports, and 11 defensive bases. If I built spaceports myself, I would probably have 31 spaceports, 3 large, 8-10 medium, and the rest small. And some defensive bases on the most valuable colonies (less than 5). If the AI switched from defensive bases to small spaceports, it would be close to filling the world with spaceports, since most defensive bases are alone at their colonies. This AI is not defensive, though, only the top colony in the galaxy got a defensive base in addition to a spaceport (the capital only got spaceport). Other AIs are different in that respect.
The evil ways of my (and other) AI is seen in the policy screen (it is a mod AI, but this should be similar for all)
Minimum distance between spaceports: 700K.
Minimum population for Large spaceport: 3000M
Some extra logic for upgrade from medium to large would be wise, and scrap that distance limit. But also keep a distance limit between medium or larger, to avoid drowning in maintenance cost. Remember that small spaceports should be cheaper.
And a better AI grasp of spending never hurts, but this is probably a core problem in the game economy. Bonus income is unpredictable in nature. If all AI had better grasp of this, the amount of money sent as gifts should go down, though.