Outsourcing provides strategic flexibility and allows companies to focus on their primary objective, the research and development of new drugs.
Often the plant that is used to produce commercial compounds is the same as the one used to produce clinical compounds. If unanticipated high demand for a commercial product creates a drug shortage, then your extra capacity needs to be rediverted towards supporting your commercial molecules. As a result, the clinical campaigns get pushed around and this drastically slows down development. In other industries, a shortage of iPhones or Teslas isn’t going to kill anyone.
Contract manufacturers are also extremely good and efficient about the manufacture of APIs. Since they focus and specialize entirely on manufacturing, they often will have expertise, tools, and equipment that a big pharma might not have. For a medium sized company, they will often contract to a CMO since those organizations will also have increased capacity.
Big pharma tries to keep things in-house since it is cheaper and they have better control over the manufacturing process. However, if a plant is behind schedule or over capacity, it helps to have alternative manufacturing options.