SAN FRANCISCO  The online classifieds company Craigslist said Thursday that it had reached an agreement with 40 state attorneys general and agreed to tame its notoriously unruly “erotic services” listings.

Prostitutes and sex-oriented businesses have long used that section of Craigslist to advertise their services.

Early this year, the attorney general of Connecticut, Richard Blumenthal, representing 40 states, sent a letter to Craigslist demanding that it purge the site of such material and better enforce its own rules against illegal activity, including prostitution. The two sides began a series of conversations about what Craigslist could do to prevent such ads.

“They identified ads that were crossing the line,” said Jim Buckmaster, chief executive of Craigslist. “We looked at those ads, we saw their point, and we resolved to see what we could do to get that stuff off the site.”