For most shows, the sweet spot will be somewhere from season 2-5. Often the first season can feel odd, because they're still working out what works, who the characters are and what will go down well with the audience. Then they'll get things all tightened up and the tone and style will feel much more like "that show", characters will feel much more familiar and you'll start getting really good scenes and episodes.
Then after about 4-5 seasons, it really starts to come down to whether they can keep thinking of interesting scenarios and funny jokes, or whether new characters they add to keep things fresh really work. This may also be the time where some senior team quit because they've been making the same show for like 5+ years and they need a change.
Most shows can't. The characters start to become caricatures of themselves (as new writers come on board who are fans of the existing show and write them like fanfic versions of themselves, or the writers get lazy and start leaning more and more into their strongest traits for cheap drama or laughs), the stories become more and more absurd, or more and more stale... because if the characters are stuck in exactly the same situation for five years, it's boring and kind of depressing... and if they're not and things get shaken up, it's a coin toss as to whether it can work (ahh, the classic "and now the high school characters all go to (the same) college!" test).
So yeah, the sweet spot is generally 2-5.