There's an AOPA Air Safety Institute video, "Ask ATC: Am I Cleared for Class B?":
In this and other "Ask ATC" videos, they point out that if you're in VFR flight-following, ATC will not clear you for Class B without your requesting it, even if your current flight path will obviously enter that airspace.
If you are getting ATC VFR flight-following, you should make the request in the course of normal communications, but no later than the point where you can easily divert if clearance is denied.
If you are not talking to ATC, you must first get their attention ("November 12345, request"), then on acknowledgement (when they'll probably abbreviate your call sign, which means you can, too), give your aircraft make/model (which tells ATC your capabilities), relative position (which tells them where to look for your radar return), altitude(s) (level/climbing/descending, which they won't get directly from your transponder), and intentions (your request). This, too, you should do well before the point when diverting around the airspace becomes inconvenient.
(If you're squawking 1200, they'll give you a squawk at the same time, probably an altimeter setting, and possibly altitudes and vectors through the Bravo, so be ready with your lapboard or tablet. Don't hesitate to ask for a re-read, or even to ask them to speak more slowly—in another "Ask ATC" video, they say that controllers often accelerate their speaking pace over the course of a shift without realizing it, they know that VFR pilots aren't as practiced in ATC comms, and they'd much rather you ask for them to repeat more slowly than for you to make a guess.)
So long as you're already handed-off to (if flight-following) or have made contact with the ATC with jurisdiction over the Class B, you can theoretically do it as early as you like.
So there's a bit of a trade-off: later makes it harder to divert, but easier for ATC to have awareness of you. Earlier makes it easier to divert, but ATC may not be ready to clear you if you're far enough out that the traffic situation may change significantly before you enter the airspace.
(Since you're probably significantly slower than most of the aircraft in the Class B, that makes it even more likely that the situation will change.)