Many college graduates starting off in their careers face a dilemma. They want to buy a home and cover their daily living expenses but they also have to pay back their student loans and save for retirement.

Often, the temptation is for graduates to rush to pay back their student loan, says Jake Spiegel, a senior research analyst at HelloWallet, a site that offers financial planning tools for its members. With an average undergraduate debt of almost $29,000, according to the Institute for College Access and Success, that isn't surprising.

But trying to wipe out that debt as quickly as possible could be a financial mistake if it prevents you from socking away enough money for retirement. According to a survey just released this week by the American Institute of CPAs, half of Americans with student loans say they delayed contributions to retirement accounts. That’s a 22 percent jump from 2013, when 41 percent delayed saving for retirement.

“There’s often a perspective that getting rid of debt equals freedom, but that overlooks the price you’ll pay if you don’t focus on retirement at the same time,” says Spiegel.

Spiegel crunched the numbers to see how well a 25-year-old, who prioritizes paying back $20,000 in student loans, is able to save for retirement. In his study, he assumed this person would ramp up her student loan payments to pay off the loan in four years, instead of the standard 10 years. His calculations assume that she is paying an interest rate of 7 percent (while the current rate on Direct Federal unsubsidized loans is 4.3 percent, loans made between 2006 and 2013 had a 6.8 percent rate). Based on her $50,000 a year salary and living expenses, the rush to pay off her student loan means she would only be able to invest enough in her retirement plan to qualify for half of her company's 5 percent match.

Keeping all other expenses equal, Spiegel found that by age 65, she would have $125,000 less in retirement than if she had paid off her student loan over 10 years and contributed enough to get the full 5 percent company match, along with an annualized 5 percent return.

“When you're younger, the value of one dollar saved is far more important than using that dollar to pay down a debt,” says Spiegel. Even if you don’t have a matching contribution, Spiegel says saving more when you are younger is much more valuable than paying off your student loans faster, because you have all those years of compounding working for you.

Prioritize Saving for Retirement

If you've got a job that comes with a retirement plan, make sure you are contributing enough of your paycheck to at least get the maximum employer matching contribution. The benefit of doing so will outweigh any benefit you expect to get from repaying your student loan faster (and paying less in interest). 

If you aren’t yet eligible for a workplace plan, open a Roth Individual Retirement Account (IRA) and aim to save at least 5 percent, and preferably 10 percent, of your annual income, says Craig Lemoine, a certified financial planner and executive director of the Center for Financial Security at The American College. If you are single and have a modified adjustable gross income below $117,000 or if you’re married and filing a joint tax return and your income is below $184,000 you can directly save in a Roth IRA. This year, for those under the age of 50, the maximum annual contribution you can make is $5,500.

If you have a federal student loan, consider a job in public service. Over the years, lawmakers, colleges, and government employers have created loan relief programs for graduates who seek public service employment and often forgo a high-paying career. After 120 qualifying payments—about 10 years—the balance is discharged.

There are also a variety of repayment plans to choose from. You can use an online calculator to estimate what your payments might be on a 10-year plan. If you are having a hard time imagining how you will eat, pay the rent, make that loan payment and save for retirement, Lemoine suggests you look into one of the federal government’s income-based repayment plans. Such a plan limits your monthly payment to 10 percent to 15 percent of your disposable income. After 20 or 25 years, depending on the plan you choose, any remaining debt will be forgiven. 

“That gives you the most flexibility in the early years,” says Lemoine. “And if everything goes your way there’s no penalty for eventually prepaying if you want.” But don’t do so at the expense of saving for retirement.