We develop an agency model of financial contracting. We derive long-term debt, a line of credit, and equity as optimal securities, capturing the debt coupon and maturity; the interest rate and limits on the credit line; inside versus outside equity; dividend policy; and capital structure dynamics. The optimal debt-equity ratio is history dependent, but debt and credit line terms are independent of the amount financed and, in some cases, the severity of the agency problem. In our model, the agent can divert cash flows; we also consider settings in which the agent undertakes hidden effort, or can control cash flow risk. (JEL G30, G32, G35, D82, D86, D92) We develop a model of long-term financial contracting and derive debt and equity as optimal securities. Our analysis captures the optimal coupon and maturity structure for long-term debt; the interest rate and credit available on a line of credit; debt versus (outside) equity financing; and dividend policy. The model has implications for how a firm's capital structure varies over time. The scenario we consider involves an agent who raises external capital to finance a business opportunity. Among investors' concerns in funding a business is that the agent might divert funds to himself, or consume other private benefits, at the expense of investors. Our analysis focuses on this agency problem.Specifically, in the model, a risk-neutral agent seeks funding from riskneutral investors. The funding will finance a business that requires an investment in assets and generates risky cash flows over the next T periods. The agent observes the realizations of these cash flows but investors do not. The agency problem is that the agent can underreport the cash flow, diverting the cash flow for his own private benefit. At any time during the life of the business, the business can be terminated. In the event of termination, the agent is left to pursue his best alternative and investors are free to make optimal use of the assets. The termination threat is the key to inducing the agent to share the cash flow with investors. A contractWe would like to thank Mark Garmaise, Denis Gromb, Bob McDonald, Maureen O'Hara, two reviewers, and seminar and conference participants for helpful comments.