Anthropic and OpenAI prompt caching in n8n: cutting the cost and latency of repeated AI calls
Published 7 August 2026 · 7 min read
An n8n workflow that classifies emails, queries a document base, or runs a conversational agent often sends the same block of text on every call: the same system prompt, the same tool definitions, the same large RAG context — only the question changes. Without optimization, the model provider bills and reprocesses that entire block on every request. Prompt caching, offered natively by Anthropic and OpenAI, remembers the computation of that identical prefix to reuse it on the next call: up to 90% cost reduction on those tokens and noticeably lower latency. This guide covers how it works, why n8n doesn't enable it automatically, and how to wire it up anyway.
What prompt caching actually solves
Take an n8n AI Agent with several custom tools: on every conversation turn, the model receives the full system prompt, the list of available tools, and the discussion history all over again. Same story for a Loop Over Items that classifies 300 emails one by one with identical sorting instructions on each iteration, or a RAG chatbot whose injected document context stays stable across several consecutive questions from the same visitor. In all three cases, a large share — sometimes the majority — of the tokens sent is strictly identical from one call to the next. Prompt caching targets exactly that redundancy.
Don't confuse it with the semantic cache described in our Redis semantic cache guide: that mechanism compares the meaning of two different questions to reuse an already-computed answer, short-circuiting the model call entirely. Prompt caching works inside the call itself: it doesn't change the generated answer, it simply speeds up and lowers the cost of processing the shared prefix. The two mechanisms are complementary rather than competing.
How prompt caching works with Anthropic
With Anthropic, activation is explicit: you add a "cache_control": {"type": "ephemeral"} field to the content blocks you want cached — the system prompt, a large document injected as context, a tool set definition. A single call can define up to four such breakpoints. Each marked block must exceed a minimum token threshold, roughly 1024 tokens depending on the model (the exact figure lives in Anthropic's official docs and varies by model generation): below that, the block simply isn't cached, with no error.
The cache stays active for 5 minutes by default, refreshed for free on every successful read; a paid option extends its lifetime to 1 hour. On pricing, writing to the cache costs more than the standard input rate (roughly 1.25x for a 5-minute cache, 2x for 1 hour), but reading an already-written cache costs only 0.1x the standard rate — a 90% discount. Anthropic's official documentation states gains of "up to 90% on cost and up to 85% on latency" for long, repeated prompts.
How prompt caching works with OpenAI
OpenAI takes a simpler but less generous approach: caching is automatic, with no field or header to add. Once a prompt exceeds roughly 1024 tokens, the API reuses the longest recently-seen prefix, in 128-token increments, with a 50% discount on those cached tokens — less dramatic than Anthropic's, but with zero implementation effort. The cache typically stays available for 5 to 10 minutes after the last call, occasionally up to an hour during off-peak periods, with no contractual guarantee on that duration.
The n8n limitation: no native option
This is the most common blocker: the AI Agent, Basic LLM Chain, and their model sub-nodes (OpenAI Chat Model, Anthropic Chat Model) rely on the LangChain integration of the credentials, which doesn't expose any field to set a cache_control breakpoint. OpenAI's prompt caching still applies automatically in the background once the token threshold is reached, since it requires no configuration on the caller's side. That's not the case for Anthropic, whose mechanism is opt-in: without the cache_control field explicitly set in the request, no cache is created, and n8n's native nodes never set it.
To enable it with Anthropic, you need to bypass the LangChain nodes and call the Messages API directly through an HTTP Request node — the same move already required for OpenAI's and Anthropic's Batch APIs, which aren't controllable through the native nodes either.
Implementation with the HTTP Request node
A call to POST https://api.anthropic.com/v1/messages with caching enabled on the system prompt looks like this:
{
"model": "claude-sonnet-4-5",
"max_tokens": 1024,
"system": [
{
"type": "text",
"text": "You are an assistant that classifies incoming emails according to the following rules: [... long, stable instructions ...]",
"cache_control": { "type": "ephemeral" }
}
],
"messages": [
{ "role": "user", "content": "{{ $json.emailContent }}" }
]
}
In n8n, this body is built inside an HTTP Request node using POST, authenticated via "Predefined Credential Type" by selecting the Anthropic credential already used by your other AI nodes — no need to create a dedicated one. The principle: everything static and reused across calls goes into the block marked cache_control, and everything that changes on every run stays in the messages array. The response includes a usage object detailing cache_creation_input_tokens and cache_read_input_tokens — a simple way to confirm the cache is actually being hit rather than rebuilt on every call.
Which n8n workflows actually benefit
The gain only materializes if the same prefix is reused several times within the cache's lifetime (5 minutes, or 1 hour on the extended option). Three workflow profiles see a net benefit:
- Classification loops: a Loop Over Items applying the same sorting instructions to dozens or hundreds of emails, tickets, or customer reviews in a row — the system prompt is billed at full rate only once, then read back at a discount as long as the loop stays under the 5-minute window between calls.
- Active conversational agents: a RAG chatbot or an AI chat widget where the same visitor sends several messages within a few minutes, with a stable system prompt and document context across turns.
- AI Agent with a large tool set: the longer the list of custom tools exposed to the model, the bigger the gain from caching their definitions on each new agent call within the same session.
Conversely, an isolated daily cron job, or a workflow whose calls are spaced several dozen minutes apart, sees no real benefit: the cache will have already expired before the next call, and the initial write (pricier than the standard rate) becomes a pure cost rather than an amortized investment.
A technical foundation, not an arbitrary discount
As with Batch APIs, this discount reflects a genuine server-side efficiency gain rather than a commercial gesture. The principle — remembering already-computed attention keys and values for a text prefix, instead of recomputing everything on every request — is documented by In Gim, Guojun Chen, Seung-seob Lee, Nikhil Sarda, Anurag Khandelwal, and Lin Zhong in "Prompt Cache: Modular Attention Reuse for Low-Latency Inference" (MLSys 2024), which measures a marked reduction in time-to-first-token. The same principle underlies RadixAttention, described by Lianmin Zheng and coauthors in "SGLang: Efficient Execution of Structured Language Model Programs", an open-source inference engine that automatically indexes shared prefixes across concurrent requests.
Pitfalls to avoid
- Block order in the prompt: caching relies on a character-for-character identical prefix. Put static content (system, tools, context) before dynamic content (the question) — a single changed token near the top of the prompt invalidates the whole cache downstream.
- Prompt too short: below the minimum token threshold, Anthropic silently ignores the
cache_controlfield, with no error and no gain. Checkcache_creation_input_tokensin the response to confirm caching actually happened. - Call spacing: a slow step between two items in a loop can let the cache expire before the next call. For long loops, the 1-hour extension on Anthropic is worth testing.
- Cost tracking: combine this optimization with the AI call cost tracking you already have in place, distinguishing cache tokens from standard tokens — without that detail, a lower bill is hard to reliably attribute to the cache.
Takeaway
Prompt caching requires no dedicated n8n node, but it does mean stepping outside the native AI nodes to build the call by hand via HTTP Request — the same workaround already needed for the Batch APIs. In exchange, workflows that reuse a large system prompt or a heavy context at high frequency — batch classification, conversational agents, a tool-heavy AI Agent — see their bill and latency drop noticeably, with no change to answer quality. The workflows in the AI Inbox Pack (€79), RAG Assistant Pack (€119), and Compliance & Audit Pack (€149) — bundled in the Complete FlowKit Bundle (€269 instead of €347) — are built on standard model sub-nodes: swapping in an HTTP Request call with cache_control in place of the Chat Model node, on high-volume steps, is an optimization to add once the workflow is validated, not a prerequisite to get started.
FAQ
Frequently asked questions
Does prompt caching work with n8n's AI Agent or Basic LLM Chain node?
Not natively, as of today: these nodes rely on the LangChain integration of the OpenAI and Anthropic credentials, which doesn't expose the cache_control field or the related headers. To enable it, you need an HTTP Request node that calls Anthropic's Messages API or OpenAI's Chat Completions API directly, building the request body yourself.
How is this different from the semantic cache described in your Redis article?
Semantic caching compares the meaning of two different questions to reuse an already-generated answer: it short-circuits the LLM call entirely. Prompt caching, on the other hand, has the provider remember the internal computation (the attention keys and values) of an identical prompt prefix — the system prompt, the RAG context, the tool definitions — to speed up and cut the cost of the next call that starts with that same prefix. The two are complementary, not competing mechanisms.
Is prompt caching worth it for a workflow that runs once a day?
Rarely. The cache expires after 5 minutes with Anthropic (up to 1 hour on a paid extension) and after a few minutes of inactivity with OpenAI. The gain shows up on calls made close together in time — a loop over several items, an active conversational agent, a dense batch job — not on an isolated daily cron where every call rebuilds the cache from scratch.
Bundle FlowKit Complet
€269