how can i see how many units are stacked in an enemy city?
In addition to the above, if you have a sufficiently large stack of bombardment units within range of the city (and are already at war, obviously!), then bombing the city and counting each successful hit on a previously healthy defender, until the top defender is one of the bomb-damaged units, will at least tell you how many 'strong' defenders are in the city. Depending on techs/resources available to the AI, this method might not tell you anything about the low-D, high-A 'attack' units in the city (unless you have sufficient Arty/ Bombers to damage them as well, after redlining the defenders) -- but attack-units are usually (much) easier to kill using comparatively weaker attackers (e.g. your Armies+Cavs+Knights can take out their Infs+Rifles+Pikes, then your Maces+LBMs+Swords can mop up their Cavs+Knights+Spears+Maces+LBMs).
Also, possible general rules of thumb, based on my experience (so good up to Emperor level at least). AICivs will:
- try to build (or retain) 1-2 defenders per town, 2-3 defenders per city, 3-4 defenders per metro (exact numbers depend on gov-type, build-priorities for that AICiv, era, and whether or not it has had any wars recently)
- double those numbers for capital-cities during the early game, possibly for every core-city by the late game (NB if you capture the original AI-capital, the 'jumped' capital will therefore have far fewer defenders for the next couple of turns -- although those units will all now gain the 'capital' defence-bonus)
- build 1 additional defender for each Wonder built in that city (if it has been allowed to accumulate enough shields to do so after building that Wonder!)
- use conscription (if available/possible) to keep the above numbers constant (e.g. after you've killed some of the defenders)
- not (AFAIK) do 'piecemeal' individual-unit upgrades, but will upgrade-all units of a specific type if it has sufficient gold available to do so in a single turn (this is why you can sometimes find a single AI-city defended by units from multiple eras, e.g. a city with a Spear, Pike, and Musket -- and why you should keep any prospective targets poor!)
- immediately and (semi-)permanently fortify units with only the 'defence' AI-strat flag (e.g. Enkidus, Spears, Hoplites, Pikes, SwMercs) in the city which built them -- AICivs will only move dedicated defenders out of cities
- to defend D=1 attack-stacks (e.g. Warriors, Archers, LBMs)
- to accompany D=0 units with the 'escort' flag (e.g. Settlers)
With respect to that last point, some 'defender' units
also have the 'attack' AI-strat flag (e.g. Rifles, Infs, MechInf -- possibly also Musketmen/ Musketeers, not sure). Such units will be permanently flagged as EITHER a defender OR an attacker on build-completion, and used ONLY in that role thereafter (i.e. EITHER fortified in the build-city, OR sent out to invade/ attack).
When such 'attacking-defender' units are damaged on the battlefield, they will then try to retreat to the nearest friendly city (which may not be the building-city) to heal. If that city is then attacked (either by another AI or the human), the higher D-value of a damaged attacking-defender may be enough to float it to the 'top-defender' position after all the 'official' defenders have been damaged, but before healthy attack-only units with lower D-values (e.g. Maces, Swords) are put up. This will also depend partly on HP, though -- e.g. a 4/4 HP D=3 unit will probably be preferred over a 1/4 HP D=6 unit