We study the optimal design of clearing systems, focusing on counterparty risk insurance and prevention. We study whether decentralized clearing improves on no clearing, and whether centralized clearing generates further improvement. We analyze how counterparty risk should be allocated, whether traders should be fully insured against that risk, and how moral-hazard a¤ects the optimal allocation of risk. The main advantage of centralized clearing is the mutualization of counterparty risk. We show, however, that the improved risk-sharing brought about by centralized clearing can induce greater risk-taking, even in the …rst-best. Furthermore, while mutualization is useful to share idiosyncratic risk, it cannot provide insurance against aggregate risk. When the latter is signi…cant, it is necessary that protection buyers exert e¤ort to nd robust counterparties. With moral hazard, this requires that they retain some exposure to counterparty risk.