The short answer is, you only need an agent if you want to publish traditionally—such as through one of the “Big 5” houses—like Penguin/Random House, Harper Collins, etc.
Obviously, if you want to self-publish, you don’t need an agent.
So the real question here is, do you want traditional publishing?
If so, then yes. You absolutely need an agent.
You can’t directly contact a big publishing house asking them to look at your manuscript. Even most of the smaller publishing houses won’t go for this. They only deal with agents.
That said, there are some smaller publishers these days who don’t require that.
Here’s a list of 30 traditional publishers who don’t require you to have an agent. (This list was published in 2019, and the publishing world changes fast these days. I haven’t researched every one of these 30 publishers to be sure they’re still in business, and still in good repute.)
… You’ll need to identify a handful of agents who have repped books similar to yours, and send them a polished, professional query letter.
Let’s say you query an agent who’s interested. She’ll probably ask to see your book proposal (if you’re a nonfiction author). You’ll want that proposal polished and ready to go, complete with a few sample chapters.
She’ll also want to see that you’ve got a healthy author platform—that basically means a decent following on social media (over one or multiple sites), and an impressive email list. There’s no hard-and-fast number about how many followers you need. Different publishers have different standards. Some say 1,000 is enough. Others say you should have 50,000 across all platforms. Some refuse to even consider you unless you get thousands of comments on your posts per day.
Your platform is a pretty big deal these days.
A publisher is more interested in selling your book than in your book itself. They want to know you’re marketable, and that you’re going to be a good partner in marketing yourself.
Think of a publisher like a business partner, not someone who is going to build your business for you. In this context, your book isn’t your baby. It’s your business proposal.
You need an agent if you want to pursue traditional publishing and go mainstream.
If you want to go that route, there are a few ducks to get in a row first.