Its origins date back to the modern firm where owners did not always manage the business. The owners were the principals and the managers were the agents on their behalf. The problem comes in when the interests of the owners are not always aligned with those of the agents. The principal-agent problem can be generalized to quite a number of situations. In outsourcing, agency costs are typically contrasted with transaction costs.
Transaction costs deal with the expenses associated with contracting out that function.
While internal performance of a particular function may initially appear cheaper than contracting externally because agency costs are lower than transaction costs, agency costs have a way of creeping up over time and are often difficult for a firm to measure and track. There is an entire literature on the principal-agent problem in the realm of Economics, depending on how entensively you want to research it.