> a firm maximizes profit by producing that quantity of output where marginal revenue equals marginal costs.
This is a pretty fundamental result in microeconomics. The profit maximization section of the wiki article explains it quite well: https://en.m.wikipedia.org/wiki/Marginal_product_of_labor
The usual disclaimers about underlying assumptions apply of course.
Hiring an engineer has a significant opportunity cost.
How to apply it wisely is a different, much wider topic.
You're right that in the short term you aim to hire where the return is highest, but in the long term that means they're right that you'd want to keep hiring as long as you get a positive return.
You want to spend it on whatever has the highest return, which will frequently be on an expense other than hiring, and when it is on hiring, it will frequently be on hiring roles other than engineers.
There isn't really an opportunity cost to hiring more people when money is no object. More likely to be the opposite.
Why do you think they increased their headcount so much in the last few years? Simply because they could.
The fact there is finite budget and headcount doesn't necessarily mean anything. If you're limited by the number of quality employees you can recruit and onboard, budget and headcount are effectively meaningless because they're not limited by money.
The question really is, how can you increase shareholder value? If the answer requires engineers then they are hired. They are paid less then the value they add. When that stops being true people like Elon Musk can increase value by reducing headcount.
That’s not the part that gives insight to the question “how can it take X engineers to run service Y?” The insight is that employees can add more value than they cost even if they aren’t working on the most visible core product that the company is known for.