r/n8n 线程抱怨搜索 API 返回原始 HTML 打破了令牌限制或剥离了太多上下文。本教程走中间路线:通过 Scavio 构建结构化片段,仅针对前 1-2 个点击进行全页摘录。
前置条件
- Python 3.10+
- Scavio API 密钥
操作指南
步骤 1: 搜索返回 10 个键入的片段
每个片段可容纳大约 100 个标记。
Python
import requests, os
API_KEY = os.environ['SCAVIO_API_KEY']
def snippets(q):
r = requests.post('https://api.scavio.dev/api/v1/search',
headers={'x-api-key': API_KEY}, json={'query': q}).json()
return r.get('organic_results', [])[:10]步骤 2: LLM 选择前 1-2 篇来完整阅读
比全部 10 个都便宜。
Python
import anthropic
client = anthropic.Anthropic()
def pick(q, snips):
msg = client.messages.create(model='claude-sonnet-4-6', max_tokens=200,
messages=[{'role':'user','content':f'Q: {q}. SNIPPETS: {snips}. Return indices of the top 2 to read fully.'}])
return msg.content[0].text步骤 3: 将这些页面提取为 markdown
Markdown 比 HTML 更便宜。
Python
def fetch(url):
r = requests.post('https://api.scavio.dev/api/v1/extract',
headers={'x-api-key': API_KEY}, json={'url': url, 'format': 'markdown'}).json()
return r.get('markdown', '')[:5000] # token-budget the page步骤 4: 撰写最终答案
片段提供广度,整页提供深度。
Python
def answer(q):
snips = snippets(q)
picks = [int(i) for i in pick(q, snips).split(',') if i.strip().isdigit()]
deep = [fetch(snips[i]['link']) for i in picks[:2]]
return {'snippets': snips, 'deep_reads': deep}步骤 5: 代币数学
10 个片段 ≈ 1K 代币; 2 个修剪后的页面 ≈ 8K 令牌;总上下文 ≈ 9K 令牌 — 适合任何 200K 上下文模型。
Text
// Token budget: well under 16K even for a 32K-context model.Python 示例
Python
# Per question: 1 search + 2 extracts = 3 credits = $0.013. Plus LLM token cost.JavaScript 示例
JavaScript
// Same pattern in TS.预期输出
JSON
Per question, the agent has 10 snippets and 2 full reads in its context. No raw HTML, no manual cleaning, no token overflow.