This paper analyzes a model where investors use a credit rating to decide whether to finance a firm. The rating quality depends on the unobservable effort exerted by a credit rating agency (CRA). We analyze optimal compensation schemes for the CRA that differ depending on whether a social planner, the firm, or investors order the rating. We find that rating errors are larger when the firm orders it than when investors do. However, investors ask for ratings inefficiently often. Which arrangement leads to a higher social surplus depends on the agents' prior beliefs about the project quality. We also show that competition among CRAs causes them to reduce their fees, put in less effort, and thus leads to less accurate ratings. Rating quality also tends to be lower for new securities. Finally, we and that optimal contracts that provide incentives for both initial ratings and their subsequent revisions can lead the CRA to be slow to acknowledge mistakes.