# MediSetu Lens **Cheapest generic medicine across three pharmacy portals that publish no API — plus the measured proof that compiling web knowledge into commands actually pays.** Built on [Webcmd](https://webcmd.dev/), self-learning browser infrastructure for AI agents. --- ## The problem A branded strip of paracetamol costs ₹31.50. The identical generic costs ₹5.50. That is a **5.7× spread on the same molecule**, and no public API anywhere will tell you about it, because pharmacy portals do not publish APIs. An AI agent can find it out. But a browser agent rediscovers the same three sites on every single run — navigate, screenshot, snapshot the DOM, reason afresh — which is slow, expensive, and breaks the moment a page shifts. Webcmd's answer is to keep what the agent learned and compile it into a deterministic command. This project puts a number on how much that is worth, and then guards it. ## Three things in this repo | Part | What it does | | --- | --- | | **The portals** | Three simulated pharmacy sites, each with a deliberately different surface | | **The plugin** | Four Webcmd commands that read them, one per execution strategy | | **The Lens** | A dashboard that measures cost live, and a sentinel that catches silent rot | ### Why the portals are local A stage demo cannot depend on a third party's uptime, rate limits, or terms of service. `portal/` is a faithful stand-in for the real thing — no API, inconsistent markup, a login wall, artificial latency — while staying fully deterministic and offline. It also lets us **break a site on purpose**, which is the whole point of the sentinel demo. Each portal exercises a different Webcmd strategy, on purpose: | Portal | Surface | Strategy | Why it is hard | | --- | --- | --- | --- | | Apex Pharmacy | Server-rendered HTML table | `PUBLIC` | Readable, but markup changes between releases | | Nova MedStore | JS-hydrated cards | `INTERCEPT` | Zero prices in the HTML; data arrives over an undocumented XHR gated on a client header | | Jan Generic Depot | Login-walled dashboard | `COOKIE` | Cheapest generics, invisible unless authenticated | Nova is the interesting one. Its prices genuinely cannot be read by a plain HTTP fetch. An agent has to watch a real browser once to discover the internal endpoint and the header it demands — and *that single discovery* converts a browser-mandatory surface into a plain HTTP call. It is the largest cost saving in the project, and it is exactly the kind of knowledge Webcmd exists to retain. ### The commands ```bash webcmd medisetu apex paracetamol # PUBLIC parse the rendered table webcmd medisetu nova paracetamol # INTERCEPT replay the internal call webcmd medisetu depot paracetamol # COOKIE authenticate, then read webcmd medisetu compare paracetamol # all three, concurrently ``` `compare` is the payoff — one command answering a question no single API can: ``` rank chain brand type price vs best 1 jan-generic-depot Paracetamol IP 650 generic ₹5.50 — 2 nova-medstore Paracetamol IP 650 generic ₹8.40 +₹2.90 3 apex-pharmacy Paracetamol IP 650 generic ₹9.50 +₹4.00 4 nova-medstore Dolo 650 branded ₹28.90 +₹23.40 5 apex-pharmacy Calpol 650 branded ₹30.00 +₹24.50 6 apex-pharmacy Dolo 650 branded ₹31.50 +₹26.00 ``` All four commands share one output contract, so any agent that can read one can read all of them. A portal that goes down degrades to a recorded error for that chain rather than failing the whole command, because a partial answer is more useful to an agent than an exception — provided you *know* it is partial, which is what the sentinel is for. ## Measured results Webcmd's own site advertises roughly a 90% token reduction, but labels that chart a *"directional placeholder, to be replaced with observed benchmark runs."* **This is the observed run.** Measured locally, `paracetamol`: | Metric | Layer 0 (fresh exploration) | Layer 3 (compiled command) | Change | | --- | --- | --- | --- | | Context bytes | 15,461 | 1,895 | **8.2× smaller** | | Est. tokens | 3,865 | 474 | **87.7% reduction** | | Round trips | 7 | 1 | **7× fewer** | | Wall clock | 2,545 ms | 1,193 ms | 2.1× faster | ### Read these numbers honestly - **`est_tokens` is context bytes ÷ 4.** It is not a tokenizer, and it is not real LLM billing. Measuring that properly needs a live model and an API key; claiming it without one would be dishonest. What *is* measured directly is context payload — the bytes an agent must pull into its window — which is what drives the cost. - **The baseline is deliberately conservative.** It fetches each page exactly once and assumes flawless first-pass parsing: no wrong turns, no retries, no screenshots. It also charges nothing for the browser render Nova genuinely requires. A real browser agent does considerably worse, so the true saving is larger than reported. Erring in that direction is the point. - **The wall-clock win is understated** because roughly a second of the compiled run is Node and CLI cold start, a fixed cost that does not grow with task size. Token reduction is the honest headline; speed is a side effect. ## The drift sentinel A compiled command is worth nothing once it silently stops telling the truth. When a portal ships a redesign, a selector-based adapter does not crash — it returns zero rows, or rows with null prices. Downstream, an agent reports "no results found" and nobody notices for weeks. `lens/sentinel.js` validates a contract rather than diffing snapshots: 1. the command exits cleanly 2. it returns at least one row 3. required fields are present and non-null 4. prices are plausible, and price never exceeds MRP 5. **aggregate commands cover every portal they claim to** Rule 5 exists because this sentinel caught a real bug in our own code. When Apex drifted, `compare` kept returning rows from the other two portals and reported itself perfectly healthy — a partial answer wearing the costume of a complete one, which could recommend a price that is not actually the cheapest. Row counts alone would never have caught it. Now: ``` [DRIFT] medisetu apex Command succeeded but returned zero rows. The page loaded and the parser matched nothing — the markup this adapter targets has changed. [ ok ] medisetu nova 2 rows, all required fields present and prices plausible. [ ok ] medisetu depot 1 rows, all required fields present and prices plausible. [DRIFT] medisetu compare Rows returned but the contract is violated: 1 of 3 portals contributed nothing (apex-pharmacy), so this ranking is incomplete and may recommend a price that is not the cheapest. ``` Exit code is non-zero on drift, so this drops straight into CI or a cron job. ## Run it Requires Node 20.6+ and Webcmd. ```bash npm install -g @agentrhq/webcmd webcmd plugin install file:///plugin node portal/server.js # terminal 1 -> http://localhost:4173 node lens/server.js # terminal 2 -> http://localhost:4180 ``` Then open **http://localhost:4180**. ```bash node lens/runner.js metformin # measured comparison, in the terminal node lens/sentinel.js metformin # health check, exit 1 on drift ``` > **Security note.** Both servers are unauthenticated and bound to loopback. The dashboard exposes controls that mutate portal state. They are presentation tools — do not bind them to a public interface or port-forward them. ## Demo script, 3 minutes **1. The hook, 20s.** Open the dashboard. "A strip of branded paracetamol is ₹31.50. The identical generic is ₹5.50. No API in the world will tell you that, because pharmacy portals don't have APIs." **2. Let a judge choose, 30s.** Ask someone to name a molecule from the list and type it in. Hit **Run both ways**. This is the single most important moment — it proves live parameterisation and kills any suspicion of a hardcoded recording. **3. The measurement, 40s.** Point at the two panels. Same question, same data, both paths. 3,865 estimated tokens and 7 round trips versus 474 and 1. Then say the honest part out loud: *"our baseline is deliberately generous to the old way, and we still cut context by 88%."* Volunteering that earns more credibility than any number. **4. Break it, 50s.** Hit **Toggle portal redesign**. Open Apex in a tab — it looks completely normal, prices all present, a human sees nothing wrong. Back to the dashboard, hit **Run sentinel**. Apex flags DRIFT, and `compare` flags an incomplete ranking. "The adapter didn't crash. It went quietly wrong, and that's the failure mode that actually hurts you in production." **5. Close, 20s.** "Webcmd makes web knowledge reusable. We measured what that's worth on surfaces that have no API, and we built the immune system that keeps it honest as those sites change." ## What is not built Stated plainly, because a judge will ask and a straight answer beats a dodge. - **Auto-repair.** The sentinel detects and diagnoses drift; it does not yet patch the adapter and open a PR. That is the natural next layer and the honest edge of the work. - **Real token accounting.** Needs a live model in the loop. See the caveats above. - **Live third-party portals.** Deliberate. The adapters are written against the same selector-and-XHR patterns real portals use, so the technique transfers, but the demo target stays local so it is deterministic and breakable on command. ## Layout ``` portal/ three simulated pharmacy portals, zero dependencies data.js catalogue, generic vs branded pricing server.js the three surfaces, login wall, drift switch plugin/ the Webcmd plugin apex.js PUBLIC nova.js INTERCEPT depot.js COOKIE compare.js all three, concurrently lib/portal.js shared parsing and the output contract lens/ runner.js measures layer 0 vs layer 3 sentinel.js contract validation and drift detection server.js the dashboard ``` Everything runs on Node's standard library. No build step, no bundler, nothing to install beyond Webcmd itself — so nothing can fail during a demo.