Five BGP tools in one: parse and count AS path hops from a raw path string, test Cisco IOS and Junos AS path regex patterns live, generate AS path prepend route-map commands, compare two BGP paths through the best path selection algorithm step by step, and detect loops and prepends in any AS_PATH attribute.
✓BGP path selection per RFC 4271 • Cisco IOS & Junos syntax verified — April 2026
Paste a raw AS_PATH string — space-separated ASNs
e.g. from "show ip bgp" output. Supports 2-byte, 4-byte, and dot-notation ASNs. Use {} for AS_SET, () for AS_CONFED.
Enter sample AS paths to test against your regex. One path per line.
Enter at least one AS path to test.
Generate AS path prepend commands for your router
The ASN you want to prepend
Enter your ASN.
1-3x is typical; >4x is rarely needed
Leave blank for a default name
Enter attributes for two BGP paths — see which wins and why
Path A
Path B
Detect loops, prepends, and anomalies in an AS path
Paste any AS_PATH string. The tool identifies loops (your own AS), prepended ASNs, and suspicious patterns.
Enter an AS path to analyze.
BGP drops routes where your own AS appears in the path (loop prevention). Enter 0 to skip.
AS Path Length
0
⚠️ Disclaimer: AS path regex uses a JavaScript approximation of Cisco/Junos BGP regex. The BGP underscore _ meta-character is translated to match space, start, end, and comma boundaries. Test all patterns on real hardware before deploying in production. Best path comparison implements the standard Cisco IOS algorithm — Junos priority order may differ slightly.
Sources & Methodology
✓BGP AS path counting rules per RFC 4271 (BGP-4). Best path selection order per Cisco IOS documentation and RFC 4271 Section 9.1. Regex metacharacter behavior per Cisco IOS BGP regular expression reference and Juniper TechLibrary. All external sources cited with nofollow links.
The definitive BGP standard. AS_PATH attribute counting rules in Section 5.1.2: AS_SEQUENCE each element counts as 1; AS_SET counts as 1 regardless of members; AS_CONFED_SEQUENCE and AS_CONFED_SET do not count toward external path length. Best path decision process in Section 9.1.
Official Cisco documentation of the 13-step BGP best path algorithm with IOS command examples. Source for the best path comparison tool's decision logic and Weight/LOCAL_PREF/AS path length priority order.
Junos BGP path selection documentation and AS path prepend syntax reference. Junos uses Local Preference, AS path length, and Origin as the primary comparison criteria (no Weight attribute equivalent — Junos uses Local Preference and community-based routing policies instead).
AS path length counting: Each ASN in AS_SEQUENCE = 1. AS_SET = 1 total regardless of members. AS_CONFED_SEQUENCE and AS_CONFED_SET = 0 (not counted outside confederation per RFC 4271).
Regex: BGP underscore _ is translated to JS regex /(^|[ ,])ASN([ ,]|$)/ boundary matching.
Best path: Implements Cisco IOS 13-step algorithm. Junos differs in that it has no Weight attribute and uses slightly different tiebreaker order.
Prepend commands: Generated per Cisco IOS route-map syntax and Junos policy-statement syntax based on the input ASN and prepend count.
Last reviewed: April 2026
BGP AS Path Explained — Length, Counting Rules, and Why It Matters
The AS_PATH attribute is BGP’s core loop prevention and traffic engineering mechanism. Every time a BGP route crosses an autonomous system boundary, that AS’s number gets prepended to the front of the AS_PATH. By the time a route reaches its destination, the path contains every AS it traversed — in order. BGP uses this path length (step 4 of 13 in the best path algorithm) to prefer shorter routes over longer ones, all else being equal.
What trips up most engineers is the counting rules. They sound simple until you hit edge cases.
AS Path Length = count(AS_SEQUENCE) + 1 per AS_SET + 0 for AS_CONFED entries
AS Path Counting Rules — The Edge Cases That Catch Engineers Off Guard
Most people know that each ASN in the sequence counts as one hop. The edge cases are where it gets interesting — and where incorrect assumptions lead to misconfigured filters.
AS Path Type
Notation
Counts As
Notes
AS_SEQUENCE entry
65001
1 per ASN
Standard path entry. Each unique ASN counts once.
AS path prepend
65001 65001 65001
1 per entry
Each repeated ASN still counts. 3 prepends = path length +3.
AS_SET
{65002 65003 65004}
1 total
Created during aggregation. Entire set = 1 regardless of members.
AS_CONFED_SEQUENCE
(65010 65011)
0 externally
Only seen within a BGP confederation. Not counted outside.
AS_CONFED_SET
[65010 65011]
0 externally
Confederation aggregate. Not counted in external path length.
4-byte ASN
131072 or 2.0
1 per ASN
Same counting as 2-byte. Dot notation and plain are equivalent.
BGP Best Path Selection — Where AS Path Fits in the Algorithm
A common misconception is that AS path length is the first thing BGP checks. It’s actually step 4 — which means Weight, LOCAL_PREF, and locally originated routes are all evaluated first. Understanding this priority order is critical for traffic engineering. If you’re prepending to influence a remote router but that router has a LOCAL_PREF set on one of your paths, your prepending does nothing — LOCAL_PREF wins every time.
Step
Attribute
Prefer
Scope
1
Weight
Highest wins
Local to router (Cisco-only)
2
LOCAL_PREF
Highest wins
Within one AS (iBGP)
3
Locally originated
Local preferred
network/redistribute/aggregate
4
AS_PATH length
Shortest wins
← Prepending affects this step
5
Origin code
IGP < EGP < ?
IGP most preferred
6
MED
Lowest wins
Between same-AS neighbors
7
eBGP vs iBGP
eBGP preferred
External beats internal
8
IGP metric to NEXT_HOP
Lowest wins
Interior path cost
9-13
Tiebreakers
Various
Router ID, cluster list, neighbor IP
AS Path Regex — The BGP Filter Language Network Engineers Actually Use
AS path regular expressions are how network operators write routing policy. You use them in ip as-path access-list on Cisco or policy-statement with as-path terms on Juniper. The syntax looks like standard regex but with one critical difference: the underscore _ is a BGP-specific metacharacter that matches a space, comma, string start, or string end. It’s the most important character in BGP regex.
Pattern
Matches
Use Case
^$
Empty path only
Locally originated routes
_65001$
Routes originating from AS 65001
Filter by origin AS
^65001_
Routes received from AS 65001 peer
Filter by direct neighbor
_65001_
Any path transiting AS 65001
Block traffic via specific AS
^65001$
Exactly and only AS 65001
Single-AS origin exact match
.*
Any path (including empty)
Permit all in filter-list
^[0-9]+$
Single-hop paths only
Direct customer routes
^(65001|65002)_
Routes from either AS
Multiple peer filter
💡 Pro tip — always test regex before deploying: Use show ip bgp regexp [pattern] in Cisco IOS to see exactly which routes match your pattern before applying it to a neighbor. A misconfigured AS path filter can drop all routes from an upstream or accidentally permit routes you intended to block. The Regex Tester tab in this calculator lets you test patterns instantly before even touching the router. On Junos, use show route aspath-regex [pattern] for the same pre-deployment verification.
AS Path Prepending — Traffic Engineering for Multihomed Networks
AS path prepending is the primary tool for influencing inbound traffic when you’re multihomed — connected to two or more upstream providers. Your outbound traffic you control with LOCAL_PREF. Your inbound traffic is controlled by what remote networks see in their BGP tables — and you influence that by making one of your advertised paths look longer through prepending.
Here’s the practical scenario: you have two upstreams, ISP-A and ISP-B. You want 80% of traffic via ISP-A and 20% via ISP-B as a backup. You advertise your prefix normally to ISP-A, and advertise the same prefix to ISP-B with three prepends of your own AS. Remote networks comparing paths via ISP-A (length 2) vs ISP-B (length 5) will prefer ISP-A. Prepending doesn’t guarantee traffic distribution — remote networks may have LOCAL_PREF overrides — but it’s the standard mechanism for this traffic engineering goal.
Frequently Asked Questions
Count each ASN in the AS_SEQUENCE (each = 1), add 1 for any AS_SET regardless of members, add 0 for any AS_CONFED entries. Examples: "65001 65002 65003" = length 3. "65001 65001 65002" = length 3 (prepend counts). "{65002 65003} 65001" = length 2 (set = 1). Use the AS Path Length tab above to parse any path string instantly. This is step 4 of the BGP best path algorithm — shorter paths are preferred, but Weight and LOCAL_PREF are evaluated first.
AS path prepending artificially increases the AS path length by repeating your own ASN multiple times at the start of the AS_PATH. Cisco IOS: route-map PREPEND permit 10 / set as-path prepend 65001 65001 65001. Junos: as-path-prepend "65001 65001 65001". Used for inbound traffic engineering when multihomed — makes one upstream path look longer, steering traffic toward the preferred upstream. 2-3 prepends is typical. More than 4 is unusual and some providers filter excessively long paths. Use the Prepend Generator tab to create the exact commands for your router.
Cisco IOS evaluates: 1) Highest Weight. 2) Highest LOCAL_PREF. 3) Locally originated. 4) Shortest AS_PATH length. 5) Lowest origin code (IGP best). 6) Lowest MED. 7) eBGP over iBGP. 8) Lowest IGP metric to NEXT_HOP. 9) Oldest eBGP path. 10) Lowest router ID. 11) Shortest cluster list. 12) Lowest neighbor IP. AS path length is step 4 — meaning Weight and LOCAL_PREF override it completely. Use the Best Path Compare tab to run two paths through all 12 steps and see exactly which attribute decides the winner.
BGP AS path regex uses standard metacharacters plus the BGP-specific underscore _. The _ matches a space, comma, start, or end of string — making it the critical boundary character. Key patterns: ^$ = locally originated only. _65001$ = routes originating from AS 65001. ^65001_ = routes received directly from AS 65001 peer. _65001_ = any path transiting AS 65001. .* = any/all paths. Test your patterns with "show ip bgp regexp [pattern]" on Cisco or use the Regex Tester tab above before applying to production filters.
AS_SEQUENCE is the ordered list of traversed ASNs (each counts as 1 in path length). AS_SET is an unordered group created during route aggregation when multiple routes with different paths are summarized into one. The entire AS_SET only counts as 1 in path length regardless of how many ASNs it contains. AS_SETs are increasingly filtered by large ISPs because they complicate RPKI validation. RFC 6472 recommends against generating new AS_SETs. You will still see them in the wild on older aggregation configurations.
Use ip as-path access-list: "ip as-path access-list 1 permit ^$" (permit only locally originated). "ip as-path access-list 2 deny _65001_" then "ip as-path access-list 2 permit .*" (block routes transiting AS 65001, permit all others). Apply to neighbor: "router bgp 65000" / "neighbor 10.0.0.1 filter-list 2 in". For outbound: neighbor 10.0.0.1 filter-list 1 out. Always test first with "show ip bgp regexp [pattern]" before applying. The Regex Tester tab above lets you validate patterns against sample paths before touching the router.
The _ in BGP regex is a special metacharacter that matches: a space, a comma, the beginning of the string (^), or the end of the string ($). It acts as a word boundary for AS numbers. Without it, the pattern "65001" could match "165001" or "650010" — but "_65001_" matches only when 65001 appears as a standalone AS number. This is why most BGP regex patterns use _ instead of plain spaces. It is the single most important character to understand when writing BGP AS path filters.
1 to 3 prepends is the practical range. Start with 1 prepend and observe traffic distribution after BGP convergence (allow 15-30 minutes). Add more if traffic does not shift sufficiently. Beyond 4 prepends: many ISPs and IXPs filter routes with extremely long AS paths (typically anything over 20-25 total hops is suspect and may be dropped). Prepending does not guarantee traffic shift — remote networks may have overriding LOCAL_PREF policies set. AS path length is just step 4 of 12 in their best path algorithm.
No. 4-byte ASNs (65536 and above, written in plain format like 131072 or dot notation like 2.0) count exactly the same as 2-byte ASNs — 1 per entry in AS_SEQUENCE. The only historical quirk: old equipment that did not understand 4-byte ASNs used AS 23456 (AS_TRANS) as a placeholder, which counted as 1 on old devices. Modern routers see the actual 4-byte ASN and count it normally. The AS Path Length calculator above handles both plain and dot-notation 4-byte ASNs.
BGP best path evaluation order places LOCAL_PREF at step 2, before AS path at step 4. If a router has LOCAL_PREF 200 set on path A and LOCAL_PREF 100 on path B, path A always wins — regardless of AS path length. You control LOCAL_PREF for your outbound traffic. For inbound, you cannot control a remote network's LOCAL_PREF — that is why AS path prepending (affecting step 4 at remote routers) is the standard inbound traffic engineering tool, but it only works when paths have equal LOCAL_PREF at the remote side.
Key commands: "show ip bgp" — full table with all AS paths. "show ip bgp [prefix/length]" — detailed attributes for one prefix. "show ip bgp regexp [pattern]" — test AS path regex filter before deploying. "show ip bgp neighbors [ip] advertised-routes" — what you are advertising and with what AS path. "debug ip bgp [neighbor] updates" — real-time BGP update monitoring (use with care in production). The most important for testing filters is "show ip bgp regexp" — it lets you validate your regex pattern against the live routing table without risk.
A route reflector (RR) distributes iBGP routes between peers without requiring a full iBGP mesh. Route reflectors add CLUSTER_LIST and ORIGINATOR_ID attributes for iBGP loop prevention — but these do NOT affect the AS_PATH attribute or its length. The AS path itself passes through route reflectors unchanged. However, CLUSTER_LIST length is used as tiebreaker step 11 in best path selection (after AS path, router ID, and several other criteria). So route reflector topology can indirectly affect which path is selected when multiple criteria tie.