My Pi Agent gets dumb after some work
On top of the default prompt, I only added a fairly small APPEND_SYSTEM.md covering writing style, when to use the subagent extension, how to browse the web, and so on. I didn't give the coding agent a separate browser tool. The system prompt just said something like "use curl to look up API references or other material if you need to." It turned out that every time it needed something from the web, it pulled the whole giant HTML page and read it as-is.
I noticed every web search burned a huge number of tokens, and after browsing, the agent felt dumber and couldn't even do the job I originally gave it. All those tags, CSS, and script blobs had to go. So I compared ways to shrink HTML before feeding it in.
also, I was thinking it would be great to apply this method to Proval. (You probably never heard about it. It's self hosted LLM code review app I'm building)
My criteria
Why I insist on the shell
It lasts
- It's mature, it keeps working over time, and it's easy to set up again when I switch models.
- The Unix file system and the shell are an extremely solid abstraction.
- Skills and plugins go stale over time, but sizable open-source CLI tools keep getting maintained. Updating is a single
brew upgradeorapt upgrade.
Models already know it well
- Models already know
curland|pipelines from pretraining. No need to write long usage instructions in the system prompt; a line or two is enough. - I hear that these days, to push agent performance and benchmark scores, models are drilled hard on terminal usage with reinforcement learning in terminal environments. To raise metrics like Terminal Bench and real-world feel, the latest models are especially good with the shell.
It's light
- If you use agent browser tools like Playwright or browser-use...
- They need Chrome running in the background, which eats memory and is heavy.
- They nudge the agent to surf around following links.
- Their usage has to go into the system prompt, so the prompt gets longer and burns more tokens.
- So I decided to post-process
curloutput, or use a CLI program that fetches and cleans up web pages on its own. - Honestly, if resources were the only issue, I could spin up a camofox docker instance on my homelab, but I made the call combining all the reasons above.
That said, curl isn't the answer
Get it as Markdown
If an article or doc can serve Accept: text/markdown, that's pretty much the best option.
You get all the formatting with no loss, and quite a few pages you'd use as references in dev work support returning markdown. Or JSON, and so on.
The dynamic page trap
CSR pages and dynamic pages are very likely to be hard to get fully with curl. Just use a headless browser like Chromium or Camofox. There are plenty of tools optimized for agent browser use, like Playwright.
What I compared
Tools
- curl: Fetches HTML, CSS, and JS untouched. Used as the baseline.
- Raw markdown: Request with
curl -H 'Accept: text/markdown', or append.mdto the URL, to get the markdown the site serves directly. For a GitHub repo, the README onraw.githubusercontent.comcounts here. - html2text: A Python tool that turns HTML into text. Piped after
curl. - pandoc: A tool that converts all kinds of documents like Word and PDF, and it can also turn HTML into markdown.
- lynx: A very old text-only web browser. It can parse HTML and dump clean text.
- trafilatura: A tool specialized for web scraping that extracts only the main content from HTML. It can even take a URL and fetch it itself.
How I measured
curl -sL -A 'Mozilla/5.0' "$URL" -o raw.html
html2text < raw.html
pandoc -f html -t markdown_strict --wrap=none raw.html
trafilatura --markdown --links < raw.html
lynx -dump "$URL" # lynx gets the URL directly. Piping from curl breaks relative links into file:///.
lynx -dump -nolist -force_html raw.html # without the link list, relative links don't matter, so I used the saved HTML.
curl -sL -H 'Accept: text/markdown' "$URL"I downloaded the HTML once, saved it, and fed the same file to every tool. Tokens were counted with tiktoken-cli using tiktoken --model gpt-4o - < out.txt | wc -l. The counts may not be exact, but they should be close enough.
Pages
I picked a quick variety of pages I look at during development. I'll attach the parsed output later too; this is just personal research, so I hope the site owners won't come after me for copyright.
| Page | Type | What to look for |
|---|---|---|
| opencode docs | Dev docs that serve raw markdown | Difference between the original and converted output |
| MDN Array | Dev docs heavy on code and tables | Whether code blocks, tables, and links survive |
| GitHub trafilatura | Repository main page | Difference between the raw README and converted output |
| HN thread | Discussion with 380 comments | Whether comments survive, how table layout is handled |
| Ars Technica article | News article | Whether it extracts just the body from ads and recommended articles |
Measurements
Byte count, token count, and each as a ratio of curl. Click a tool name to see the actual output.
opencode docs
opencode's model configuration docs. Send Accept: text/markdown and the site returns the raw markdown as-is.
| Tool | Bytes | Tokens | Notes |
|---|---|---|---|
| curl | 148,826 (100%) | 47,473 (100%) | Baseline |
| html2text | 26,252 (18%) | 5,483 (12%) | No link URLs, broken tables |
| pandoc | 46,891 (32%) | 16,290 (34%) | Structure intact, but tables left as HTML, lots of base64 icons |
| lynx | 28,245 (19%) | 7,346 (15%) | No heading markers, link list is mostly sidebar |
| lynx -nolist | 23,256 (16%) | 5,624 (12%) | No link URLs, line wrapping cuts code mid-line |
| trafilatura | 20,230 (14%) | 5,016 (11%) | Code, tables, links intact, but most headings missing |
| Raw markdown | 21,582 (15%) | 5,240 (11%) | No loss, no noise |
MDN docs
MDN's JavaScript Array reference. Lots of code examples and tables, so the difference shows in whether code blocks and tables survive.
| Tool | Bytes | Tokens | Notes |
|---|---|---|---|
| curl | 245,807 (100%) | 66,916 (100%) | Baseline |
| html2text | 46,775 (19%) | 11,827 (18%) | No link URLs, spaces turned into _ |
| pandoc | 89,833 (37%) | 26,443 (40%) | All content kept, but mixed with CSS and tags |
| lynx | 99,847 (41%) | 26,343 (39%) | All content kept, but over half is menus and link list |
| lynx -nolist | 48,576 (20%) | 12,133 (18%) | No link URLs, includes sidebar menu |
| trafilatura | 57,810 (24%) | 15,455 (23%) | Headings, code blocks, tables kept. Last section missing |
GitHub repository
The trafilatura repository's main page. The only real content is the README; the rest is GitHub UI. The raw README is available from raw.githubusercontent.com.
| Tool | Bytes | Tokens | Notes |
|---|---|---|---|
| curl | 298,800 (100%) | 114,670 (100%) | Baseline |
| html2text | 12,892 (4%) | 2,908 (3%) | No link URLs, duplicated sidebar |
| pandoc | 141,193 (47%) | 68,166 (59%) | Headings kept, code examples split by tags, tons of base64 badges |
| lynx | 25,961 (9%) | 7,173 (6%) | README kept, link numbers mixed into the text |
| lynx -nolist | 12,691 (4%) | 2,918 (3%) | No link URLs, starts with GitHub menus |
| trafilatura | 7,528 (3%) | 1,858 (2%) | README body intact, but headings missing |
| Raw markdown | 9,293 (3%) | 2,408 (2%) | README only, no repository info |
HN thread
An HN discussion with 380 comments. The whole layout is built with <table>, and every comment has vote and reply links.
| Tool | Bytes | Tokens | Notes |
|---|---|---|---|
| curl | 545,610 (100%) | 185,328 (100%) | Baseline |
| html2text | 149,063 (27%) | 32,350 (17%) | No link URLs, spaces turned into _ |
| pandoc | 595,770 (109%) | 202,251 (109%) | Comment hierarchy kept, but HTML tags left as-is |
| lynx | 323,266 (59%) | 109,258 (59%) | Authors kept, reply hierarchy lost, nearly half is link list |
| lynx -nolist | 117,611 (22%) | 29,751 (16%) | Authors kept but reply hierarchy lost, lots of UI text |
| trafilatura | 90,278 (17%) | 21,420 (12%) | Authors dropped, first comment dropped |
Ars Technica article
A news article from Ars Technica. The body has to be picked out from between ads and recommended articles.
| Tool | Bytes | Tokens | Notes |
|---|---|---|---|
| curl | 158,957 (100%) | 62,920 (100%) | Baseline |
| html2text | 6,384 (4%) | 1,575 (3%) | Body kept, no link URLs, duplicated menus and captions |
| pandoc | 36,303 (23%) | 17,238 (27%) | Everything kept, over half is base64 |
| lynx | 12,200 (8%) | 3,475 (6%) | Body kept, over half is link list |
| lynx -nolist | 6,844 (4%) | 1,615 (3%) | Body, author, captions kept. No link URLs |
| trafilatura | 1,356 (1%) | 285 (<1%) | Only half the body left, title and author missing |
Results
| Tool | Tokens | Context |
|---|---|---|
| Raw markdown | Among the smallest, close to trafilatura | No loss, no noise. Only available when the site provides it |
| trafilatura | Smallest (9%) | Cleanest when it works, but silently drops headings, body, and authors |
| lynx -nolist | Close to trafilatura (11%) | No content loss. Menus and UI text mixed in, no link URLs |
| html2text | Close to trafilatura (11%) | No link URLs. Spaces become _, mangling code and text |
| lynx | Large (32%) | No content loss. Most of the size is the link list |
| pandoc | Largest (69%). Bigger than the raw HTML on HN | Almost no content loss. Lots of base64, CSS, and tag leftovers |
What each tool showed
- Raw markdown: Gets all the needed information intact at minimal token cost. If it's available, always use it.
- trafilatura: Cleanest on pages where it works well. But it threw away structure like headings inside buttons, pages with multiple body containers, and some of the comments, and unless the agent looks at the original, it has no way of knowing something is missing.
- html2text: Small, but drops every link URL and even mangles code, like
slice(0,_-1). - lynx -nolist: Some unneeded stuff gets mixed in, but almost all the needed information survives. The model can skip the noise, and long code lines getting cut is fixed with
-width. - lynx: All content is kept, but in some cases the link list at the end is most of the size. On HN it had over 3,000 links.
- pandoc: All the information is there, but unconverted base64,
<style>, and<span>are left in, making it expensive and hard to read.
Getting blocked happens a lot
I tried quite a few other pages too. Some got blocked by Cloudflare, and CSR pages just don't work. That seems hard to get around without a browser.
Putting it to use
I decided to use lynx -nolist as the default. Its token count is almost the same as trafilatura's (11% vs 9%), and it doesn't silently drop content. The model skips mixed-in noise on its own, but it can't know about content that's missing. The missing link URLs can be handled by calling lynx again only when needed and cutting out just that one link.
On a Mac, you can install it right away with brew.
brew install lynxMy Pi Agent web browsing workflow
Try markdown first and save to a temp file
If it's markdown, read it directly; otherwise read it with
lynx -dump -nolistIf you need a link URL, rerun without
-nolistand cut out only the lines you need with a pipeline. Searching the body for the link text gives a number like[12]Installation, and finding that number's line in the list at the end gives the absolute URL.lynx -dump -width=1000 "$URL" | rg -o '\[[0-9]+\]Installation' # [12]Installation lynx -dump -width=1000 "$URL" | rg '^ *12\. ' # 12. https://...If that still doesn't work, read the raw HTML and use rg to find only the parts you need. Pages rendered only by JavaScript won't work here either, so at that point the agent stops and reports.
Here's what I put in the system prompt.
# WEB BROWSING & TOKEN OPTIMIZATION
1. fetch and save source. for docs, try a machine-readable source first
- `curl -fsSL --max-time 20 -A 'Mozilla/5.0' -H 'Accept: text/markdown, text/html' -o /tmp/pi/<slug>.raw`
- Accept as markdown only if 2xx, body does not start with `<!DOCTYPE`/`<html`, and size > 100B.
2. Otherwise read `lynx -dump -nolist` with pipeline
3. If you need a link's url then rerun without `-nolist` and grep, don't read all.
- `rg -o '\[[0-9]+\]Link text'` -> [N]
- `rg '^ *N\. '` -> URL
4. If you find this isn't going to work, then try something else (ex. read raw html)Seems to work pretty well. If web browsing makes it into Proval, I plan to use this same approach there.