States have significantly relaxed their drug laws in recent years — a sign that any ramping down of the war on drugs may happen at the state rather than federal level.

Forty states relaxed their drug laws between 2009 and 2013, a new Pew Research Center analysis found.

This is a major policy shift from the 1980s, when fears about a nationwide crack-cocaine epidemic led states to impose strict measures like mandatory minimum sentences. So why are states relaxing their approach now? And how, exactly, do these changes work?

Locking people up for drugs is really expensive

When states passed stricter drug laws, there was a spike in incarcerations for drug offenses. Relaxing those laws correlated with a recent decline in drug-related incarceration rates.

Keeping people in prison is pricey: Pew points to research from the Vera Institute of Justice that found the average inmate costs states more than $31,000 each year.

The high price imposes substantial costs for states, which incarcerated more than 1.3 million people in 2012. At $31,000 per inmate, that's more than $41 billion in annual costs for states (about 2.5 percent of overall state spending in fiscal 2012).

The problem is even more challenging in states with serious prison overcrowding problems. The Supreme Court singled out California in particular for its high prison population, forcing the state to make changes and reduce its amount of inmates. (In California, however, the problems went beyond high costs; the state declared an increased risk of violence and security breaches as a result of overcrowding.)

And the cost of so much imprisonment doesn't even count the billions more states spend on drug enforcement efforts, with no drop in illicit drug use to show for it in the past decade.

With states generally spending more than they collect in tax revenue, trimming these costs can play a key role in balancing budgets.

Public opinion increasingly favors relaxed drug laws

A recent Pew survey found two-thirds of Americans support government policies that treat, not prosecute, drug users. About 63 percent of Americans also backed moving away from mandatory minimum sentences for non-violent drug crimes — an increase of 16 percentage points since 2001.

In comparison, Pew notes 73 percent of Americans in 1990 favored the death penalty for major drug traffickers.

Pew found similar shifts in opinion across the board. When it came to legalizing marijuana, for example, 54 percent of Americans are now in support. That's a flip from just four years ago, when 52 percent said marijuana should not be legal.

Pew isn't the only organization to report a shift in public opinion on drugs, either. Gallup found 58 percent of Americans now support marijuana legalization, up from 31 percent in 2000.

It's a big swing in public opinion, and state legislators seem to be taking notice.

But how states relax their drug laws varies

States have taken a wide array of steps against the war on drugs in recent years, with a lot of variation from place to place.

The biggest steps occurred in Colorado and Washington, where voters decided to outright legalize marijuana. With 40,000 estimated to be in federal and state prisons due to marijuana and hundreds of thousands more arrested each year for possessing the drug, legalization could help reduce the costs of law enforcement and incarceration.

Outside Colorado and Washington, 15 states and the District of Columbia have passed legislation decriminalizing marijuana, which removed criminal penalties — importantly, imprisonment — for possessing, and sometimes selling, the drug. Maryland will join those 15 states and DC soon, as well.

Some states also relaxed other laws aimed at drug offenders. Ohio, for instance, decriminalized possession of marijuana-related paraphernalia and enacted prison sentencing reforms that will let nonviolent offenders avoid prison or get out of prison early.

All states also adopted or expanded some form of drug courts. Instead of condemning drug users to prison, these courts supervise rehabilitation attempts to avoid public health and safety problems that arise from drug abuse. Texas in particular increased the number of drug courts in the state since 2001 from seven to 71, according to the governor's office.

The federal government is now catching up to states with reduced sentences for low-level drug offenders and a softer approach toward marijuana. But for states these kinds of changes are already commonplace.