Skip to content

YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context

perf: enable Anthropic prompt caching in AI gateway (90% input cost reduction)

Louis Beaumontc4c7ba70be48
diff --git a/packages/ai-gateway/src/providers/anthropic-proxy.ts b/packages/ai-gateway/src/providers/anthropic-proxy.ts
index 5687db148..7f814333b 100644
--- a/packages/ai-gateway/src/providers/anthropic-proxy.ts
+++ b/packages/ai-gateway/src/providers/anthropic-proxy.ts
@@ -31,7 +31,7 @@ export async function proxyToAnthropic(
stream?: boolean;
max_tokens?: number;
messages?: any[];
- system?: string;
+ system?: string | Array<{ type: string; text: string; cache_control?: { type: string } }>;
temperature?: number;
[key: string]: any;
};
@@ -52,6 +52,25 @@ export async function proxyToAnthropic(
body.messages = sanitizeMessages(body.messages);
}
+ // Inject prompt caching on the system prompt if not already present.
+ // Anthropic caches the prefix up to the last cache_control breakpoint,
+ // reducing input costs by 90% on cache hits (5-min TTL).
+ if (body.system) {
+ if (typeof body.system === 'string') {
+ // Convert string to array format with cache_control
+ if (body.system.length >= 4096) {
+ body.system = [{ type: 'text', text: body.system, cache_control: { type: 'ephemeral' } }];
+ }
+ } else if (Array.isArray(body.system) && body.system.length > 0) {
+ // Array format — add cache_control to last block if not already present
+ const last = body.system[body.system.length - 1];
+ const totalLen = body.system.reduce((sum, b) => sum + (b.text?.length || 0), 0);
+ if (totalLen >= 4096 && !last.cache_control) {
+ last.cache_control = { type: 'ephemeral' };
+ }
+ }
+ }
+
const isStreaming = body.stream === true;
// Forward directly to Anthropic API
diff --git a/packages/ai-gateway/src/providers/anthropic.ts b/packages/ai-gateway/src/providers/anthropic.ts
index c37e2ab6f..f0e2ed8e8 100644
--- a/packages/ai-gateway/src/providers/anthropic.ts
+++ b/packages/ai-gateway/src/providers/anthropic.ts
@@ -24,9 +24,11 @@ export class AnthropicProvider implements AIProvider {
}
/**
- * Build the system prompt from system messages and response_format
+ * Build the system prompt from system messages and response_format.
+ * Returns an array of content blocks with cache_control on the last block
+ * to enable Anthropic prompt caching (90% input cost reduction on cache hits).
*/
- private buildSystemPrompt(body: RequestBody): string | undefined {
+ private buildSystemPrompt(body: RequestBody): Array<{ type: 'text'; text: string; cache_control?: { type: 'ephemeral' } }> | undefined {
const parts: string[] = [];
// Extract system messages from the conversation
@@ -50,7 +52,18 @@ export class AnthropicProvider implements AIProvider {
}
}
- return parts.length > 0 ? parts.join('\n\n') : undefined;
+ if (parts.length === 0) return undefined;
+
+ const combined = parts.join('\n\n');
+ // Only cache system prompts with enough tokens to benefit (Anthropic min: 1024 tokens for haiku, 2048 for sonnet/opus)
+ // Rough heuristic: 4 chars ≈ 1 token, so 4096 chars ≈ 1024 tokens
+ const shouldCache = combined.length >= 4096;
+
+ return [{
+ type: 'text' as const,
+ text: combined,
+ ...(shouldCache ? { cache_control: { type: 'ephemeral' as const } } : {}),
+ }];
}
/**