The federal government is preparing to forgive billions of dollars in student loans to doctors and other white-collar Americans with expensive educations, under a law designed to help modestly paid workers in public service.

At issue is a 2007 program that forgives any federal student debt after a borrower has made a decade of payments, which are pegged to a low percentage of salary, while working for government or nonprofit entities. It was designed to encourage young Americans to pursue traditionally hard-to-fill jobs: public defenders, social workers, teachers and modestly paid doctors in underserved areas.

But the program is encompassing far more workers than envisioned, many of them well-paid. Thousands of workers with pricey graduate degrees are on track to discharge five- and six-figure debts on their way to typically lucrative careers, according to enrollment figures and financial advisers.

The biggest beneficiaries will be medical-school students, who owe on average $180,000 upon graduation and are increasingly working for nonprofit hospitals and gaining eligibility for the program. Financial advisers estimate that many will have 80% or more of their original balances forgiven under what some are calling a “doctors’ loophole” in the program.

Meanwhile, critics and experts say the program does little to help the millions of Americans struggling the most with student debt, such as college dropouts owing far smaller sums. College graduates who don’t go on to grad school will see limited benefits because they start out owing a median of about $30,000—a sum often paid off within 10 years.