When Dick Costolo attended the University of Michigan, in the nineteen-eighties, his major was computer science, but he was surprised to find that he also had a knack for improv comedy. After graduation, he moved to Chicago and took classes at the Second City Theatre. Unlike some of his peers there—Steve Carell, Tina Fey, Adam McKay—Costolo was not asked to join the theatre’s house company, and his comedy career dried up. He fell back on his skills as a coder and founded a series of tech startups, one of which was eventually acquired by Google, for a hundred million dollars. In 2010, he became the C.E.O. of Twitter, earning about ten million dollars in his first year. At a charity event, he ran into Steve Carell, and they reminisced about their days as bohemian improvisers. “I’m sorry it didn’t work out for you,” Carell joked.

In June of 2015, with Twitter’s stock price languishing, Costolo announced that he would leave the company. (According to the tech press, the board of directors had forced him out; Costolo maintains that leaving was his idea.) Three days later, HBO aired the second-season finale of its half-hour satire “Silicon Valley.” The season ended on a cliffhanger: the central character, the founder and C.E.O. of a tech company, was fired by his board. Costolo, a fan of the show, found the situation uncannily familiar. “I could relate to every person in that situation—the founder who’s leaving, the C.E.O. who’s coming in, the employees who are watching it happen,” he said.

Around that time, Costolo had breakfast in San Francisco with Kara Swisher, a tech reporter and power broker who has been called “Silicon Valley’s most feared and well-liked journalist.” Conversation turned to “Silicon Valley,” the show. “People in the Valley—at least, the people I know—talk about the show all the time,” Costolo told me. “Most of them love it, oddly. I think there are a lot of people telling themselves, with varying levels of accuracy, ‘They’re satirizing those annoying tech people—not me.’” Swisher, who knows everyone, was in frequent contact with the showrunners, Mike Judge and Alec Berg. “I’ll introduce you,” she told Costolo.

The next month, Costolo had lunch with Judge and Berg in Los Angeles. They told him that they had written themselves into a corner. Their show was about an entrepreneur striving to build a company; having separated the entrepreneur from the company, they weren’t sure how to proceed. For a show that devotes a good amount of time to slapstick and gross-out sight gags, “Silicon Valley” is deceptively well-researched, and Judge and Berg had decided that the best way out of their bind was to hire a consultant who could give them more information. To their surprise, Costolo expressed interest. “We just need someone who knows how these companies work, not someone who actually ran one of them,” Berg said. Despite being overqualified, Costolo got the job.

Dick Costolo, the former C.E.O. of Twitter, was one of more than two hundred consultants on the show. *Photograph by Scott Olson / Getty * Photograph by Scott Olson / Getty

“Silicon Valley” is mostly filmed on multiple sets, inside a concrete Sony lot in Los Angeles—not in Silicon Valley, but in the same time zone. Every Monday morning for three and a half months, Costolo flew from San Francisco to L.A., took an Uber to Culver City, dropped his overnight bag at a nearby hotel, and spent Monday and Tuesday in the writer’s room. Berg, Judge, and ten writers peppered him with questions, both narrow and existential. Where would the most powerful person in a boardroom sit? What would motivate an entrepreneur like Richard, and what would he find most demoralizing? “I would tell them a detail about something I’d observed or someone I’d met, and they would get this sparkle in their eye and go, ‘That really happens?’” Costolo said.

Over time, Costolo grew comfortable enough to pitch jokes of his own. “They were generous about letting me down gently,” he told me. “It was interesting to go from the C.E.O. to the least experienced guy in the room.” Among the tech-journalism books that everyone on staff had read was “Hatching Twitter,” Nick Bilton’s history of the company. “Once, they were debating what should happen next in a story arc,” Costolo told me. “Mike asked the room, ‘Didn’t they face a problem like this in the Twitter book? What did they decide?’ Someone had to point out, ‘Mike, one of the people from that book is in the room. Let’s just ask him what happened.’”

“Silicon Valley,” now in its third season, is one of the funniest shows on television; it is also the first ambitious satire of any form to shed much light on the current socio-cultural moment in Northern California. The show derives its energy from two semi-contradictory attitudes: contempt for grandiose tech oligarchs and sympathy for the entrepreneurs struggling to unseat them. In the pilot episode, Richard Hendricks, a shy but brilliant engineer, designs a compression algorithm—an ingenious way to make big files smaller. He later turns this innovation into a company, which he insists on calling Pied Piper. (Richard: “It’s a classic fairy tale.” Employee: “It’s about a predatory flautist who murders children in a cave.”) As his company grows, Richard becomes a nerd David beset by Goliaths: duplicitous board members, corporations trying to steal his intellectual property. Can he succeed without compromising his values? The deep irony of Richard’s situation—that his ultimate goal, presumably, is to become a Goliath himself—either has not yet come up in the writer’s room or is being tabled for later.

Richard Hendricks, played by Thomas Middleditch, is a genius coder who struggles to make an algorithm into a business. Photograph by John P. Fleenor / HBO Photograph by John P. Fleenor / HBO

“Real startups go through all the shit you see on the show, as well as even crazier shit,” Roger McNamee, a venerable venture capitalist and a consultant to the show, told me. “If anything, the writers might have to leave out true things in order to seem more realistic.” Both Judge and Berg have an eye for authenticity. In Judge’s movie “Office Space,” from 1999, he enlivened his subject—white-collar drudgery—with details he had experienced or observed: a boss’s onerous attention to the formatting of T.P.S. reports, a chain restaurant that forces its servers to wear at least fifteen pieces of “flair.” Similarly, many of the shows that Berg has written for, notably “Seinfeld,” harvested story lines from real life. “On ‘Seinfeld,’ the same thing happened again and again,” Berg told me. “Someone would pitch ten ideas. The first nine would be wacky, silly things, and the tenth would be genuinely funny and interesting. You’d go, ‘That tenth thing—where’d that one come from?’ and the person would say, ‘That one actually happened to a friend of mine.’”

When you’re writing a show about nothing, or a movie about cubicle culture, it’s easy to collect realistic details. But if you want to know how a non-compete clause would be structured, or what kind of car a typical brogrammer would drive, or whether Richard’s firing would trigger an afternoon of malaise or a personal crisis, then you need to do your homework. TV writers have long consulted experts—a doctor to demonstrate how to hold a defibrillator, a military officer to make sure the uniforms are the right color. In the past, these consultants were often akin to fact-checkers, brought in near the end of the writing process to make sure that nothing looked glaringly wrong. These days, TV is taken more seriously, and everyone’s a critic with access to Twitter and Wikipedia. “You can’t fool audiences with unrealistic schlock anymore,” Jay Carson told me. Carson was the press secretary for Hillary Clinton’s Presidential campaign in 2008; he then served as the Chief Deputy Mayor of Los Angeles. In 2011, his friend Beau Willimon hired him as a political consultant on “House of Cards.” “I helped us pass a smell test, both with D.C. insiders and the general audience,” he said. “Even during the five years I was there, the audience got more sophisticated every season.”