Federal agents trying to deport illegal-immigrant criminals are hitting a new roadblock in New York City — crafty defense lawyers who will put their clients in jail to keep them in the country, The Post has learned.

A Legal Aid lawyer took the extreme measure in The Bronx Thursday, when she claimed an Immigration and Customs Enforcement agent was waiting for her client and begged a judge to lock up the suspect.

The rookie judge — a Mayor de Blasio appointee — complied, setting $3,000 bail even though the defendant had been free for the six months since his arrest.

“I can’t believe this — every day Legal Aid is asking for no bail” for their clients, one insider told The Post.

“And now they’re asking for bail because even going to Rikers is better than being deported.”

Lawyers are also advising clients not to post even the lowest of bails so that they’ll go to Rikers Island rather than into the waiting arms of ICE agents, sources said.

The city last year evicted ICE from its offices at the jail as part of de Blasio’s “Sanctuary City” reforms — and no longer turn over many criminals who have detainer requests.

Hizzoner’s stance has led defense lawyers to ask judges to sentence their clients to just under a year in jail, so that they’ll remain in city custody rather than go into the state prison system.

“It’s a shame, to go that far to protect someone,” one federal source complained.

“It’s like a new cat-and-mouse game, when you’re an immigration officer looking for someone. It’s a new tactic to work against the federal government, when all these officers are doing is enforcing the laws set by Congress. It’s a shame.”

In Thursday’s case, illegal immigrant Luis Mercado, 29, arrived for his scheduled date at Bronx Criminal Court only to find ICE agents waiting for him.

Mercado, of The Bronx’s Belmont neighborhood, had been arrested three times for what he himself termed “fishing” envelopes out of nearby mailboxes using rodent glue traps and string, according to a criminal complaint.

“I was fishing in the mailbox,” Mercado told cops when he was arrested Oct. 9, court papers say.

“The money order I had on me came from that mailbox,” he allegedly admitted that night.

Thursday was his eighth court appearance since then.

But this time, his lawyer said ICE was waiting. A source said he was wanted for failing to appear in an immigration court in Texas, according to a source.

Mercado’s Legal Aid lawyer, Elana Silberman, asked Judge Margaret Martin to jail him.

“I understand the department of immigration, ICE, is here in the hallway to take him into custody,” Silberman said.

“I am asking that my client have bail set on him,” she said, according to a transcript.

“That you set between $500 and $2,000 bail on this case . . . If no bail is set, he has no ability to come back.”

The judge, appointed by de Blasio in January after 21 years as a Legal Aid lawyer, went one better and set bail at $3,000.

“Based on the facts and circumstances, the judge made a ruling,” a spokesman for the judge told The Post.

“And that’s her discretion.”

Additional reporting by Laura Italiano