I was young when I started to build my fantasy worlds. So, when I built my big cities, and wondered how large they should be, I pulled out a map of San Francisco, of Oakland, of Los Angeles, etc... and built mine to be that large. Over time, there were discussions of the size of the cities and I came up with explanations.
Most of my major cities have existed for several thousand years. A common practice would be for these cities to expand in additional rings around the existing city by building a wall a distance outside the city and then filling it over time. Sometimes these were entire rings around the existing city, and sometimes they were bubbles built on the side. Sometimes the city would expand adjacent to a town that had been established outside the existing city and then absorb it. Some of these absorbed cities are agricultural lands, and the area absorbed begins as a large series of farms. As such, there are areas within my massive cities that are not densely packed to begin, althoygh over time those farms do get displaced.
Magic is a common tool in the construction of new locations. The evolution of how this works has changed over time, but in current lore, a low level wizard might be employed to provide unseen servants, floating disks, mold earth, shape water, etc... services. You might spend a thousand gold to send your smartest child to a place to learn magic (and work their way through schooling as the servant of a wizard for 5 years) and then put them to work on construction. They may be able to achieve more than 20 workers could in a single day by using magic that expends no resources such as rituals and cantrips.
If you have access to more powerful magics such as wall of stone, conjuration, divination, fabricate, etc... spells you can achieve far more (although only elven and dwarven spellcasters live long enough to rise to these levels without being touched by the Gods - and if you're touched by the Gods you are often too busy to be a crafstman full time). As such, these long lived races have master craftsman mages that spend the last couple hundred years of their lives building the major projects of the cities of the world.