We derive the optimal dynamic contract in a continuous-time principal-agent setting, and implement it with a capital structure (credit line, long-term debt, and equity) over which the agent controls the payout policy. While the project's volatility and liquidation cost have little impact on the firm's total debt capacity, they increase the use of credit versus debt. Leverage is nonstationary, and declines with past profitability. The firm may hold a compensating cash balance while borrowing (at a higher rate) through the credit line. Surprisingly, the usual conflicts between debt and equity (asset substitution, strategic default) need not arise.