Taking just a few pills before and after sex can reduce the risk of HIV infection by 86%, a new study shows.

The pills contain two anti-AIDS medications. People in the study took two pills a few hours before a sexual encounter, a third the next day and a fourth the day after that, according to the study, published online Tuesday in The New England Journal of Medicine. A preliminary version of the study’s results was presented in February at the annual meeting of the Conference on Retroviruses and Opportunistic Infections.

Participants in the 400-person study took an average of 15 pills a month. Doctors followed them for an average of 9.3 months. Everyone in the study received counseling about reducing their HIV risk, along with free condoms and screening and treatment for sexually transmitted diseases, according to the report.

Doctors have known for several years that people can reduce their risk of HIV infection by taking a daily pill called Truvada. This prevention strategy, called pre-exposure prophylaxis, or PrEP, has been shown to reduce the risk of HIV transmission by 92% in studies of men who have sex with men. PrEP also reduced the risk of HIV infection by 70% in a study of intravenous drug users.

Until now, however, doctors didn’t know if taking PrEP “on demand” — geared around particular sexual encounters –worked as well as taking the pills everyday.

The new study adds to a growing list of HIV prevention strategies.

In the early years of the AIDS epidemic, public health officials focused on getting people to change their behavior: having sex only with a monogamous, HIV-negative partner; using condoms correctly and consistently; or abstaining from sex altogether.

In more recent years, medications have become a critical part of HIV prevention.

Research has shown that treating HIV can prevent its spread. People who control the disease with anti-AIDS medications can reduce the amount of virus in their blood to levels too low to be detectable with standard tests. These individuals have almost no risk of transmitting the virus to others, according to the Centers for Disease Control and Prevention.

Actor Charlie Sheen, who last month disclosed that he has HIV, said that his level of virus is undetectable, making it extremely unlikely that he would transmit the virus to his sexual partners. Sheen also said that his sexual partners have been under the care of his doctor. One former girlfriend, Amanda Bruce, has publicly said that she took medication to prevent becoming infected with HIV.

In Africa, studies have found that circumcision can reduce a man’s risk of becoming infected with HIV by 60%, according to the World Health Organization. Circumcision has been promoted as a relatively cost-effective strategy to combat HIV in developing countries, where a one-time surgery can be more accessible and affordable than daily medications.

In the USA, PrEP is a good option for people who don’t use condoms consistently, said Mark Milano, an HIV educator at ACRIA, a New York-based AIDS service organization.

Yet PrEP isn’t for everyone.

Although few people have serious side effects while taking PrEP, the pills can cause bone and kidney problems. In the new study, 18% of those taking on-demand PrEP developed kidney problems, compared to 10% of those given placebos, according to the study.

And unlike condoms — which help prevent pregnancy and a variety of sexually transmitted infections — PrEP doesn’t prevent diseases other than HIV. In the new report, 33% to 41% of participants developed a sexually transmitted infection during the study. PrEP doesn’t prevent pregnancy.

Milano notes that most people can’t predict when their next sexual encounter will be, making it difficult to take the first two PrEP pills in advance.

To take on-demand PrEP properly, “you have to know when you’re going to have sex,” said Milano, who cited studies showing that people aren’t very good at predicting the timing of their next sexual encounter. “Sex is often impulsive.”

Milano, who has HIV, said his partner of 13 years has chosen not to take PrEP, because both men are committed to using condoms consistently. Milano is now taking HIV medications that have made his virus levels undetectable, which further reduces his risk of transmitting the virus.

Doctors still have unanswered questions about on-demand PrEP.

The new study included only men who have sex with men or transgender women, who were born male but identify as female. Men in the study also had frequent sex.

The CDC notes that no one knows how well on-demand PrEP would work for women, injection drug users or gay or bisexual men who have sex less frequently than those in the study.

For these reasons, the CDC recommends taking PrEP daily, not on-demand.

In a study released last week, the CDC reported that very few of the 1.2 million Americans at high risk for HIV are taking any sort of PrEP regimen.

About 40,000 Americans are diagnosed with HIV every year — a rate that has fallen steadily for the past five years, according to the CDC.

In a study published in September, researchers reported on the use of PrEP in the “real world” — not just in tightly structured clinical trials — by describing the experiences of 657 people taking daily PrEP at Kaiser Permanente clinics.

Some doctors have been concerned that PrEP could give users a false sense of security, leading them to take more risks.

In the study, 74% of patients on daily PrEP said their number of sexual partners hadn’t changed. The number of sexual partners decreased in 15% of patients but increased in 11%, according to the study, published in Clinical Infectious Diseases. Condom use was unchanged in 56% of people in the study; decreased in 41%; and increased in 3%.

After six months on daily PrEP, 30% of users had been diagnosed with at least one sexually transmitted infection. There was no comparison group of patients in the study, so researchers don’t know how many people would have been diagnosed with a sexually transmitted infection without going on PrEP.