Routing Custom LangChain and LlamaIndex Inbound Evaluation Agents to Human Closers Before Token Exhaustion
本文为原文前 6,000 字符的节选翻译,完整内容请查看原文。
Routing Custom LangChain and LlamaIndex Inbound Evaluation Agents to Human Closers Before Token Exhaustion
Introduction: The Hidden Failure Mode of Inbound Evaluation Agents B2B revenue teams increasingly build custom inbound evaluation agents on orchestration frameworks such as LangChain (via LangGraph) and LlamaIndex (via event-driven Workflows). These agents qualify enterprise leads, parse technical RFPs, inspect architecture diagrams, and answer product compliance questions before a discovery call is booked. In short interactions, these agents work well. Enterprise evaluations are rarely short. A prospect uploads a SOC 2 questionnaire, pastes a large JSON schema, and negotiates SLA terms over many turns. At that point an autonomous agent faces two problems: it can run into a hard token or rate limit, and its answer quality can degrade well before that limit.
引言:入站评估代理的隐藏故障模式 B2B 营收团队越来越多地基于 LangChain(通过 LangGraph)和 LlamaIndex(通过事件驱动的工作流)等编排框架构建自定义入站评估代理。这些代理在预约发现通话之前,负责筛选企业潜在客户、解析技术 RFP、检查架构图并回答产品合规性问题。在简短的交互中,这些代理表现良好。但企业级评估很少是简短的。潜在客户可能会上传 SOC 2 调查问卷、粘贴大型 JSON 模式,并在多轮对话中协商 SLA 条款。此时,自主代理面临两个问题:它可能会遇到硬性 Token 限制或速率限制,且其回答质量在达到该限制之前就可能大幅下降。
The second problem is documented. In July 2025, Chroma published a technical report called Context Rot, which tested 18 LLMs. It found that models do not use their context uniformly, and that performance becomes less reliable as input length grows, even on simple tasks. Large context windows therefore don’t make it safe to let an agent run until it hits the ceiling. Windows now reach 1M tokens or more, but reliability can fall off long before the window is full. Cost also grows with conversation length. Most chat APIs are stateless, so every turn re-sends the accumulated history. Cumulative input tokens across a conversation therefore grow roughly quadratically with the number of turns, unless provider-side prompt caching reduces the bill. And if the agent errors out, loops, or improvises a pricing commitment late in a long session, you may lose a high-value deal.
第二个问题已有记录。2025 年 7 月,Chroma 发布了一份名为《上下文衰减》(Context Rot)的技术报告,测试了 18 个大语言模型。报告发现,模型对上下文的使用并不均匀,随着输入长度的增加,即使在简单任务上,性能也会变得不可靠。因此,拥有巨大的上下文窗口并不意味着让代理运行到极限是安全的。现在的窗口已达到 100 万个 Token 或更多,但可靠性往往在窗口填满前就已大幅下降。成本也会随着对话长度的增加而增长。大多数聊天 API 是无状态的,因此每一轮对话都会重新发送累积的历史记录。除非提供商端的提示词缓存(Prompt Caching)能降低费用,否则整个对话中的累计输入 Token 数大致会随轮数呈二次方增长。如果代理在长会话后期出错、陷入循环或即兴做出定价承诺,你可能会失去一笔高价值交易。
The fix is proactive, telemetry-driven escalation. Intercept the agent before it exhausts its budget or starts to degrade. Checkpoint its state, compress the qualification context into a short brief, and post an interactive claiming card to a Slack channel. A human Account Executive (AE) or Solutions Engineer (SE) can then claim the lead and continue the conversation. This guide covers the architecture and working patterns for LangGraph, LlamaIndex Workflows, Redis locking, and Slack Bolt. The code is a starting point rather than a drop-in product. Thresholds, model names, and storage choices should be tuned to your own traffic.
解决方案是主动的、基于遥测的升级机制。在代理耗尽预算或开始性能下降之前对其进行拦截。检查其状态,将资格评估上下文压缩为简短摘要,并向 Slack 频道发布交互式认领卡片。随后,人类客户经理 (AE) 或解决方案工程师 (SE) 可以认领该潜在客户并继续对话。本指南涵盖了 LangGraph、LlamaIndex 工作流、Redis 锁定和 Slack Bolt 的架构与工作模式。代码仅作为起点,而非即插即用的产品。阈值、模型名称和存储选择应根据你自己的流量进行调整。
1. Token Exhaustion vs. Context Rot in Multi-Turn Evaluations Two distinct failure thresholds apply to multi-turn agent loops. Hard token exhaustion (the infrastructure ceiling). The prompt sent on turn n contains the system prompt, the full history of prior user, assistant, and tool messages, the new user message, and any retrieved RAG chunks: prompt_tokens(n) = system + Σ(i < n) [user_i + assistant_i + tool_output_i] + user_n + rag_chunks_n. Trouble starts when this approaches the model’s maximum context window, or when sustained usage trips an API tokens-per-minute (TPM) rate limit. A single turn with a large document upload and a wide retrieval step can add tens of thousands of tokens. How fast you reach the limit depends on your documents, chunk sizes, and tools, so measure it on real transcripts instead of assuming a number.
1. 多轮评估中的 Token 耗尽与上下文衰减 多轮代理循环存在两个不同的故障阈值。硬性 Token 耗尽(基础设施上限)。第 n 轮发送的提示词包含系统提示词、之前用户、助手和工具消息的完整历史记录、新的用户消息以及任何检索到的 RAG 数据块:prompt_tokens(n) = system + Σ(i < n) [user_i + assistant_i + tool_output_i] + user_n + rag_chunks_n。当该数值接近模型的最大上下文窗口,或持续使用触发 API 每分钟 Token 数 (TPM) 限制时,问题就会出现。单轮对话中上传大型文档并进行广泛检索步骤可能会增加数万个 Token。达到限制的速度取决于你的文档、数据块大小和工具,因此请根据实际记录进行测量,而不是凭空假设。
Soft exhaustion: context rot and reasoning loops. Context rot is the decline in output quality as input grows, even when the task stays equally hard. Chroma’s work showed it varies by model, by task, and by how similar the distractor content is to the question. Retrieval quality also affects it. In inbound sales evaluations it tends to show up as: Circular clarification loops: the agent re-asks for deployment constraints the prospect already gave. Tool-calling thrash: the agent calls the same CRM lookup or documentation retriever repeatedly with slightly altered arguments and makes no progress. Policy drift: the agent stops following negative constraints in the system prompt (for example, “never quote custom discounts without SE approval”).
软性耗尽:上下文衰减与推理循环。上下文衰减是指随着输入增长,即使任务难度保持不变,输出质量也会下降。Chroma 的研究表明,这种情况因模型、任务以及干扰内容与问题的相似度而异。检索质量也会对其产生影响。在入站销售评估中,它通常表现为:循环澄清:代理反复询问潜在客户已经提供的部署约束。工具调用抖动:代理反复调用相同的 CRM 查询或文档检索器,参数略有不同,但毫无进展。策略漂移:代理不再遵守系统提示词中的负面约束(例如,“未经 SE 批准,绝不报价自定义折扣”)。
The escalation threshold matrix. Because degradation is gradual and model-specific, define an operational token budget well below the provider’s maximum window. Set it from evals on your own transcripts, for example the point where qualification accuracy or instruction-following starts to slip. The thresholds below are illustrative starting points, not industry standards. The token rows assume an operational budget of 64k tokens.
升级阈值矩阵。由于性能下降是渐进的且因模型而异,请定义一个远低于提供商最大窗口的操作 Token 预算。通过对你自己的记录进行评估来设定该值,例如在资格评估准确性或指令遵循能力开始下滑的点。下方的阈值仅为说明性起点,而非行业标准。Token 行假设操作预算为 64k Token。
2. System Architecture: Decoupling Agent Execution from Human Claiming Don’t hold an HTTP request or a Python worker open while a human decides to answer in Slack. A closer may claim a lead in seconds, or the lead may sit unclaimed for minutes or longer. The agent’s execution state therefore has to be checkpointed to durable storage, suspended, and resumed through events.
2. 系统架构:将代理执行与人工认领解耦 不要在人类决定在 Slack 中回复时保持 HTTP 请求或 Python 工作进程处于打开状态。销售人员可能在几秒钟内认领潜在客户,也可能让潜在客户闲置数分钟甚至更久。因此,代理的执行状态必须检查点化(Checkpoint)到持久存储中,挂起,并通过事件恢复。