Ch45: 挑战深读⑦——半 Agentic、轮次护栏与 Token 五源
Part 10 · 合并 challenges #8 + #16 · 主读:Ch12 Agentic RAG · Ch38 最后一公里
结论先行:#8 每多一轮 Agentic 检索 ≈ 规划 LLM + 检索 + Rerank + 更长 context;#16 生产必须在 质量天花板与成本地板 间设分级——简单 FAQ 走快速模式,多跳/低分才二轮。半 Agentic(Rerank < τ 才 rewrite)是 WeKnora 与 MCP 外溢场景的默认;硬顶 max_rounds=2(不超过 3)+ Token 五源监控 比「全 Agent demo」更接近可上线 PR。
一、日常类比:研究助理的图书馆次数
派助理查资料:每多进一次书库 = 再花「想查什么」+「走路」+「精读」的钱(#8)。若助理「总觉得不够」查 10 次,账单和等待都失控——需要 最多进 N 次 的规矩(max_rounds)。同时整栋图书馆的电费还来自:新书入库扫描(embed)、精读员(rerank)、写报告(生成)、助理反复进出(Agent)、期末统考(RAGAS)——#16 的五源(见下表)。
二、Agentic 成本公式(#8)
单次轮次 ≈ C_plan + C_retrieve + C_rerank + C_context_tokens
N 轮 ≈ N × 上式(context 常递增)
WeKnora 经 MCP 暴露检索——成本在 外部 Agent 编排轮次 与 WeKnora QPS 两侧叠加 Ch12。大师 PR 应同时暴露 agentic_rounds_total 与租户级 token 估算。
三、半 Agentic 与护栏
| 护栏 | 作用 | 关联 |
|---|---|---|
max_rounds=2 |
硬顶防死循环 | #8 |
| Rerank < τ 才二轮 | 半 Agentic 触发 | Ch38 |
| 递减 top_k | 第 2 轮缩小检索面 | #16 |
| 查询 rewrite cache | 同 rewrite 不重复检 | 延迟 |
| FAQ 路由 | 单事实不走 Agent | Token |
| 快速模式 | 双路、小 top_k、可选无图谱 | Ch43 |
伪代码:
results = retrieve(q)
if max_rerank_score(results) < TAU and rounds < max_rounds:
q2 = llm_rewrite(q, results) # 控制 rewrite prompt 长度
results = merge(results, retrieve(q2, top_k=top_k2))
answer = generate(q, truncate(results, max_context_tokens))
全 Agentic 适合 demo 与多跳研究集;生产默认 τ 调参 + max_rounds=2。
四、Token 五源(#16)
| 来源 | 量级感(万级日活) | 优化杠杆 |
|---|---|---|
| Embedding 摄入 | 频繁更新时累积 | 语义 diff 跳过 re-embed Ch42 |
| Rerank | 单次便宜 × QPS 大 | RRF 先粗排缩小 N |
| LLM 生成 | 通常最大头 | top_k、chunk 限长、引用压缩 |
| Agentic 多轮 | 单次 3–5× | 半 Agentic、max_rounds |
| RAGAS 评估 | 离线批量 | smoke-10 而非全库 |
| 决策 | 质量↑ | 通常值得? |
|---|---|---|
| top_k 5→20 | 更多 context | 复杂题有时 |
| 上 Rerank | +Cross-Encoder | 几乎总是 |
| chunk 500→200 | 更细粒度 | 精确查值得 |
| Agent 3 轮 | 多跳 | 简单题 否 |
五、Query 分级产品模式
轻量分类(规则或 1 次小模型)
├── FAQ / 单事实 → top_k=5, 无 Agent, 可选无 rerank
├── 标准 → 默认三路 + rerank
└── 多跳 / 低分 → 半 Agentic + rewrite( rounds ≤ max_rounds )
预算触顶降级链:缩小 top_k → 关图谱 → 关 rerank → 拒答/转人工。
六、grep / 配置锚点
rg -n "max_round|MaxRound|MCP|rewrite" internal/ mcp/ --glob "*.{go,ts,md}" | head -30
MCP Tool 定义与 OAuth 大 PR 慎选——见 Ch32 大师通关 · case-1774 三层 API。
七、实验
从 Ch20 / smoke-10 抽 10 条多跳或低分敏感题(可含 zh-002 类),对比:
| 模式 | faithfulness | 总 token | P95 延迟 |
|---|---|---|---|
| 1 轮固定 | baseline | 低 | 低 |
| 半 Agentic τ=0.5 | ? | 中 | 中 |
| 2 轮无条件 | ? | 高 | 高 |
成功标准:2 轮仅在 faithfulness 有 >5% 相对增益 且 token < 1.8× 时默认启用半 Agentic;否则文档推荐保守 τ。
八、贡献方向
- 默认
max_rounds=2+ admin 文档与 env 示例 - Prometheus:
agentic_rounds_total、context_tokens_histogram - Admin:租户 Agentic 占比 + 预算告警 → 自动切快速模式
max_context_tokens硬顶 + 截断策略(保留高分 chunk 优先)- MCP 文档:调用方如何实现半 Agentic(链 Ch12)
章末自检
- 列 Token 五源 并指出生产最大头通常是哪一项
- 写半 Agentic 触发条件(一句话)与
max_rounds推荐值 - 说清 MCP 场景下 谁承担 Agent 编排成本(调用方 vs WeKnora)
- 设计预算触顶时的 三级降级链(≥3 步)