We provide a model of contagion where countries borrow or lend for consumption smoothing at the market interest rate or a lower IMF rate. Highly indebted countries hit by large negative shocks to output will default. The resulting reduction in loanable funds raises interest rates, increases the vulnerability of other indebted countries, and can generate further rounds of defaults. In this environment the IMF can limit default and internalize the externality generated by contagion through its lending with conditionality. We characterize the IMF's optimal lending decision in mitigating the loss in world consumption.