When Sarah Silverman was in her teens, she was taking over a dozen Xanax tablets a day.

“I was put on Xanax at 13,” she says in “Laughing Matters,” a new documentary that digs deep into the journeys of 12 comedians as they juggle telling jokes and their own struggles with anxiety, depression and other mental health issues.

“They just upped the dose … until I was taking four Xanax four times a day.”

The comedian, who has been open about her battle with depression, reveals that things took a dark turn several times in her life, one of which involved suicide.

“The psychiatrist who originally put me on it hung himself,” she says. “I mean, I can’t just skate by that — it’s crazy.”

Behind the scenes, it isn’t always butterflies and rainbows for the funniest among us. “Laughing Matters” offers unique insight from stars like Silverman, Neal Brennan, Wayne Brady, Chris Gethard and others about their issues with fame and mental illness.

Released on Mental Health Awareness Day, the documentary presented by Funny or Die is the brainchild of digital media company SoulPancake, which was founded by “The Office” star Rainn Wilson.

“It’s hard to find a comedian or comic actor or improv actor that hasn’t struggled with these issues at some point in time,” Wilson tells The Post. “They have often dealt with anxiety, depression and even suicidal ideation.”

Wilson says his own anxiety stems from being abandoned by his mother in Seattle when he was just 2 years old, which is why he calls himself “off” and someone who “doesn’t really fit in.”

“I had a lot of family issues growing up — seemingly normal-looking family from the outside but very dysfunctional and extremely unhappy,” Wilson says. “I felt incredibly isolated, alienated like I would never fit in.”

When he learned he was able to make people laugh, he says it became the “catalyst” to seek a career in comedy, something he believes other entertainers relate to.

“Oftentimes, people that are funny experience some great trauma as a child or feel tremendously alienated or suffer some kind of depression,” he says, “and this kind of like almost forces them to find comfort in humor.”

“One hundred percent of comedians become comedians because somewhere in their childhood, they needed to be funny in order to survive,” Silverman says in the documentary. “I became a comedian because I needed to be funny to be liked.”

Comedians have long struggled with mental illness, the documentary points out, from the 1997 drug overdose death of Chris Farley — who suffered from depression — to Robin Williams’ 2014 suicide, which shocked the nation.

‘This is an epidemic. It’s happening under our noses.’ - Rainn Wilson

“I hear about people all the time in the comedy world that are really struggling with this issue,” Wilson says. “Fame just throws kerosene on that. You create a false front and a mask and you are almost like a performing monkey and you’re expected to be funny. It can be very overwhelming.”

In the film, Silverman says it’s not just comedians who are at risk. “I think a lot of people find humor in the darkest places,” she says. “Humor is how we all survive.”

That’s backed up by the National Alliance on Mental Illness (NAMI), which reports that one in five people will experience some sort of mental illness each year.

“The increase I’ve seen in anxiety, depression and suicidal ideation has skyrocketed,” Wilson says. “This is an epidemic. It’s happening under our noses.”

The country’s suicide rate has also increased by 31 percent since 2001, NAMI reports, and it’s now the second-leading cause of death among people ages 10 to 34.

“I had a lot of suicidal thoughts,” reveals Chris Gethard, who says in the documentary he spent two years “physicalizing” anxiety dreams in his sleep by clenching muscles or punching.

“I would start my days waking up in physical pain because of the level of anxiety I was feeling,” he adds.

While some might think laughter is the best medicine, Gethard doesn’t believe being a comedian can be a substitute for therapy because it’s “not a safe space to go heal.”

“Comedy is not going to save you,” he says. “And if you are thinking about doing comedy as a substitute for therapy, it doesn’t work. I tried.”

Silverman credits success in her struggles to Klonopin — a drug often used to treat seizures and anxiety — and fellow comedian friend Mark Cohen. When she was hired for “Saturday Night Live” in 1993 and dealing with depression head-on, she says she nearly quit the show — until Cohen recommended a psychiatrist.

“These years of torture and shame kind of became my superpower,” she says. “There’s nothing more important to me than being funny — except being well.”

Wilson acknowledges that therapy and medication are the best routes to treat issues, but he hopes opening discussion about struggles with mental health will connect people with similar feelings to their “heroes who are really funny.”

“When you’re suffering from this kind of mental illness, it almost tricks you into thinking that you’re by yourself on a desert island,” he says. “Hopefully this documentary offers connection between these funny people and people who might be suffering in some way.”

“There’s a lot of ‘I love you’ going on in the standup community,” says Silverman in the documentary. “I think it’s kind of us going, ‘Don’t die! Don’t die!’”