In this paper we develop a time consistent rational expectations model which analyzes the equilibrium loan contract between a borrowing country and a foreign bank. The loan contract specifies both the amount of the loan and the promised interest payments, and rationally reflects the investment decisions of the country and the possibilities of renegotiation and repudiation of the debt. An important feature of the model is that at the initial negotiation of the loan there is uncertainty about whether the country will renegotiate for partial forgiveness in the future, and whether it will eventually repudiate the debt, even having successfully renegotiated. Moreover, the probabilities of renegotiation and repudiation, and the amount of possible forgiveness are endogenously determined. In the model the repudiation decision is directly related to the underinvestment problem; the objective of the renegotiation is precisely to alleviate this problem. The model is used to analyze the effects of four variables on both the optimal contract and the country's welfare: the degree of penalties that a bank can impose on a defaulting country, the uncertainty of production, the productivity of investments and the riskless interest rate. The analysis has policy implications as well as testable predictions.