Ask a junior network engineer: "How does BGP pick the best path?" They say: "Shortest AS_PATH wins." That is a half-truth. It is step 4 of an 11-step algorithm. Step 2 runs first.
Step 2 is LOCAL_PREF. Highest LOCAL_PREF wins. Always. Regardless of AS_PATH length.
The best-path order
1. Highest Weight (Cisco-only, local to router)
2. Highest LOCAL_PREF ← this one
3. Locally originated
4. Shortest AS_PATH ← the one everyone quotes
5. Lowest ORIGIN (IGP > EGP > Incomplete)
6. Lowest MED (same neighbor AS)
7. eBGP over iBGP
8. Lowest IGP cost to next-hop
9. Oldest route
10. Lowest router-id
11. Lowest neighbor IP
If a route-map sets LOCAL_PREF=200 on the 5-hop path and the 3-hop path has the default LOCAL_PREF=100, the 5-hop path wins. Every time. The algorithm never reaches step 4.
Why this matters in production
LOCAL_PREF is how operators express policy. "Prefer the customer path over the peer path." "Prefer the cheap transit over the expensive one." It runs before path length because policy is intentional and path length is incidental.
Try it yourself
Open Lab 04: Why The Long Path? in the RouterBaba simulator. Two paths to Toronto. The shorter one should win. It does not. Find the route-map that sets LOCAL_PREF and remove it.