Hey KAMI1a,
As an AI Automation Engineer running production workloads with Anthropic and n8n daily, I completely feel your pain. When you are running 10–20 messages per conversation across multiple clients, un-cached system prompts will absolutely eat your API budget alive.
@achamm is spot on—n8n doesn’t natively expose the cache_control parameter inside the Advanced Options of the Anthropic Chat Model node yet.
While we wait for native support to land on the roadmap, you don’t have to bleed API costs. The absolute best production-grade workaround right now is to intercept the request and inject the cache headers yourself.
Here is exactly how to set this up:
The Solution: The Cloudflare Worker Proxy Trick
Instead of sending requests from n8n directly to Anthropic, you point n8n to a lightweight, free Cloudflare Worker. The Worker takes n8n’s payload, injects the cache_control: {"type": "ephemeral"} block into the system prompt array, and forwards it to Anthropic.
1. Deploy the Cloudflare Worker
Create a new Cloudflare Worker and drop in a script that intercepts the incoming POST request body. Your JavaScript code inside the worker should look something like this:
JavaScript
export default {
async fetch(request, env) {
if (request.method === "POST") {
let body = await request.json();
// Inject cache_control into the system prompt if it exists
if (body.system && typeof body.system === 'string') {
body.system = [
{
type: "text",
text: body.system,
cache_control: { type: "ephemeral" }
}
];
}
// Forward the modified request to Anthropic
const modifiedRequest = new Request("https://api.anthropic.com/v1/messages", {
method: "POST",
headers: request.headers,
body: JSON.stringify(body)
});
return fetch(modifiedRequest);
}
return new Response("Method not allowed", { status: 405 });
}
};
2. Update n8n Credentials
-
Go to your Credentials manager in n8n and open your Anthropic API configuration.
-
Toggle on the Base URL override option (you may need to enable advanced settings).
-
Replace https://api.anthropic.com/v1 with your deployed Cloudflare Worker URL (e.g., https://your-worker-name.workers.dev).
Crucial Production Gotchas to Keep in Mind
Since you’re running this for real clients, keep these two rules in mind to make sure you actually get a cache hit:
-
Keep the Prefix Static: Anthropic requires the cached block (system prompt + tools) to be entirely stable. If your AI Agent node dynamically injects variable data (like the current time or a changing customer name) directly into the system prompt string, it will invalidate the cache every single turn. Keep dynamic data inside the user messages instead.
-
Mind the Token Minimum: Anthropic only triggers prompt caching if your prompt prefix is greater than 1,024 tokens (Sonnet/Opus). Since your system prompt is ~1,500 tokens, you’re perfectly in the clear, but keep this in mind if you try to scale down smaller prompts!
This little proxy middleware layer should drop your recurring message costs by up to 90% immediately until the native feature drops.
Let me know if you need any help tweaking the worker script to match your exact tool payload structure!