This approach is much better than treating people in prostitution as criminals. But there is a debate about whether to legalize all of it. “Criminalization makes the most marginalized even more vulnerable to exploitation,” according to R.J. Thompson, managing director of the Sex Workers Project at the Urban Justice Center. “We should take it out of the shadows and treat it as work.” There is trafficking in the restaurant business as well, Mr. Thompson said, but no one argues we shouldn’t eat out.

He said that current law enforcement practices to fight sex trafficking end up making life harder for prostitutes in general. Conflating prostitution and sex trafficking “results in actual policies and legislation that impact workers in very negative ways, in the guise of anti-trafficking laws,” he said.

A 2012 study of the experiences of 116 countries found that legalizing prostitution — buying as well as selling sex — does make life healthier and safer for prostitutes, and encourages buyers to prefer legal prostitutes over illegal ones, who are more likely to be trafficked. But that same study also found that legalization increased human trafficking, because the demand for commercial sex increased greatly.

The New York Police Department started using its decoy chatbot in January 2018. The Cook County Sheriff’s Office in the Chicago area uses a childsafe.ai chatbot as well. Six other cities use it intermittently, as part of a national twice-yearly campaign to go after buyers of sex.

Freedom Signal’s developer, Seattle Against Slavery, is still mostly volunteer; the tech team, for example, has one paid employee and a lot of volunteers with day jobs at Amazon or Microsoft. Freedom Signal asks partner cities to cover its costs, and many can, using federal anti-trafficking grants. But if a city can’t pay, Mr. Beiser will provide it free. He said that 13 cities are using it, including the Cook County and Los Angeles County sheriffs, and Boston’s police.

In its early days, Freedom Signal placed ads that popped up when someone typed in the usual terms of a search for sex online. Some ads warned of the harm that buying sex can cause. Others offered men help in quitting a behavior harmful to them and their families.

Then Mr. Beiser realized that online ads could make it easy to reach people in prostitution — after all, they had published their phone numbers. Freedom Signal began sending mass texts to the numbers. In the text, a trafficking survivor introduces herself and says: “I was in the life, and now I help people find housing and counseling. Are you interested in talking more?”