Season 4 of “Silicon Valley,” which ended Sunday night, plays more like a tragedy than any of the show’s adventures so far. After three seasons of trying to combat magnate Gavin Belson (Matt Ross), the vicious head of the show’s Google-like Hooli, protagonist Richard Hendricks (Thomas Middleditch) spends most of Season 4 in a free fall of his own principles. If “Silicon Valley” started as a story where Richard thought he was going to defeat Gavin, it paused Sunday night as a story observing that Richard is becoming Gavin, one agonizing moral compromise at a time.

Meanwhile, one of the show’s most beloved characters, the big-haired and blustering Erlich Bachmann (T.J. Miller), was written off the show with an ending that is neither happy nor conclusive: He’s sentenced to a five-year opium addiction in Tibet, with a careless shrug, at the end of a season where he kept trying and failing to find his place in the tech scene.

“Silicon Valley” has always served its comedy with an edge of lacerating recrimination, so Richard’s arc isn’t exactly surprising. But Season 4 was more painful than funny, and the show struggled to mesh its plot — the arcs of character development in its ensemble — with the humor of the situation. Perhaps Richard has just become that much of a monster: “Hooli-Con,” the ninth episode, is a damning (and compelling) portrait of Richard’s burgeoning megalomania. But “Hooli-Con,” like the entire season, allows its characters to escape judgment by ending on a technicality: Richard’s made a series of selfish mistakes and nearly destroyed the reputations of his long-suffering colleagues, but because of the tech world’s extant corruption (corporate infighting and stupid smart-fridges), he not only gets away with it but is steadily rewarded for it.

This is, of course, a brilliant evisceration of the real-life Silicon Valley, a consequence-free sandbox for the very rich — a place that Erlich increasingly struggled to belong to. But it’s not exactly something to laugh at; it’s comedic in an unsettling way that lasts for a few days, like a bad hangover. Perhaps “Silicon Valley’s” goal is this darker, weirder tone. But even if it is, Season 4 is missing some of show’s strengths that were so clearly evident in Season 3. Where Season 4 relied on a lot of humor that was more conceptually comical — like the deus ex machina of smart-fridge networking, which is golden material that never reaches its full potential — Season 3 emphasized Silicon Valley’s total disconnect with the physical world, whether that was horses mating or private jets playing chess in the sky.

Season 4 continued to tell jokes like this, specifically around the new Hooli CEO Jack Barker (Stephen Tobolowsky), and those are some of the funniest moments the season has to offer. But Jack isn’t a member of the dysfunctional but lovable Pied Piper ensemble — in Season 4, he doesn’t even talk to a member of Pied Piper. “Silicon Valley” is still telling that kind of joke, but it’s not telling it about the group at the heart of the show. Increasingly, it’s hard to tell what joke, what punchline, is being told about the central figures of this show.

Without getting into the behind-the-scenes dynamics behind Erlich being written off the show, there’s something incredibly sad and poignant about Richard’s business partner — the insufferable man who has routinely bailed him out and believed in him — being exiled from Silicon Valley and “Silicon Valley” because he never quite fit there. Miller observed himself that Erlich’s story is kind of a tragedy, and Season 4 underscores that: Erlich is a lost, helpless soul, trying to cover his wounds with his characteristic bluster and failing to convince anyone, including himself. Dinesh (Kumail Nanjiani), in the last episode, introduces the idea that Erlich is cursed, and it’s hard to conclude otherwise: Every single thing he touches turns to ash (literally; it seems noncoincidental that his primary character trait is smoking some kind of drugs). The finale makes it feel like Erlich could step back into the show at any moment, which is unsettling, given that Miller has not only departed the show but also thoroughly burned his bridges. It’s like Erlich, and his failure to be a part of Pied Piper, will now haunt the show.

Perhaps this will be an impetus for flourishing creativity. As with so many sitcoms, every character in the core Pied Piper ensemble ends up being representative of something bigger. Guilfoyle (Martin Starr) is the purist, binary, throwback nerd of the tech world; Dinesh is its sexual, sensual id; Jared is its poorly treated but infinitely essential conscience. Erlich — earthy, un-self-aware, and usually stoned — was the impulsive gut instinct constantly at war with Richard’s brilliant, self-conscious, cerebral personality. For three seasons, the show was the story of conflict and codependence between these two very different characters, where Erlich’s style offered an essential counterbalancing quality to Richard’s narrative. More simply, Erlich is just very funny. The back half of Season 4 is scattered with moments where Miller’s sandals-and-socks shod character nabs the biggest laughs.

Maybe he was destined to be a tragic figure. If the story of the real-life Silicon Valley is writ large in these characters, Richard’s conniving brain has bullied his conscience into submission, harnessed his id to do his bidding, and exiled any sense of groundedness to the other end of the earth. Erlich is replaced — literally supplanted — in the show’s story by Haley Joel Osment’s similarly hirsute Keenan Feldspar — who if anything, is even more impulsive, shallow, and immature than Erlich was. The difference seems to be that while Erlich failed constantly, Keenan has never failed once.

Failure was a nice element of the fabric of “Silicon Valley.” It was what tethered the show — a window into a funhouse version of rapacious capitalism — to the world the rest of us live in. Of course, Pied Piper couldn’t fail forever. So the show sacrificed one character to a lifetime of failure — a failure so complete that no one even says goodbye to him. It’s interesting, and it’s poignant, and it may be an opportunity for the show to reframe brilliantly, as other shows have when faced with a cast member’s departure. But it’s not funny at all.