Click any words you think contain an error. Click again to unmark.
Raw 80KB HTML pasted verbatim into the assistant turn is the bug, the tool wrapper should parse, extract, and cap before anything reaches the prompt.
Imagine asking a friend to summarise a long Wikipedia article and they hand you the whole printout including the sidebar, the ads, the page footer, the citation list, and the edit history. You wanted the answer; you got a stack of paper. Worse, every time you ask a follow-up they hand you the same stack again because they keep it in their lap. The tool wrapper is the friend's job, read once, take out the useful paragraph, throw the rest away, and only hand you the small note.
Detailed answer & concept explanation~5 min readEverything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example.
Everything important, quickly.
7 min: identifying the bug span, why HTML is mostly chrome, the parse extract cap reference pattern, and how the fix preserves prompt caching and lost-in-the-middle behaviour.
# Before: raw HTML to context (the bug in the question)
result = http_get(url).text # 80KB of HTML
messages.append({"role": "tool", "content": result})
# After: parse, extract, cap, reference
from readability import Document
import hashlib
MAX_EXCERPT_CHARS = 800 # roughly 200 tokens
def search_wrapper(url: str, store) -> dict:
raw = http_get(url).text
doc = Document(raw)
body = doc.summary(html_partial=False)
excerpt = body[:MAX_EXCERPT_CHARS]
ref_id = hashlib.sha1(url.encode()).hexdigest()[:10]
store.put(ref_id, raw)
return {
"title": doc.title(),
"url": url,
"excerpt": excerpt,
"truncated": len(body) > MAX_EXCERPT_CHARS,
"ref_id": ref_id,
}Real products, models, and research that use this idea.
- Anthropic's web_search tool returns title, URL, and a short snippet, never the full HTML, exactly to avoid this failure.
- Perplexity's API returns structured search results with bounded fields per source.
- Mozilla Readability and trafilatura are the standard parsers production teams use to extract main content before injection.
- Claude Code's WebFetch tool runs an LLM-summarisation pass on the page before the result reaches the agent loop.
What an interviewer would ask next. Try answering before peeking at the approach.
QHow would you handle a tool that returns JSON with deeply nested objects rather than HTML?
QWhat if the agent genuinely needs to read the full page for one specific call?
Don't say thisRed flags and common mistakes that signal junior thinking. Click to expand.
Red flags and common mistakes that signal junior thinking. Click to expand.
Treating the model as the place to fix verbose tool outputs, the model has no control over its own inputs; the tool wrapper does.
The night-before-the-interview bullets. Scan these on the way to the call.
Primary sources. Skim if you want the original framing.
Same topic, related formats. Practice these next.