User agent strings identify the browser, operating system, and device making a request. They follow loose conventions but vary widely in format. Understanding their structure helps you parse them correctly and build device-specific features.
User Agent Components
A typical user agent contains: browser name and version (Chrome/120.0), rendering engine (AppleWebKit/537.36, Gecko), operating system (Windows NT 10.0, Macintosh), device type (Mobile, Tablet), and sometimes additional tokens for compatibility. For example: "Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 Chrome/120.0.0.0". Each segment provides clues about the client environment.
Why User Agents Are Messy
User agents are notoriously inconsistent because browsers prioritize compatibility over clarity. Most browsers include "Mozilla/5.0" for historical reasons (early websites checked for Netscape). Chrome includes "Safari" and "AppleWebKit" because it's based on WebKit. Mobile browsers often include "Mobile" tokens. This legacy mess makes parsing challenging—use libraries rather than regex.
Client Hints: The Modern Alternative
User-Agent Client Hints (UA-CH) is a newer standard that provides structured device information via HTTP headers like Sec-CH-UA, Sec-CH-UA-Platform, and Sec-CH-UA-Mobile. Browsers send basic info by default; servers can request detailed info. This approach offers better privacy (less fingerprinting) and cleaner parsing. However, adoption is gradual—use user agent parsing as fallback.
Mobile vs Desktop Detection
Mobile user agents typically include "Mobile" or "Android" tokens. Tablets may include "Tablet" or report as desktop. iOS devices include "iPhone" or "iPad". However, some mobile browsers can request desktop sites, sending desktop user agents. For critical mobile detection, combine user agent analysis with screen size detection or CSS media queries.