AI Skills AI技能 13h ago Updated 8h ago 更新于 8小时前 49

LAI #139: Fewer Tokens Cost Us More LAI #139:更少的Token反而让我们付出更多代价

Chat history is often not the primary context bottleneck; old retrieval results and tool outputs can dominate token usage, contributing up to 100k tokens per turn Prompt caching economics can make summarization counterproductive: keeping full history retained ~87% cache discount while compaction reduced tokens by 41% but doubled cost Capping tool outputs at a stable size reduced cost per turn by 38% with no measurable quality loss, outperforming summarization-based approaches Hybrid retrieval (B 上下文窗口成本主要由检索和工具输出填充,而非对话历史,优化前需先测量实际占用来源 保留完整历史配合缓存折扣比摘要压缩更经济,后者会破坏缓存前缀导致成本翻倍 工具输出稳定截断比摘要重写更有效,可在不损失记忆质量的前提下降低38%单轮成本 混合检索需保留关键词与语义检索的独立强候选再合并,过早融合会导致精确匹配丢失 约束类型(窗口/成本/质量)决定上下文策略选择,复杂方案(如GraphRAG)未必优于简单混合检索

65
Hot 热度
75
Quality 质量
70
Impact 影响力

Analysis 深度分析

TL;DR

  • Chat history is often not the primary context bottleneck; old retrieval results and tool outputs can dominate token usage, contributing up to 100k tokens per turn
  • Prompt caching economics can make summarization counterproductive: keeping full history retained ~87% cache discount while compaction reduced tokens by 41% but doubled cost
  • Capping tool outputs at a stable size reduced cost per turn by 38% with no measurable quality loss, outperforming summarization-based approaches
  • Hybrid retrieval (BM25 + dense) is essential: dense retrieval dropped to 0% recall at 400k-token corpora while BM25 maintained 100%, and GraphRAG cost 44% more per turn with no quality gain
  • The core principle: name your constraint first (window size, cost, or quality degradation), as each requires a fundamentally different optimization strategy

Why It Matters

This article provides empirically grounded, production-tested guidance on context engineering that directly challenges common assumptions—particularly the instinct to summarize or compress conversation history. For AI practitioners building agentic systems, the findings offer a roadmap for reducing costs without sacrificing capability, while highlighting retrieval fusion strategies that many teams overlook. The $590 eval experiment serves as a cautionary template for how systematic benchmarking can expose flawed defaults before they scale.

Technical Details

  • Context composition analysis: In a production AI tutor, retrieval outputs alone contributed up to 100k tokens per turn, with some turns reaching ~200k tokens—far exceeding chat history as the primary context consumer
  • Cache economics: Full history retention achieved ~87% cached input discount at under $0.55 per million tokens; compaction reduced tokens by 41% but lost cache continuity, resulting in roughly 2x the cost
  • Tool output capping: Implementing stable size caps on tool outputs cut cost per turn by 38% with no measured degradation in memory or response quality, outperforming summarization-based compaction
  • Retrieval performance: Dense retrieval failed completely (0% recall) at 400k-token corpora in buried-fact tests, while BM25 maintained 100% recall; GraphRAG achieved equal accuracy but consumed 61% more tokens and cost 44% more per turn
  • Hybrid search fusion strategy: Early merging of semantic and keyword results can eliminate exact matches; the fix is to preserve top-5 candidates from each retriever before merging and deduplicating
  • Production stack: DeepSeek V4 Flash, hybrid retrieval, full history retention, stable tool output caps, no tool-output clearing, and summarization deferred as a last resort

Industry Insight

  • Organizations should audit their context composition before investing in summarization or compaction—most teams are optimizing the wrong component. Retrieval and tool output management typically offer higher ROI than history compression.
  • Prompt caching should be treated as a first-class economic factor in agent design; strategies that preserve cache continuity (like stable output capping) can dramatically outperform those that rewrite cached prefixes, even if they use more raw tokens.
  • Hybrid retrieval with careful fusion ordering is a low-effort, high-impact improvement. Teams relying on semantic search alone risk catastrophic recall degradation at scale, while simple keyword retrieval remains robust—preserving both streams in the fusion pipeline is critical.

TL;DR

  • 上下文窗口成本主要由检索和工具输出填充,而非对话历史,优化前需先测量实际占用来源
  • 保留完整历史配合缓存折扣比摘要压缩更经济,后者会破坏缓存前缀导致成本翻倍
  • 工具输出稳定截断比摘要重写更有效,可在不损失记忆质量的前提下降低38%单轮成本
  • 混合检索需保留关键词与语义检索的独立强候选再合并,过早融合会导致精确匹配丢失
  • 约束类型(窗口/成本/质量)决定上下文策略选择,复杂方案(如GraphRAG)未必优于简单混合检索

为什么值得看

本文通过$590实测数据揭示了代理上下文管理的反直觉规律,纠正了行业常见的优化误区。开源实验框架和评估工具为AI工程师提供了可复现的上下文工程方法论,对构建生产级代理系统具有直接指导价值。

技术解析

  • 上下文构成分析显示检索结果可达100k tokens,使部分对话轮次逼近200k窗口,历史对话仅占次要比例
  • 缓存经济学实验表明完整历史保留方案获得87%输入缓存折扣,而摘要压缩使缓存前缀失效导致成本上升
  • 工具输出稳定截断策略在零质量损失下实现38%成本下降,优于动态摘要重写方法
  • 混合检索融合策略需保留各检索器Top5候选再合并去重,避免语义检索淹没精确关键词匹配
  • GraphRAG在基准测试中比混合检索多消耗61% tokens且成本增加44%,未带来检索收益提升

行业启示

  • 上下文优化应遵循"约束优先"原则:窗口限制选检索卸载,成本敏感选缓存策略,质量要求选截断而非摘要
  • 生产环境宜采用"DeepSeek V4 Flash+混合检索+完整历史+稳定截断"的简化架构,避免过度工程化
  • 检索系统评估需区分事实类型:显著性事实可支持800k tokens检索,模糊事实需结合BM25保障召回率

Disclaimer: The above content is generated by AI and is for reference only. 免责声明:以上内容由 AI 生成,仅供参考。

LLM 大模型 Agent Agent RAG 检索增强生成 Evaluation 评测