Collaboration is essential for achieving complex goals, but brings with it a new challenge: when should team members specialise in distinct roles, and when should they be generalists who flexibly contribute to all parts of a task? Drawing on formalisations of parallelism in distributed computer systems, we identify a mathematical principle for division of labour based purely on the underlying structure of the problem. From this perspective, structural bottlenecks determine which strategy can be more efficient depending on how much work can proceed concurrently. When bottlenecks dominate, specialisation should be most efficient. Across seven large-scale experiments, our mathematical principle explained the behaviour of both human and AI dyads. Human teams closely matched the predicted effects of bottlenecks, offering new insight into how people spontaneously coordinate when faced with novel tasks. Multi-agent reinforcement learning (MARL) systems independently converged on similar solutions, demonstrating that bottlenecks determine specialisation even when agents are optimised with abundant training opportunities. Divergences between human and AI behaviour were themselves revealing: some reflected algorithmic biases in MARL training, while others showed that MARL agents could occasionally overcome bottlenecks through precise timing and coordination strategies unavailable to human teams. Together, these results unify theory, behaviour, and computation around a single principle governing when specialisation helps teams.