Honestly? As many as you can possibly build.
Build a few research labs, enough so it's not too slow, but it's not that much of a priority... Tech is all just a little bit extra damage here, a 5% bonus there. It's not that important. Twice as much tech vs. twice as many ships, the science nerd will lose every time.
Then balance your energy/food/mineral/consumer goods income. (That is, get it to zero, plus your needs for some civilian construction.)
Now get your amenities balanced (5-10 extra for some happiness).
Then put a foundry in literally every other building slot you have. Take an occasional break if you need a refinery.
There are two related reasons for this:
- First, alloys are the only thing you can never have too many of. Despite the economic overhaul this remains a core weakness of Stellaris. It has no competing priorities.
Think about it this way: For any other resource, what do you spend excess income on? Not that much. There's the odd edict or decision, but they're trivial. Mostly your empire is exactly the same whether you have +1,000 consumer goods/energy/food/etc. or +1 for the whole game. Everything above 0 is irrelevant and excess is just stockpiled in case of future deficits. But +1,000 alloys vs. +1 is the difference between winning and losing.
This has made min/maxing toward alloys a dominant strategy, because there's no downside to minimizing any other resource and all upside to maximizing alloys.
- Second, this keeps happening because ships are the only things that matter in Stellaris.
This is a competitive game that wants to pretend it's not, so none of the other systems (economic, diplomatic or technological) are designed to give you a meaningful advantage over other players. As a result, ships are the only thing you need and can use in unlimited quantities.
This is why there's no advantage to having more of any other resource. There's nothing you can spend those resources on, and nothing that would be useful even if you could. This is a war game (in reality, if not by design). The only relevant spending in Stellaris is on something that makes your empire stronger, and only ships make your empire stronger.
If you could spend other resources in ways that made your empire asymmetrically stronger against other empires (technology, economy, productive capacity, diplomacy, etc.), it would be different. Then you'd be giving something up by min/maxing to alloys. But there are no asymmetrical solutions or strategies in Stellaris. There's just the one: Get alloys, build ships. So... do that.