Prompt Caching with Deep AgentsDeep Agents 的提示词缓存

Alex Olsen
June 26, 2026
5
min
Go back to blog

A powerful lever in running agents cost-efficiently at scale is Prompt Caching, a feature offered by model providers that can reduce the token cost of inference by 41-80%. As Manus AI puts it -大规模运行智能体时实现成本效益的一个有力手段是提示词缓存(Prompt Caching),这是模型提供商提供的一项功能,可将推理的 token 成本降低 41-80%。正如 Manus AI 所说——

💡 "If I had to choose just one metric, I'd argue that the KV-cache hit rate is the single most important metric for a production-stage AI agent.""如果只能选一个指标,我认为 KV 缓存命中率是生产阶段 AI 智能体最重要的单一指标。"

However, model providers support varied strategies for controlling caching, making provider-agnostic caching a trickier solve.然而,各模型提供商支持的缓存控制策略各不相同,这使得与提供商无关的缓存成为一个更棘手的难题。

Deep Agents is our general purpose, model-agnostic agent harness which supports prompt caching features across all major providers. We’re going to dig into how Deep Agents uses prompt caching to cut API costs, but first let’s look at how prompt caching reduces token costs in a chat model conversation.Deep Agents 是我们通用的、与模型无关的智能体框架,支持所有主要提供商的提示词缓存功能。我们将深入探讨 Deep Agents 如何利用提示词缓存来削减 API 成本,但首先让我们看看提示词缓存如何降低聊天模型对话中的 token 成本。

TL;DR: prompt caching太长不看:提示词缓存

The token cost of a chat model conversation grows quickly. For each new message, the model must reprocess every prior token in the conversation, including the:聊天模型对话的 token 成本增长很快。对于每条新消息,模型必须重新处理对话中之前的所有 token,包括:

  • System prompt系统提示词
  • Tool descriptions工具描述
  • Loaded skills已加载的技能
  • Message history消息历史
  • New message新消息

When we opt into prompt caching, the provider stores a snapshot of the model’s state after processing a prompt:当我们选择使用提示词缓存时,提供商会存储模型处理提示词后的状态快照:

On the next request, the model picks up from that snapshot and only processes new text.在下一次请求时,模型从该快照处继续,只处理新的文本。

However, loading a new skill or tool can modify our prompt earlier in the conversation, potentially causing a cache bust. Some model providers enable us to add explicit cache breakpoints earlier in the prompt, resulting in a cache hit on a subset of the prompt rather than a full cache bust. However, not all model providers support explicit cache breakpoints:然而,加载新技能或工具可能会修改对话中较早位置的提示词,可能导致缓存失效。一些模型提供商允许我们在提示词中较早位置添加显式的缓存断点,从而对提示词的子集实现缓存命中,而不是完全缓存失效。不过,并非所有模型提供商都支持显式缓存断点:

Anthropic OpenAI Gemini AWS Bedrock Fireworks
Explicit Breakpoints Per-provider

Explicit caching is also just one prompt caching feature with varied support among providers:显式缓存只是众多提示词缓存功能之一,各提供商的支持情况各不相同:

Anthropic OpenAI Gemini AWS Bedrock Fireworks
Explicit Breakpoints Per-provider
Configurable TTL Per-model Per-provider
Cache Prewarm Anthropic
Routing Key OpenAI

The prompt caching feature support landscape changes quickly. Be sure to check model provider docs for reference on feature support.提示词缓存功能的支持格局变化很快。请务必查阅模型提供商的文档以了解功能支持情况。

Between differing prompt caching implementations and feature support among providers, it can be a challenge to achieve maximal cost savings across providers.鉴于各提供商在提示词缓存实现和功能支持方面存在差异,要在所有提供商处实现最大的成本节省可能颇具挑战。

How we’re solving this in Deep AgentsDeep Agents 如何解决这一问题

The Deep Agents harness makes a best-effort attempt at utilizing prompt caching features by automatically:Deep Agents 框架会尽力利用提示词缓存功能,自动执行以下操作:

  1. Setting explicit cache breakpoints when supported在支持时设置显式缓存断点
  2. Opting in to provider-side implicit caching when explicit breakpoints aren’t supported在不支持显式断点时,选择使用提供商端的隐式缓存
  3. Structuring your prompt to maximize cache reads构建提示词结构以最大化缓存读取

These strategies are supported for all major providers, so you’re able to switch provider at any time and still reap maximal token savings. To take advantage of provider-specific features, the harness detects the current model provider and delegates caching to provider-specific middleware. You can also use the middleware in your own createAgent() to opt in to prompt caching savings:这些策略支持所有主要提供商,因此您可以随时切换提供商,仍然可以获得最大的 token 节省。为了利用提供商特定的功能,框架会检测当前模型提供商,并将缓存委托给提供商特定的中间件。您也可以在自己的 createAgent() 中使用该中间件来选择加入提示词缓存节省:

// In Deep Agents you get prompt caching for free!
const agent = createDeepAgent({ model: 'gpt-5.5' });
 
// In LangChain, opt in via our middleware:
const agent = createAgent({
  model: 'claude-haiku-4-5-20251001',
  middleware: [anthropicPromptCachingMiddleware()],
});

The Deep Agents harness also structures your prompt and explicit cache points to minimize cache degradation. Optimally the static prefix (your tool descriptions, skills, system prompt) in a model invocation remains static. It can however change when doing things like updating a memory or compacting a conversation, leading to a cache bust. Deep Agents minimizes the blast radius by structuring your prompt and explicit cache points such that if e.g. a memory is updated, you still get a cache read on a subset of your prompt.Deep Agents 框架还会构建您的提示词和显式缓存点,以最小化缓存退化。理想情况下,模型调用中的静态前缀(您的工具描述、技能、系统提示词)保持不变。然而,在执行更新记忆或压缩对话等操作时,它可能会发生变化,导致缓存失效。Deep Agents 通过构建提示词和显式缓存点来最小化影响范围,例如,如果记忆被更新,您仍然可以对提示词的某个子集实现缓存读取。

The real savings of prompt caching提示词缓存的实际节省效果

Feature tables tell us what's possible. To see what prompt caching actually saves, we ran the Deep Agents eval suite across a mid-tier model from each of three providers: claude-haiku-4-5, gpt-5.4-mini, and gemini-3.5-flash. The result is the chart below. On real agent trajectories, prompt caching cut token cost by 49–80%.功能表格告诉我们什么是可能的。为了解提示词缓存的实际节省效果,我们在三个提供商的各一款中端模型上运行了 Deep Agents 评估套件:claude-haiku-4-5、gpt-5.4-mini 和 gemini-3.5-flash。结果如下图所示。在真实的智能体运行轨迹中,提示词缓存将 token 成本降低了 49-80%。

  • claude-haiku-4-5: -77%. Using Anthropic's explicit breakpoints, we can keep a large portion of the prompt cached. This significantly reduced the token cost of each request.claude-haiku-4-5:-77%。使用 Anthropic 的显式断点,我们可以保持提示词的很大一部分处于缓存状态。这显著降低了每次请求的 token 成本。
  • gpt-5.4-mini: -80%. OpenAI's automatic longest-prefix caching gives us a sizable 80%  cost reductiongpt-5.4-mini:-80%。OpenAI 的自动最长前缀缓存为我们带来了可观的 80% 成本降低
  • gemini-3.5-flash: -49%. Gemini's implicit caching makes no explicit savings guarantee, but we still see considerable savingsgemini-3.5-flash:-49%。Gemini 的隐式缓存不做显式的节省保证,但我们仍然看到了可观的节省

It's also worth noting that caching pays off more the longer a conversation runs: the cached prefix is reused across every turn, so the long-horizon tasks are the ones that benefit most.还值得注意的是,缓存的效果随着对话时间的延长而更加显著:缓存的前缀在每一轮中都会被重复使用,因此长期任务受益最大。

Observability with LangSmith使用 LangSmith 进行可观测性

Cost savings from prompt caching are only as good as your ability to measure them. LangSmith offers visibility into API cost, cache reads, and token usage at a per-invocation and per-trajectory level:提示词缓存带来的成本节省,取决于您衡量它们的能力。LangSmith 提供了对 API 成本、缓存读取和 token 使用量的可见性,涵盖每次调用和每次运行轨迹的粒度:

For each invocation you get time-to-first-token, total input tokens, cache-read tokens, and total output tokens rolled up to a per-trajectory aggregate. Because cache reads are itemized separately, you can see exactly how much of each prompt was served from cache rather than reprocessed.对于每次调用,您可以获得首 token 时间、总输入 token 数、缓存读取 token 数和总输出 token 数,并汇总到每次运行轨迹的聚合数据。由于缓存读取是单独列出的,您可以准确看到每次提示词中有多少是从缓存中直接读取的,而非重新处理。

This is also how we produced the numbers in this post:这也是我们生成本文中数据的方式:

  1. Run the Deep Agents eval suite against each agent configuration针对每种智能体配置运行 Deep Agents 评估套件
  2. Inspect trace data in the LangSmith dashboard to verify run results在 LangSmith 仪表板中检查追踪数据以验证运行结果
  3. Pull the run data via the LangSmith Client SDK通过 LangSmith 客户端 SDK 拉取运行数据
  4. Compute per-provider cost deltas by dropping the data into a Jupyter notebook (or have an agent use LangSmith Skills to help)将数据导入 Jupyter 笔记本计算各提供商的成本差异(或者让智能体使用 LangSmith 技能来协助)

LangSmith lets us disentangle savings from caching, trajectory length, and cheaper turns, which can inform how we optimize our agent. More on how to read and act on data in LangSmith here.LangSmith 让我们能够区分缓存、轨迹长度和更便宜的轮次所带来的节省,这有助于我们优化智能体。更多关于如何在 LangSmith 中解读和根据数据采取行动的内容,请点击此处。

Next in prompt caching提示词缓存的未来

Model providers have yet to converge on a common feature set for prompt caching. Explicit breakpoints drove some savings above, but it’s only the start. A handful of other features - cache prewarm, routing keys, configurable TTL - stand to unlock further cost savings and  latency wins.模型提供商尚未就提示词缓存的通用功能集达成一致。显式断点在上文带来了一些节省,但这只是开始。还有一些其他功能——缓存预热、路由键、可配置 TTL——有望进一步释放成本节省和延迟优化。

You can take advantage of the currently-supported features today by using createDeepAgent - no additional config needed. As model providers add additional feature support, we’ll continue to fold them into the existing harness.您可以通过使用 createDeepAgent 立即利用当前支持的功能——无需额外配置。随着模型提供商增加更多功能支持,我们将继续将其整合到现有框架中。

S
e
e
w
h
a
t
y
o
u
r
a
g
e
n
t
i
s
r
e
a
l
l
y
d
o
i
n
g

LangSmith, our agent engineering platform, helps developers debug every agent decision, eval changes, and deploy in one click.