# Agentry Agentry is a Ruby gem that tells you who sent an HTTP request: which browser and version, on which operating system and device, or which bot. It reads the `User-Agent` header, a line of text every browser and crawler sends with each request, such as `Mozilla/5.0 (iPhone; CPU iPhone OS 18_1 like Mac OS X) … Version/18.1 Mobile/15E148 Safari/604.1`. Each browser and crawler writes it in its own way, and Agentry knows the formats so that your code can ask plain questions such as `agentry.bot?` or `agentry.device.mobile?`. Apps use the answers to leave bots out of their analytics, to turn crawlers away, to adapt a page to phones, or to warn visitors on an old browser. Agentry works in any Ruby program, and gives Rails controllers and views an `agentry` helper for the current request. Website and guide: https://levups.github.io/agentry/ Coding agents can start from , or read this README and the website's pages as one Markdown file: . ## Why Agentry Agentry is a fork of [browser](https://github.com/fnando/browser), the gem Nando Vieira wrote, which has not changed since June 2025. It has the same methods under its own name, `Agentry` instead of `Browser`, and Agentry 4 still answers to the old names, so code written for browser runs on it as it is (see [Switching from browser](https://github.com/levups/agentry#switching-from-browser)). Here is what is different: - It answers in 25 µs per request instead of 126 µs with browser 6.2.0, and in 2 µs for a user agent the process has seen before (Ruby 4.0.9 with YJIT, see [Performance](https://github.com/levups/agentry#performance)). - Its list of bots is updated every week. Of the 882 crawler user agents added to [crawler-user-agents](https://github.com/monperrus/crawler-user-agents) since 2024 (test drivers and scanners left out), browser misses 353 and Agentry 11. - It says what kind of bot sent the request: an AI crawler, a search engine, an assistant fetching a page for a person, a vulnerability scanner. - It can check that a request claiming to be Googlebot, Bingbot or GPTBot comes from the IP addresses Google, Microsoft or OpenAI publish for their crawlers. - It reads the headers Cloudflare can add to a request, and the Client Hints Chromium browsers send, which tell Windows 11 from Windows 10. ## Installation Agentry needs Ruby 3.3 or later. It is tested on Rails 8.0 and 8.1, and works without Rails. Add it to your Gemfile: ```ruby gem "agentry" ``` If the Gemfile already has `gem "browser"`, see [Switching from browser](https://github.com/levups/agentry#switching-from-browser). ## A first example ```ruby require "agentry" user_agent = "Mozilla/5.0 (iPhone; CPU iPhone OS 18_1 like Mac OS X) AppleWebKit/605.1.15 " \ "(KHTML, like Gecko) Version/18.1 Mobile/15E148 Safari/604.1" agentry = Agentry.new(user_agent, accept_language: "fr-FR,fr;q=0.9,en;q=0.8") agentry.name # => "Safari" agentry.version # => "18" agentry.full_version # => "18.1" agentry.platform.name # => "iOS (iPhone)" agentry.device.mobile? # => true agentry.bot? # => false agentry.accept_language.first.full # => "fr-FR" ``` `accept_language:` is optional: it takes the `Accept-Language` header, the languages the visitor's browser asks for. In Rails there is nothing to build. The `agentry` helper reads the current request's headers: ```erb <% if agentry.device.mobile? %> <%= render "mobile_menu" %> <% end %> ``` Outside Rails, pass the Rack env as `headers:` so that Agentry can read Client Hints and Cloudflare headers too: `Agentry.new(user_agent, accept_language: language, headers: env)`. The [guide](https://levups.github.io/agentry/guide.html#usage) lists every method: browsers from Chrome and Safari to WeChat and Yandex, devices such as the iPad, Kindle, PlayStation and Xbox, and platforms down to each version of Windows. Version checks take a number or constraints, such as `agentry.chrome?(">= 65")` or `agentry.platform.ios?(9)`. ## Bots ```ruby agentry = Agentry.new("Mozilla/5.0 (compatible; GPTBot/1.2; +https://openai.com/gptbot)") agentry.bot? # => true agentry.bot.name # => "ChatGPT" agentry.bot.category # => :ai_crawler agentry.bot.ai? # => true agentry.bot.user_triggered? # => false ``` `agentry.bot?` is true for an empty user agent, for one that matches `/crawl|fetch|search|monitoring|spider|bot/`, and for one that contains a name from [bots.yml](https://github.com/levups/agentry/blob/main/bots.yml). The net is wide on purpose, as it was in browser. Every Monday, a workflow adds the bots of [crawler-user-agents](https://github.com/monperrus/crawler-user-agents) and [ai.robots.txt](https://github.com/ai-robots-txt/ai.robots.txt) (both MIT) to `bots.yml` and files each under a category. It opens a pull request that lists every answer the change moves, and a maintainer merges it. The [Bots page](https://levups.github.io/agentry/bots.html#coverage) counts, per category, how many of those bots browser and Agentry detect. ### Categories `agentry.bot.category` is what the bot says it is, named after Cloudflare's verified bot categories where one fits: `search_engine_crawler`, `ai_crawler`, `ai_search`, `ai_assistant`, `page_preview`, `feed_fetcher`, `monitoring_and_analytics`, `advertising_and_marketing`, `archiver`, `security` and a few more. Agentry adds `ai_agent`, `http_library`, `scanner` and `browser_automation`. `bot.ai?` is true for the four AI categories, and `bot.user_triggered?` for `ai_assistant` and `ai_agent`: a person is driving them at that moment, as with ChatGPT-User or Claude-User. The category is `nil` for a browser, for a bot exception, and for a bot no list names. HTTP libraries (`curl`, `python-requests`) and scanners (sqlmap, Nikto) are bots. Test drivers (headless Chrome, Playwright, Puppeteer, Selenium) are not: your own system tests drive a real browser through them, and would otherwise count as bots. They get the `browser_automation` category, so production code can still catch them with `agentry.bot? || agentry.bot.category == :browser_automation`. PhantomJS stays a bot, as in browser. ### Your own rules A matcher is anything that responds to `call(ua, browser)`. This one makes every user agent containing `externalhit` a bot: ```ruby Agentry::Bot.matchers << ->(ua, _browser) { ua.match?(/externalhit/i) } ``` `agentry.bot.why?` returns the matcher that fired. The lists can change at runtime too (`Agentry::Bot.bots.merge!(new_bots_hash)`), and the [Bots page](https://levups.github.io/agentry/bots.html#matchers) shows how to remove the built-in matchers or go back to the detection of browser 2.x. ### Verified crawlers Anyone can send `Googlebot` as their user agent. Google, Bing, OpenAI, Apple, Perplexity and DuckDuckGo publish the IP ranges their crawlers use, and Agentry ships them, refreshed by the weekly sync. `bot.verified?(ip)` says whether a request comes from the ranges of the company its user agent names: ```ruby agentry.bot.owner # => :google for a user agent claiming to be Googlebot agentry.bot.verified?(request.remote_ip) # => true when the IP is in Google's ranges if agentry.bot.owner && !agentry.bot.verified?(request.remote_ip) head :forbidden # claims to be a known crawler, comes from somewhere else end ``` The check searches the ranges in memory, with no network call, and an IP that does not parse answers `false`. Pass the IP as your proxies resolve it: `request.remote_ip` in Rails, with `trusted_proxies` set. `verified?(ip, reverse_dns: true)` adds the DNS check Google, Bing and Apple document, at the cost of two DNS queries for an IP outside the ranges. A fake Googlebot controls its own reverse DNS and can make that lookup wait for the timeout (1 second per nameserver of `/etc/resolv.conf`), so a cache, off by default, can remember each answer, failures included: ```ruby Agentry::Bot::IPRanges.dns_cache_size = 10_000 # 0, the default, caches nothing Agentry::Bot::IPRanges.dns_cache_ttl = 3600 # seconds Agentry::Bot::IPRanges.dns_timeout = 0.5 # seconds per query and nameserver Agentry::Bot::IPRanges.refresh # fetches the ranges again, in a long-lived process ``` The reverse lookup reads answers from a name server the requester chooses, and versions of the `resolv` gem before 0.7.2 (0.3.2 in the 0.3 series) can be made to use up memory or CPU by such answers (CVE-2026-80212, CVE-2025-24294). Before turning `reverse_dns:` on, check `bundle info resolv`, or add `gem "resolv", ">= 0.7.2"` to your Gemfile. ## Cloudflare and Client Hints When the Rack env is passed as `headers:` (the Rails helper and the middleware do it for you), Agentry reads two more sources. Behind Cloudflare, a Transform Rule can copy what Cloudflare knows about a request into headers, and `agentry.cloudflare` reads them: ```ruby agentry.cloudflare.verified_bot? # => true for a bot Cloudflare verified agentry.cloudflare.bot_category # => :search_engine_crawler, with the names of bot.category agentry.cloudflare.bot_score # => 1 (automated) to 99 (human), with Bot Management only agentry.cloudflare.country # => "FR", from CF-IPCountry ``` The header names can be changed with `Agentry::Cloudflare.header_names = {bot_score: "X-My-Score"}`. These headers prove something only when every request goes through Cloudflare: if clients can reach your server directly, they can send the headers themselves. The [guide](https://levups.github.io/agentry/guide.html#cloudflare) has the rule to create and how to lock the origin. Chromium browsers no longer put the real Windows, macOS or Android version in their user agent. They send it in Client Hints (`Sec-CH-UA`, `Sec-CH-UA-Mobile`, `Sec-CH-UA-Platform`, `Sec-CH-UA-Platform-Version`), which Agentry uses to refine the platform and the device: `agentry.platform.windows11?` needs them. Without `headers:`, every answer comes from the user agent alone, as in browser. The [guide](https://levups.github.io/agentry/guide.html#client-hints) shows how to ask browsers for the platform version. `bench/signals.rb` measures what reading these headers costs. ## Accept-Language `agentry.accept_language` turns the `Accept-Language` header into a list of languages, the most wanted first: ```ruby language = Agentry.new(user_agent, accept_language: "en-us").accept_language.first language.code # => "en" language.region # => "US" language.full # => "en-US" language.quality # => 1.0 language.name # => "English/United States" ``` As the HTTP specification says, a language without a quality value has 1.0, and one with a quality of 0 is not accepted, so it is left out. ## Middleware `Agentry::Middleware` redirects requests based on the browser, before they reach your app. In Rails, from an initializer such as `config/initializers/agentry.rb`, route helpers work and `request` is the `Rack::Request`: ```ruby Rails.configuration.middleware.use Agentry::Middleware do redirect_to upgrade_path if agentry.ie? && request.env["PATH_INFO"] != "/exclude_me" end ``` In another Rack app, `use Agentry::Middleware do … end` takes the same block. To use Agentry in Rails without the `agentry` helper, require only the library: `gem "agentry", require: "agentry/agentry"`. ## Cache, limits and boot Each process remembers what it found for the last 1,000 user agents (browser, platform, device and bot answers) and the last 1,000 `Accept-Language` headers, about 1 KB per user agent. A user agent seen before costs about 2 µs. The cache empties itself when it is full, and when `Agentry.matchers`, `Agentry::Bot.matchers`, `Agentry::Platform.matchers`, `Agentry::Device.matchers`, the bot list or its exceptions change. Setting `Agentry.cache_size` empties it too, for example after renaming an entry of `Agentry::Bot.bots` in place: ```ruby Agentry.cache_size = 5_000 # 0 turns it off ``` A user agent or an `Accept-Language` header longer than 2048 bytes raises `Agentry::Error`. Both limits can be raised with `Agentry.user_agent_size_limit = 4096` and `Agentry.accept_language_size_limit = 4096`. An `Accept-Language` header listing more than 32 languages is parsed on each request instead of cached, so that crafted headers cannot fill the cache with thousands of languages each. Headers whose bytes are not valid UTF-8, forged or read back from a log file, do not raise: Agentry reads the user agent and the `Accept-Language` header with each invalid byte replaced by U+FFFD, and ignores such a Client Hint or Cloudflare header. Version checks such as `agentry.chrome?(">= 65")` leave the versions they read from a request out of RubyGems' version cache, where those an attacker sends would pile up for the life of the process. The lists and matchers are built on first use: the first request of each process takes about 13 ms more, the following ones 0.2 ms. `Agentry.eager_load!` builds them at boot, so that a server that loads the app before forking (Puma's `preload_app!`, Unicorn, Pitchfork) shares them with every worker. Rails calls it whenever it eager loads, which production does by default. Elsewhere, call `Agentry.eager_load!` before the fork. ## Switching from browser Replace `gem "browser"` with `gem "agentry", require: "browser"` in the Gemfile (keep `require: "browser/browser"` if the app had it). Code written for browser keeps working on Agentry 4: the require paths browser documents (`browser`, `browser/browser`, `browser/aliases`) and `browser/testing`, `browser/rails` still load and define `Browser` as another name for `Agentry`, and the `browser` helper still answers. A bare `gem "agentry"` leaves out the `Browser` constant, which then raises `NameError`. The old names are deprecated and go away in Agentry 5, so move to the new ones when you can: | browser, deprecated in Agentry 4 | Agentry | |---|---| | `gem "browser"`, `require "browser"` | `gem "agentry"`, `require "agentry"` | | `require: "browser/browser"` (without Rails) | `require: "agentry/agentry"` | | `require "browser/aliases"`, `"browser/testing"`, `"browser/rails"` | `require "agentry/aliases"`, `"agentry/testing"`, `"agentry/rails"` | | `Browser.new(…)`, `Browser::Bot`, `Browser::Middleware`… | `Agentry.new(…)`, `Agentry::Bot`, `Agentry::Middleware`… | | the `browser` helper, `browser` in a middleware block | `agentry` | | `@browser`, in controllers, views, tests and middleware blocks (no warning) | `@agentry` | | `config.browser` | `config.agentry` | Each deprecation warns once per process, only where Ruby shows deprecations: run the tests with `RUBYOPT=-W:deprecated` to list what is left to move. The [migration guide](https://levups.github.io/agentry/migration.html) walks through the change, and gives a prompt for a coding agent to make it. Class names change even where code keeps calling `Browser`: `browser.class.name` reads `Agentry::Chrome`. Agentry 4 ships the same `browser/*` files as the browser gem, so it cannot share a bundle with it, for example through a dependency that still needs browser; Agentry 5 drops those files. Some answers change from the day you switch, because of the fresher bot list: 600 user agents of the corpus that browser took for people are bots in Agentry, so analytics that leave bots out will count fewer visitors, and 792 user agents that browser called `Generic Bot` get a real name. No browser, platform or device answer changes, and test drivers still are not bots. To go back, remove the calls to what browser does not have (`cache_size=`, the `headers:` argument, `bot.category`, `bot.ai?`, `bot.user_triggered?`, `bot.verified?`, `cloudflare`), bring back the browser names of the table above, then put `gem "browser"` back. The [migration guide](https://levups.github.io/agentry/migration.html#going-back) has the details. ## Performance `bench/request.rb` asks what a web request typically asks (name, version, `bot?`, platform, device, accept-language) over 2,000 distinct user agents of the corpus and over 20 repeated ones, and keeps the median of 7 runs. On Ruby 4.0.9 with YJIT: | Per request | browser 6.2.0 | Agentry | |---|---:|---:| | A new user agent | 126 µs, 245 objects | 25 µs, 52 objects | | A user agent seen before | 114 µs, 217 objects | 2 µs, 12 objects | The [benchmarks page](https://levups.github.io/agentry/benchmarks.html) has the runs without YJIT, the machine, and why Agentry is faster. To run them again: ```bash bundle exec rake bench UPSTREAM=path/to/upstream # a clone of fnando/browser at dd88933 ``` It runs `ruby [--yjit] -I lib bench/request.rb --write` for each gem and JIT setting, records each run (gem, commit, Ruby, YJIT, machine, date) in `docs/benchmarks.json`, and `rake docs:render` then rewrites the site's numbers from that file. ## How the answers are checked Agentry runs browser's own test suite, unmodified (it calls `Browser`, which Agentry 4 still answers to), next to its own tests. The repository also holds a corpus of 20,328 user agents, gathered from uap-core, crawler-user-agents and browser's test fixtures. On every pull request, CI asks both browser and Agentry about each of them and compares the answers. An answer that differs is either listed in `corpus/expected_differences.jsonl` as an intended change, such as a newly known bot, or fails the build. ## Development ```bash bundle install bundle exec rake test bundle exec rubocop ``` To compare every answer with [fnando/browser](https://github.com/fnando/browser) at `dd88933`, as CI does: ```bash ruby -I path/to/upstream/lib script/corpus_snapshot.rb > /tmp/upstream.jsonl ruby -I lib script/corpus_snapshot.rb > /tmp/agentry.jsonl ruby script/corpus_diff.rb /tmp/upstream.jsonl /tmp/agentry.jsonl ``` It passes when the answers that differ are exactly those in `corpus/expected_differences.jsonl`. A change meant to change answers rewrites that file with `ruby script/corpus_diff.rb --write …`, and its diff shows what changed. The weekly bot sync (`.github/workflows/bot-sync.yml`) runs these steps, which also work by hand: ```bash ruby script/sync_bots.rb # bots.yml and bot_categories.yml ruby -I lib script/sync_ip_ranges.rb # bot_ip_ranges.yml ruby script/build_data.rb # data/*.json, what the gem reads, from the *.yml ruby script/bot_coverage.rb path/to/upstream/lib # docs/bot_coverage.json, and its Markdown table ruby script/render_docs.rb # the site's numbers, from docs/*.json and the lists ``` The numbers on the site's pages (`docs/*.html`) sit between `` markers and come from `docs/benchmarks.json`, `docs/bot_coverage.json`, the bot lists and `corpus/expected_differences.jsonl`. CI fails when a page differs from what `bundle exec rake docs:render` writes. A new code block on a page needs `bundle exec rake docs:highlight`. To add a bot, add its user agent to `test/ua_bots.yml`, and its name to `bots.yml` above the synced section, under a lowercase string its user agent contains. A search engine goes in `test/ua_search_engines.yml`, `test/ua_bots.yml` and `search_engines.yml`. See [CONTRIBUTING.md](https://github.com/levups/agentry/blob/main/CONTRIBUTING.md) before opening a pull request. ### Releasing Pull requests leave `CHANGELOG.md` alone. A release starts by writing its notes under `## [Unreleased]`, from the pull requests merged since the last tag, without committing them. Then, from an up-to-date `main` with green CI: ```sh bin/release minor --dry-run # shows the notes and the changes, writes nothing bin/release minor # or patch, major, or an exact version such as 1.2.0 ``` `main` takes no direct push, so `bin/release` dates the notes and bumps the version on a `release/vX.Y.Z` branch, opens a pull request that merges itself once CI is green, and waits for it (`bin/release --wait X.Y.Z` if you stopped it). The merge starts the Release workflow, which waits for a maintainer's approval, tags the merge commit `vX.Y.Z`, publishes the gem to RubyGems through trusted publishing and creates the GitHub release with the notes and the `.gem`. ## License MIT, by Level UP Solutions, see [LICENSE.md](https://github.com/levups/agentry/blob/main/LICENSE.md). Originally written by Nando Vieira as [fnando/browser](https://github.com/fnando/browser) (MIT). The corpus gathers user agents from [ua-parser/uap-core](https://github.com/ua-parser/uap-core) (Apache 2.0), [crawler-user-agents](https://github.com/monperrus/crawler-user-agents) (MIT) and browser's test fixtures. The bot list and its categories come from [crawler-user-agents](https://github.com/monperrus/crawler-user-agents) (MIT) and [ai.robots.txt](https://github.com/ai-robots-txt/ai.robots.txt) (MIT). The crawler IP ranges are the ones their owners publish for that purpose. [corpus/SOURCES.md](https://github.com/levups/agentry/blob/main/corpus/SOURCES.md) lists every source. ## Agentry Bots Bot detection is deliberately aggressive: a user agent is a bot as soon as one matcher says so, unless it is a known exception. ### The lists 1,572 · named bots · `bots.yml`, keyed by a lowercase substring of the user agent 16 · categories · `bot_categories.yml`: `ai_crawler`, `search_engine_crawler`, `scanner`… 6 · search engines · `search_engines.yml`: answer `bot.search_engine?` 4 · exceptions · `bot_exceptions.yml`: never a bot, whatever a matcher says `agentry.bot.name` is the name from `bots.yml`, or `Generic Bot` when another matcher caught it. The lists are in the [repository](https://github.com/levups/agentry/blob/main/bots.yml). Every week, `script/sync_bots.rb` rewrites the part of `bots.yml` below its marker line, and `bot_categories.yml`, from crawler-user-agents and ai.robots.txt, and a pull request shows the answers that change. Above the marker, the list inherited from browser stays as it was. ### Coverage How many of the bots of crawler-user-agents and ai.robots.txt each gem calls a bot, per category: every user agent crawler-user-agents gives as an instance, and, for each name of ai.robots.txt, which gives none, the user agent an AI agent of 2026 sends. `script/bot_coverage.rb` counts them into `docs/bot_coverage.json` and the weekly sync regenerates it, with this table. Of the 883 crawler user agents added to crawler-user-agents since 2024 (test drivers and scanners left out), browser misses 354 and Agentry 11. Against fnando/browser dd88933. | Category | User agents | browser: `bot?` | Agentry: `bot?` | Agentry: a category | | --- | --- | --- | --- | --- | | `academic_research` | 35 | 24 | 35 | 34 | | `advertising_and_marketing` | 97 | 78 | 94 | 94 | | `ai_agent` | 37 | 12 | 23 | 30 | | `ai_assistant` | 38 | 20 | 37 | 37 | | `ai_crawler` | 187 | 151 | 184 | 184 | | `ai_search` | 45 | 36 | 44 | 43 | | `archiver` | 68 | 50 | 68 | 68 | | `browser_automation` | 23 | 2 | 2 | 21 | | `feed_fetcher` | 92 | 40 | 91 | 91 | | `http_library` | 103 | 68 | 102 | 102 | | `monitoring_and_analytics` | 261 | 141 | 259 | 257 | | `page_preview` | 136 | 110 | 135 | 133 | | `scanner` | 108 | 46 | 106 | 106 | | `search_engine_crawler` | 418 | 384 | 417 | 406 | | `search_engine_optimization` | 685 | 481 | 674 | 671 | | Total | 2,333 | 1,643 | 2,271 | 2,277 | `browser_automation` is never `bot?` on purpose ([below](https://levups.github.io/agentry/bots.html#categories)). `ai_agent` misses its plain-word names (`Operator`, `Cursor`, `Devin`, `Codex`): as substrings they would match human user agents. ### Categories `agentry.bot.category` names what a bot declares it is, with the names of Cloudflare’s verified bot categories where one fits, so an app can compare it with what Cloudflare verified. It is `nil` for a browser, a bot exception, and a bot no list names (caught by a keyword). ```ruby agentry = Agentry.new("Mozilla/5.0 (compatible; Claude-User/1.0)") agentry.bot.category # => :ai_assistant agentry.bot.ai? # => true: ai_crawler, ai_search, ai_assistant or ai_agent agentry.bot.user_triggered? # => true: ai_assistant or ai_agent, acting for a person right now ``` | Category | `bot?` | | --- | --- | | `search_engine_crawler`, `search_engine_optimization`, `ai_crawler`, `ai_search`, `ai_assistant`, `ai_agent`, `page_preview`, `feed_fetcher`, `advertising_and_marketing`, `monitoring_and_analytics`, `archiver`, `academic_research`, `security` | true | | `http_library`: curl, got, python-requests… | true, as `curl` and `python-requests` already were in browser: system tests don’t send them. | | `scanner`: sqlmap, Nikto, Censys… | true | | `browser_automation`: HeadlessChrome, Playwright, Puppeteer, Selenium… | false: the system tests of an app drive a real browser through them, and Playwright’s and Selenium’s headless Chrome send `HeadlessChrome`. PhantomJS stays a bot, as in browser. | To treat test drivers as bots in production: `agentry.bot? || agentry.bot.category == :browser_automation`. ### Matchers | Matcher | A bot when | | --- | --- | | `Agentry::Bot::EmptyUserAgentMatcher` | The user agent is empty. | | `Agentry::Bot::KnownBotsMatcher` | The user agent contains a key of `bots.yml`. | | `Agentry::Bot::KeywordMatcher` | The user agent matches `/crawl\|fetch\|search\|monitoring\|spider\|bot/`. | `agentry.bot.why?` returns the matcher that fired. A matcher is any callable taking the lowercased user agent and a browser: ```ruby Agentry::Bot.matchers << ->(ua, _browser) { ua.match?(/externalhit/i) } ``` To start from nothing, `Agentry::Bot.matchers.clear`; to get the built-in ones back, `Agentry::Bot.matchers.concat(Agentry::Bot.default_matchers)`. To restore browser 2’s detection, remove the keyword and empty user agent matchers: ```ruby Agentry::Bot.matchers.delete(Agentry::Bot::KeywordMatcher) Agentry::Bot.matchers.delete(Agentry::Bot::EmptyUserAgentMatcher) ``` ### Extending the lists ```ruby Agentry::Bot.bots.merge!(new_bots_hash) Agentry::Bot.bot_exceptions.concat(new_exceptions) Agentry::Bot.search_engines.merge!(new_search_engines_hash) ``` The prefix tries behind each list are rebuilt when the list grows or is replaced. `Agentry::Bot.category_by_keyword` takes new categories the same way. To add a bot to Agentry itself, add its user agent to `test/ua_bots.yml` and its name to `bots.yml`; a search engine also goes in `test/ua_search_engines.yml` and `search_engines.yml`. **A trap.** Public crawler lists call “bot” anything that is not a human browser: crawlers, but also test drivers (Playwright, Puppeteer, Selenium), scripts and scanners. Adding them all to `bot?` would flag the system tests of the apps using it, so test drivers get a category of their own and no `bot?`. ### The corpus `corpus/user_agents.txt` holds 20,328 distinct user agents. A change that is not meant to change an answer proves it there: one script writes every answer of a gem as JSON lines, another diffs two snapshots. ```sh ruby -I path/to/upstream/lib script/corpus_snapshot.rb > /tmp/upstream.jsonl ruby -I lib script/corpus_snapshot.rb > /tmp/agentry.jsonl ruby script/corpus_diff.rb /tmp/upstream.jsonl /tmp/agentry.jsonl ``` `path/to/upstream` is a plain clone of [fnando/browser](https://github.com/fnando/browser) at `dd88933`. A change meant to change answers, like a fresher bot list, lists the differences it expects in `corpus/expected_differences.jsonl` (`corpus_diff.rb --write`), and the diff must show exactly those. No browser of uap-core turns into a bot: the sync leaves out any keyword that would flag one. ### Data sources The corpus gathers, deduplicated, the user agents of: - the test cases of [ua-parser/uap-core](https://github.com/ua-parser/uap-core), Apache License 2.0; - the `instances` of [monperrus/crawler-user-agents](https://github.com/monperrus/crawler-user-agents), MIT; - the test fixtures of [fnando/browser](https://github.com/fnando/browser), MIT. The bot list and its categories are synced from permissively licensed sources only: [crawler-user-agents](https://github.com/monperrus/crawler-user-agents) and [ai.robots.txt](https://github.com/ai-robots-txt/ai.robots.txt), both MIT. `script/bot_coverage.rb` counts, per category, the bots of both lists that browser and Agentry detect, into `docs/bot_coverage.json`. Details in [`corpus/SOURCES.md`](https://github.com/levups/agentry/blob/main/corpus/SOURCES.md). ## Agentry Guide Every predicate of the browser gem, under the `Agentry` name, and what Agentry adds: categories and verified crawlers, Cloudflare headers, Client Hints. ### Installation ```ruby gem "agentry" ``` Agentry requires Ruby 3.3 or later. It is tested on Rails 8.0 and 8.1, and works without Rails. ### Migrating from browser Replace `gem "browser"` with `gem "agentry", require: "browser"`. Agentry 4 still answers to `Browser`, the `browser/*` require paths and the `browser` helper, deprecated until 5.0, so the app keeps running before you rename anything. What changes on its own (more bots from the first day), the table of names to move to `Agentry`, a prompt for a coding agent and how to go back are on the [migration page](https://levups.github.io/agentry/migration.html). ### Usage ```ruby require "agentry" agentry = Agentry.new("Some User Agent", accept_language: "en-us") ``` #### Browser ```ruby agentry.name # readable browser name agentry.version # major version number agentry.full_version agentry.to_s # the meta info joined by space agentry.meta # an array with several attributes agentry.known? # has the browser been successfully detected? agentry.unknown? # the browser wasn't detected agentry.webkit? agentry.webkit_full_version agentry.chromium_based? agentry.chrome? agentry.edge? # newest MS browser agentry.electron? # Electron Framework agentry.firefox? agentry.ie? agentry.ie?(6) # detect specific IE version agentry.ie?([">8", "<10"]) # detect specific IE (IE9) agentry.opera? agentry.opera_mini? agentry.safari? agentry.safari_webapp_mode? agentry.samsung_browser? agentry.core_media? agentry.duck_duck_go? agentry.epiphany? agentry.nokia? agentry.phantom_js? agentry.qq? agentry.quicktime? agentry.sougou_browser? agentry.sputnik? agentry.uc_browser? agentry.wechat? agentry.weibo? agentry.yandex? ``` Version predicates take a version or a list of constraints, so a “modern browser” check is a method of yours: ```ruby def modern_browser?(agentry) [ agentry.chrome?(">= 65"), agentry.safari?(">= 10"), agentry.firefox?(">= 52"), agentry.ie?(">= 11") && !agentry.compatibility_view?, agentry.edge?(">= 15"), agentry.opera?(">= 50"), agentry.facebook? && agentry.safari_webapp_mode? && agentry.webkit_full_version.to_i >= 602 ].any? end ``` #### Bots ```ruby agentry.bot? agentry.bot.name agentry.bot.search_engine? agentry.bot.why? # which matcher detected this user agent as a bot Agentry::Bot.why?(user_agent) ``` How a bot is detected, and how to change the lists, is on the [Bots page](https://levups.github.io/agentry/bots.html). #### Device ```ruby agentry.device agentry.device.id agentry.device.name agentry.device.unknown? agentry.device.mobile? agentry.device.tablet? agentry.device.tv? agentry.device.console? agentry.device.ipad? agentry.device.iphone? agentry.device.ipod_touch? agentry.device.kindle? agentry.device.kindle_fire? agentry.device.silk? agentry.device.surface? agentry.device.samsung? agentry.device.blackberry_playbook? agentry.device.nintendo? agentry.device.switch? agentry.device.wii? agentry.device.wiiu? agentry.device.playstation? agentry.device.ps3? agentry.device.ps4? agentry.device.psp? agentry.device.vita? agentry.device.xbox? agentry.device.xbox_360? agentry.device.xbox_one? ``` #### Platform ```ruby agentry.platform agentry.platform.id agentry.platform.name agentry.platform.version # e.g. 9 (for iOS 9) agentry.platform.unknown? agentry.platform.adobe_air? agentry.platform.android? agentry.platform.android?(4.2) # detect Android Jelly Bean 4.2 agentry.platform.android_app? # detect webview in an Android app agentry.platform.android_webview? # alias for android_app? agentry.platform.blackberry? agentry.platform.blackberry?(10) # detect specific BlackBerry version agentry.platform.chrome_os? agentry.platform.firefox_os? agentry.platform.ios? agentry.platform.ios?(9) # detect specific iOS version agentry.platform.ios_app? # detect webview in an iOS app agentry.platform.ios_webview? # alias for ios_app? agentry.platform.kai_os? agentry.platform.linux? agentry.platform.mac? agentry.platform.windows? agentry.platform.windows_xp? agentry.platform.windows_vista? agentry.platform.windows7? agentry.platform.windows8? agentry.platform.windows8_1? agentry.platform.windows10? agentry.platform.windows11? # from Client Hints only, see below agentry.platform.windows_rt? agentry.platform.windows_mobile? agentry.platform.windows_phone? agentry.platform.windows_touchscreen_desktop? agentry.platform.windows_wow64? agentry.platform.windows_x64? agentry.platform.windows_x64_inclusive? ``` The full lists live in [`lib/agentry/platform.rb`](https://github.com/levups/agentry/blob/main/lib/agentry/platform.rb) and [`lib/agentry/device.rb`](https://github.com/levups/agentry/blob/main/lib/agentry/device.rb). #### Aliases To call `agentry.mobile?` and `agentry.tablet?` on the base object, include the aliases: ```ruby require "agentry/aliases" Agentry::Base.include(Agentry::Aliases) Agentry.new("Some user agent").mobile? #=> false ``` ### Options | Setting | Default | What it does | | --- | --- | --- | | `accept_language:` | `nil` | The Accept-Language header, passed to `Agentry.new`; parsed into `agentry.accept_language`. | | `headers:` | `nil` | The Rack env of the request, passed to `Agentry.new`; read for the [Client Hints](https://levups.github.io/agentry/guide.html#client-hints) and the [Cloudflare](https://levups.github.io/agentry/guide.html#cloudflare) headers. The Rails helper and the middleware pass it. | | `Agentry.user_agent_size_limit` | 2048 | Largest user agent accepted, in bytes. A larger one raises `Agentry::Error`. | | `Agentry.accept_language_size_limit` | 2048 | Largest Accept-Language header accepted, in bytes. A larger one raises `Agentry::Error`. A header listing more than 32 languages is parsed on each request, never cached. | | `Agentry.cache_size` | 1000 | Distinct user agents (and Accept-Language headers) whose detection each process keeps, about 1 KB each; emptied when full or when a matcher list or the bot list changes. `0` turns it off. | | `Agentry::Bot.matchers` | three built-in matchers | Callables deciding whether a user agent is a bot. See [Bots](https://levups.github.io/agentry/bots.html#matchers). | | `Agentry::Bot.bots` | `bots.yml` | Known bots, keyed by a lowercase substring of their user agent. | | `Agentry::Bot.search_engines` | `search_engines.yml` | Bots answering `bot.search_engine?`. | | `Agentry::Bot.bot_exceptions` | `bot_exceptions.yml` | Substrings that are never a bot, even when a matcher says so. | | `Agentry::Bot::IPRanges` | `bot_ip_ranges.yml` | The IP ranges crawler owners publish; `.refresh` fetches them again. See [Verified crawlers](https://levups.github.io/agentry/guide.html#verified-crawlers). | | `Agentry::Cloudflare.header_names` | `X-Bot-Verified`, `X-Bot-Category`, `X-Bot-Score`, `CF-IPCountry` | The headers `agentry.cloudflare` reads; set a hash of the ones to change. See [Cloudflare](https://levups.github.io/agentry/guide.html#cloudflare). | ```ruby Agentry.user_agent_size_limit = 4096 Agentry.accept_language_size_limit = 4096 ``` A user agent or an Accept-Language header whose bytes are not valid UTF-8 (forged, or read back from a log file) does not raise: each invalid byte is read as U+FFFD. A Client Hint or a Cloudflare header that is not valid UTF-8 is ignored. ### Rails The Gemfile line is enough: a railtie adds an `agentry` helper to controllers and views, built from the request’s `User-Agent` and `Accept-Language` headers, its [Client Hints](https://levups.github.io/agentry/guide.html#client-hints) and [Cloudflare](https://levups.github.io/agentry/guide.html#cloudflare) headers. ```erb <% if agentry.ie?(6) %>

You're running an older IE version. Please update it!

<% end %> ``` To use Agentry in a Rails app without adding the helper to controllers, load only the library: ```ruby gem "agentry", require: "agentry/agentry" ``` ### Middleware `Agentry::Middleware` redirects user agents from a Rack stack: ```ruby use Agentry::Middleware do redirect_to "/upgrade" if agentry.ie? end ``` In Rails, route helpers work from an initializer (`config/initializers/agentry.rb`), and `request` gives the `Rack::Request`: ```ruby Rails.configuration.middleware.use Agentry::Middleware do redirect_to upgrade_path if agentry.ie? && request.env["PATH_INFO"] != "/exclude_me" end ``` ### Accept-Language `agentry.accept_language` is an array of languages, sorted by quality from highest to lowest. A missing quality means 1.0; a quality of zero means the language is not accepted. ```ruby language = Agentry.new(user_agent, accept_language: "en-us").accept_language.first language.code #=> "en" language.region #=> "US" language.full #=> "en-US" language.quality #=> 1.0 language.name #=> "English/United States" ``` ### Verified crawlers A user agent is a claim: anyone can send `Googlebot`. Google, Bing, OpenAI, Apple, Perplexity and DuckDuckGo publish the IP ranges of their crawlers, and Agentry ships them. `bot.owner` names the owner of the crawler a user agent claims to be, `bot.verified?(ip)` says whether the IP belongs to that owner’s ranges. ```ruby agentry.bot.owner # => :google, :bing, :openai, :apple, :perplexity, :duckduckgo or nil agentry.bot.verified?(request.remote_ip) # => true when a Googlebot comes from Google if agentry.bot.owner && !agentry.bot.verified?(request.remote_ip) head :forbidden # claims to be a known crawler, comes from elsewhere end ``` The lookup is a binary search over sorted integer ranges, IPv4 and IPv6, with no network call: `bench/signals.rb` measures it. An IP that does not parse (a forged `X-Forwarded-For`) answers `false`, never raises. Pass the client’s IP as your proxies resolve it (`request.remote_ip` in Rails, with `trusted_proxies` set). **Reverse DNS.** Google, Bing and Apple also document a DNS check: the IP’s reverse lookup must name one of their domains, and that name must resolve back to the IP. It costs two DNS queries, so it is off unless asked, and only tried for an IP outside the published ranges: ```ruby agentry.bot.verified?(request.remote_ip, reverse_dns: true) ``` A resolver answers in milliseconds, but a forged Googlebot picks its own IP, and its owner controls that IP’s reverse zone: one that never answers holds the request for `dns_timeout` (1 second) per nameserver of `/etc/resolv.conf`, on every request. A cache, off by default, remembers each verdict, failures included, so that IP pays once per TTL: ```ruby Agentry::Bot::IPRanges.dns_cache_size = 10_000 # verdicts kept, the oldest dropped first; 0 (the default) keeps none Agentry::Bot::IPRanges.dns_cache_ttl = 3600 # seconds a verdict holds (the default) Agentry::Bot::IPRanges.dns_timeout = 0.5 # seconds per query and nameserver, 1 by default ``` Each process holds its own cache. An attacker spreading over many IPs still costs one wait per IP: on a hot path, run the check in a background job and keep the answer in your own cache. The answers come from a name server the requester chooses, and versions of the `resolv` gem before 0.7.2 (0.3.2 in the 0.3 series) can be made to use up memory or CPU by such answers (CVE-2026-80212, CVE-2025-24294). Before turning `reverse_dns:` on, check `bundle info resolv`, or add `gem "resolv", ">= 0.7.2"` to your Gemfile. **Fresh ranges.** The weekly bot sync rewrites `bot_ip_ranges.yml` from the owners’ JSON files, and each release ships them. A process that runs for weeks can fetch them again. Each process holds its own copy; a failed source raises `Agentry::Error` and keeps the current ranges: ```ruby Agentry::Bot::IPRanges.refresh # fetches every owner's JSON, swaps the ranges at once # or fetch once, share them through a cache, and load them in every process prefixes_by_owner = Rails.cache.fetch("bot_ip_ranges", expires_in: 1.week) { Agentry::Bot::IPRanges.fetch } Agentry::Bot::IPRanges.load(prefixes_by_owner) ``` ### Cloudflare Behind Cloudflare, the edge already knows whether a bot is verified, its category and the visitor’s country. A Transform Rule copies what the app needs into request headers, and `agentry.cloudflare` reads them: ```ruby agentry.cloudflare.verified_bot? # => true for a bot Cloudflare verified agentry.cloudflare.bot_category # => :search_engine_crawler, :ai_crawler… (the names of bot.category) agentry.cloudflare.bot_score # => 1 (automated) to 99 (human), Bot Management only agentry.cloudflare.country # => "FR", "XX" when unknown, "T1" for Tor ``` The Rails helper and the middleware pass the request’s headers along. Elsewhere, pass the Rack env: `Agentry.new(user_agent, headers: env)`. Each site names its headers in its own rule; the defaults are generic, change any of them: ```ruby Agentry::Cloudflare.header_names = { verified_bot: "X-Bot-Verified", # the defaults bot_category: "X-Bot-Category", bot_score: "X-Bot-Score", country: "CF-IPCountry" } ``` **The Transform Rule.** In the Cloudflare dashboard, for your zone: 1. Open _Rules_, then _Overview_, and create a _Request Header Transform Rule_. Name it, for example, _Bot signals for the app_. 2. Under _If incoming requests match…_, choose _All incoming requests_: _Set_ replaces whatever header the client sent, so a header the rule sets on every request is one a client can’t forge. 3. Under _Then… Modify request header_, add one _Set dynamic_ operation per header: - `X-Bot-Verified`, value `cf.bot_management.verified_bot` (Bot Management), or `cf.client.bot` on plans without it: both give `true` or `false`; - `X-Bot-Category`, value `cf.verified_bot_category`, such as `Search Engine Crawler` or `AI Crawler`; - `X-Bot-Score`, value `cf.bot_management.score`, only with Bot Management (Enterprise): leave it out otherwise. 4. Deploy the rule. 5. For the country, turn on _IP Geolocation_ under _Network_: Cloudflare then adds `CF-IPCountry` itself, no rule needed. The headers prove something only if every request goes through Cloudflare: accept connections on the origin from Cloudflare’s IPs only (or through a tunnel), or a client can reach it with forged headers. ### Client Hints Chromium browsers freeze their user agent: Windows 11 still says `Windows NT 10.0`, macOS stays at `10_15_7`, Android at `Android 10; K`. They send the real values as User-Agent Client Hints, and Agentry uses them when present, falling back on the user agent otherwise (Safari and Firefox send none). The Rails helper and the middleware pass them along; elsewhere, pass the Rack env as `headers:`. ```ruby agentry = Agentry.new(user_agent, accept_language: language, headers: rack_env) agentry.platform.windows11? # Sec-CH-UA-Platform-Version 13 or more agentry.platform.version # => "11", "15.1.0" on macOS 15.1, "15.0.0" on Android 15 agentry.device.mobile? # Sec-CH-UA-Mobile, when sent agentry.client_hints.platform # => "Windows" agentry.client_hints.version_by_brand # => {"Chromium" => "142", "Google Chrome" => "142"} ``` `Sec-CH-UA`, `Sec-CH-UA-Mobile` and `Sec-CH-UA-Platform` come with every request. `Sec-CH-UA-Platform-Version`, which tells Windows 11 from Windows 10, comes only once the site asks for it with a response header, from the next request on: ```ruby response.headers["Accept-CH"] = "Sec-CH-UA-Platform-Version" ``` Without hints, every answer is the user agent’s, as in browser: `Agentry.new(user_agent, accept_language:)` answers exactly as before. Hints that only repeat the user agent reuse its cached detection; the others build a platform and a device for that request alone, never cached under the user agent. ### Internet Explorer and Safari **Internet Explorer.** In compatibility view, IE8 and later run as an older version. `version` and `full_version` return the navigator’s version; `msie_version` and `msie_full_version` return the engine’s. ```ruby agentry.version #=> 11 agentry.full_version #=> 11.0 agentry.msie_version #=> 7 agentry.msie_full_version #=> 7.0 agentry.compatibility_view? #=> true ``` **Safari.** iOS webviews and web apps are not Safari. Combine platform and WebKit detection instead: ```ruby agentry = Agentry.new("Mozilla/5.0 (iPad; U; CPU OS 3_2_1 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Mobile/7B405") agentry.safari? #=> false agentry.webkit? #=> true agentry.platform.ios? #=> true ``` ## Agentry Migration Switching is one line of the Gemfile. Renaming to `Agentry` can wait until Agentry 5, and a coding agent can do it for you. ### From browser Change one line of the Gemfile, nothing else: ```diff - gem "browser" + gem "agentry", require: "browser" ``` Agentry 4 still answers to the names browser used: `require "browser"`, `require "browser/aliases"` or `require: "browser/browser"` (and `browser/testing`, `browser/rails`) keep the meaning they had, define `Browser` as another name for `Agentry`, and the `browser` helper of Rails controllers and views still answers. Other `browser/*` files, such as `browser/middleware`, are gone: require one of those instead. Keep `require: "browser/browser"` if the app had it. A bare `gem "agentry"` leaves out the `Browser` constant, which then raises `NameError`. They are deprecated, so [move to the new names](https://levups.github.io/agentry/migration.html#names) when you can. Agentry is forked from browser 6.2.0 (`dd88933`), and every pull request diffs its answers against that version on the 20,328 user agents of [the corpus](https://levups.github.io/agentry/bots.html#corpus). #### What changes on its own - **More bots, from the day you switch.** 600 user agents of the corpus that browser took for humans are bots for Agentry: AI crawlers and assistants (`meta-externalagent`, `Perplexity-User`, `Claude-User`), ad and SEO crawlers, monitors, feed readers, scanners. Analytics that leave bots out count fewer visitors from that day, and anything gated by `bot?` (a session, a captcha, a cache) treats them as bots. Note the date of the switch next to your traffic charts. [Coverage per category](https://levups.github.io/agentry/bots.html#coverage). - **Names for bots.** 792 user agents that browser named `Generic Bot` get the name of their bot. No name browser gave changes. - **Client Hints in Rails.** The Rails helper and the middleware also read the Client Hints Chromium sends with every request. They only move an answer when they contradict the user agent: Chrome for Android asking for the desktop site becomes an Android tablet instead of a Linux desktop. Without the hints (`Agentry.new(user_agent, accept_language:)`, Safari, Firefox), nothing moves. See [Client Hints](https://levups.github.io/agentry/guide.html#client-hints). - **Nothing else.** No bot stops being one, and no browser, platform or device answer moves for a user agent alone. Test drivers (HeadlessChrome, Playwright, Puppeteer, Selenium) stay out of `bot?`, so the system tests of an app see what they saw. - **A cache, on by default.** Each process keeps the detection of its last 1,000 distinct user agents, about 1 MB. It empties itself when a matcher list or the bot list changes, so code that edits them keeps working. #### What you can add - `Agentry.cache_size = 5_000` to keep more user agents, `0` to turn the cache off. See [Options](https://levups.github.io/agentry/guide.html#options). - `agentry.bot.category`, `bot.ai?` and `bot.user_triggered?`, to tell an AI crawler from a search engine, a scanner or an assistant fetching a page for a person. See [Categories](https://levups.github.io/agentry/bots.html#categories). - `agentry.bot.verified?(request.remote_ip)`, to tell a real Googlebot (or Bing, OpenAI, Apple, Perplexity, DuckDuckGo crawler) from one that only claims to be. See [Verified crawlers](https://levups.github.io/agentry/guide.html#verified-crawlers). - `agentry.cloudflare`, to read the verified bot flag, category, bot score and country Cloudflare knows, through a Transform Rule. See [Cloudflare](https://levups.github.io/agentry/guide.html#cloudflare). - An `Accept-CH: Sec-CH-UA-Platform-Version` response header, so that `agentry.platform.windows11?` and the macOS and Android versions come from Chromium’s Client Hints. See [Client Hints](https://levups.github.io/agentry/guide.html#client-hints). ### Moving to the Agentry names | browser, deprecated in Agentry 4 | Agentry | | --- | --- | | `gem "browser"`, `require "browser"` | `gem "agentry"`, `require "agentry"` | | `require: "browser/browser"` (without Rails) | `require: "agentry/agentry"` | | `require "browser/aliases"`, `"browser/testing"`, `"browser/rails"` | `require "agentry/aliases"`, `"agentry/testing"`, `"agentry/rails"` | | `Browser.new(…)`, `Browser::Bot`, `Browser::Middleware`… | `Agentry.new(…)`, `Agentry::Bot`, `Agentry::Middleware`… | | the `browser` helper, `browser` in a middleware block | `agentry` | | `@browser`, in controllers, views, tests and middleware blocks (no warning) | `@agentry` | | `config.browser` | `config.agentry` | Each old name warns once per process, only where Ruby shows deprecations. Run the test suite with them shown to list what is left: ```shell RUBYOPT=-W:deprecated bin/rails test git grep -nP '\b(module|class)\s+Browser\b|\bBrowser\b(::|\.|\[|\)|,|\s*$)|require.*["\x27]browser|@browser\b|:browser\b|[(,=]\s*browser\b|\bbrowser:|\bbrowser(\.|&\.|\[)|config\.browser' ``` The grep also finds a local variable named `browser`: it can keep its name, only the helper call becomes `agentry`. Names that merely contain the word (`SamsungBrowser`, `:browser_automation`) are not old names. Three changes no warning shows: `@browser` becomes `@agentry`, in the same change as the `browser` calls around it (the `browser` helper fills and reads `@browser`, `agentry` only `@agentry`), class names read `Agentry::Chrome` even where code calls `Browser` (check `class.name` comparisons and marshalled objects), and the Railtie initializer is named `"agentry"`, so `before: "browser"` or `after: "browser"` no longer orders anything. Agentry 4 ships the same `browser/*` files as the browser gem, so it cannot share a bundle with it, for example through a dependency that still needs browser; Agentry 5 drops those files. ### From Agentry 3 Only the names change; every answer is the one Agentry 3 gave. One thing breaks: `require "agentry"` no longer defines `Browser`. An app with a bare `gem "agentry"` line that calls `Browser` either renames those calls to `Agentry` (see [the table](https://levups.github.io/agentry/migration.html#names)), or keeps them a while longer with `gem "agentry", require: "browser"`. An app that requires `browser`, `browser/browser`, `browser/aliases`, `browser/testing` or `browser/rails` sees only the deprecation warnings; the other `browser/*` files Agentry 3 shipped (`browser/middleware`, `browser/version`…) are gone. As when [coming from browser](https://levups.github.io/agentry/migration.html#names), class names read `Agentry::Chrome`, and the Railtie initializer is named `"agentry"`. ### With a coding agent The renaming is mechanical, which suits a coding agent such as Claude Code, Codex or Cursor. Paste this prompt in the app’s repository, then review the diff it leaves: ```plaintext Migrate this app from the browser gem (or Agentry 3) to Agentry 4 without changing its behavior. Rules: https://levups.github.io/agentry/migration.html#names 1. In the Gemfile, replace gem "browser" with gem "agentry" (keep any require: option, renamed as below), then run bundle install. 2. Rename the old names to the new ones: - require "browser" -> require "agentry"; "browser/browser" -> "agentry/agentry"; "browser/aliases", "browser/testing", "browser/rails" -> "agentry/aliases", "agentry/testing", "agentry/rails". - The Browser constant -> Agentry: Browser.new, Browser::Bot, Browser::Middleware, Browser.cache_size... - In Rails controllers, views, helpers and Agentry::Middleware blocks, calls to the browser helper -> agentry. - config.browser -> config.agentry. - The @browser instance variable the gem filled -> @agentry, in controllers, views, tests (instance_variable_set(:@browser, ...), assigns(:browser)) and Agentry::Middleware blocks. - Stubs of the browser helper in tests (stubs(:browser), receive(:browser)) -> stubs of agentry. 3. Leave alone: local variables, instance variables or methods the app itself names browser, such as a test driver (only what the gem's helper filled or returned changes), names that merely contain the word (SamsungBrowser, :browser_automation, "Samsung Browser"), fixtures, recorded data, translations and comments that talk about web browsers. 4. Run the test suite with RUBYOPT=-W:deprecated. Fix each "[agentry] ... deprecated" warning it prints, and run it again until none is left and the suite is as green as before. 5. Grep for leftovers: git grep -nP '\b(module|class)\s+Browser\b|\bBrowser\b(::|\.|\[|\)|,|\s*$)|require.*["\x27]browser|@browser\b|:browser\b|[(,=]\s*browser\b|\bbrowser:|\bbrowser(\.|&\.|\[)|config\.browser' and check each hit by hand, including class.name comparisons ("Browser::Chrome" is now "Agentry::Chrome") and after: "browser" initializers. 6. Do not change any answer the app relies on, add features or refactor. Show me the diff and the list of files you changed. ``` ### Going back Remove the calls to what browser does not have (`cache_size=`, the `headers:` argument, `bot.category`, `bot.ai?`, `bot.user_triggered?`, `bot.verified?`, `cloudflare`), or they raise `NoMethodError`. Bring back the browser names of [the table](https://levups.github.io/agentry/migration.html#names), then put `gem "browser"` back in place of `gem "agentry"`. The bots of the weekly sync go back to being humans, so the analytics shift reverses on that day. ### Agentry 5 Agentry 5 removes every old name of [the table](https://levups.github.io/agentry/migration.html#names): the `browser/*` require paths, the `Browser` constant, the `browser` helper, `browser` in a middleware block, `@browser` and `config.browser`. An app whose test suite prints no `[agentry]` deprecation under `RUBYOPT=-W:deprecated`, and that no longer uses `@browser`, upgrades without a change. ## Agentry Benchmarks One benchmark, the shape of a web request, run against browser and Agentry, with and without YJIT. The table below is rendered from `docs/benchmarks.json`, which the benchmark writes. ### Per request Ruby 4.0.9, Apple M4 Max, measured on 2026-11-04: browser 6.2.0 at `dd88933`, Agentry 1.0.0 at `e8bdc86`. Each figure is the median of 7 runs. | User agents | JIT | browser 6.2.0 µs | Agentry 1.0.0 µs | Faster | Objects | | --- | --- | --- | --- | --- | --- | | 2000 distinct user agents | YJIT | 125.84 | 25.36 | 5.0× | 245 → 52 | | 20 user agents, repeated | YJIT | 114.02 | 2.01 | 56.7× | 217 → 12 | | 2000 distinct user agents | none | 141.73 | 42.05 | 3.4× | 245 → 52 | | 20 user agents, repeated | none | 133.64 | 3.79 | 35.3× | 217 → 12 | `bench/request.rb` asks each user agent what a web request asks: the browser’s name and full version, `bot?` and the bot’s name, the platform’s name and version, `device.mobile?`, `device.tablet?` and the first accepted language. It runs over 2,000 distinct user agents sampled from [the corpus](https://levups.github.io/agentry/bots.html#corpus), more than the cache holds, so that row measures a user agent seen for the first time; then over 20 of them repeated, as a site’s regular visitors are. ### Why it is faster **A cache per user agent.** Every answer but Accept-Language depends on the user agent alone. Each process keeps, for the last 1,000 distinct user agents, the browser class `Agentry.new` chose, the platform, the device and the bot answers, computed lazily; the last 1,000 Accept-Language headers are parsed once too. About 1 KB per user agent, so 1 MB per worker. `Agentry.cache_size` changes the size, `0` turns it off, and changing a matcher list or the bot list empties it. **Bots in one walk.** browser matches the user agent against its bot list one entry at a time. Agentry walks a byte trie of the list, five times longer since the weekly sync, once per user agent, and the built-in bot matchers no longer build a second `Agentry` object for the same user agent. **Less work on a new user agent.** `Agentry.new` skips, unbuilt, a browser whose required pattern fails, and platform and device matchers are built only when a request asks for them. ### Reproduce Run it without JIT and with `--yjit`, for upstream and for Agentry, one at a time on an otherwise idle machine. `path/to/upstream` is a plain clone of [fnando/browser](https://github.com/fnando/browser) at `dd88933`. ```sh ruby --yjit -I path/to/upstream/lib bench/request.rb ruby --yjit -I lib bench/request.rb ``` `--write` records the run in `docs/benchmarks.json`, with the gem, its commit, the Ruby, YJIT, the machine and the date, replacing the previous run of that gem and JIT. `rake bench` runs all four and renders this page and the home page from the file: ```sh bundle exec rake bench UPSTREAM=path/to/upstream ```