Scaling Prompt Caching Beyond Single-User Demos
Basic prompt caching tutorials demonstrate single-user workflows: caching a single static document across 5 consecutive queries.
In multi-tenant B2B SaaS platforms with 10,000 corporate clients, naive caching architectures fail:
- Inserting a user’s account name at the top of the prompt invalidates the cache for all other users in that company.
- Inserting a company’s custom branding at the top invalidates the global system cache shared across all companies.
Achieving high Cache Hit Ratios (>85%) across a multi-tenant enterprise requires a Hierarchical Three-Tier Cache Architecture.
1. The Three-Tier Cache Hierarchy
Prompt caching requires exact byte-for-byte prefix matching. Therefore, tokens must be ordered strictly from most universal to most specific:
[Tier 1: Global Platform Core] -> Shared across ALL tenants (99% Hit Rate)
↳ [Tier 2: Tenant Domain Knowledge] -> Shared across ALL users in Tenant X (85% Hit Rate)
↳ [Tier 3: User Session State] -> Ephemeral to specific user (60% Hit Rate)
↳ [Tier 4: Dynamic Query & Current Timestamp] -> Never cached
graph TD
T1[Tier 1: Global Core Instructions & Base Tool Schemas (10k tokens)] --> T2[Tier 2: Organization Knowledge Base & Legal Policies (30k tokens)]
T2 --> T3[Tier 3: User Active Conversation Window (2k tokens)]
T3 --> T4[Tier 4: Dynamic Prompt Query (100 tokens)]
By placing Tier 1 at the absolute top, every request from every customer reuses the pre-computed KV cache of your core platform instructions.
2. Implementing Hierarchical Breakpoints with Anthropic SDK
// ai/enterprise-cached-agent.ts
import Anthropic from '@anthropic-ai/sdk';
const anthropic = new Anthropic();
interface EnterprisePromptContext {
globalSystemInstructions: string; // Tier 1
tenantDocumentation: string; // Tier 2
userConversationHistory: Array<{ role: 'user' | 'assistant'; content: string }>; // Tier 3
currentQuery: string; // Tier 4
}
export async function executeHierarchicalCachedQuery(ctx: EnterprisePromptContext) {
const response = await anthropic.messages.create({
model: 'claude-3-5-sonnet-20241022',
max_tokens: 1024,
system: [
// Breakpoint 1: Global Core (Cached for ALL tenants globally)
{
type: 'text',
text: ctx.globalSystemInstructions,
cache_control: { type: 'ephemeral' },
},
// Breakpoint 2: Tenant Knowledge Base (Cached for all users in this tenant)
{
type: 'text',
text: ctx.tenantDocumentation,
cache_control: { type: 'ephemeral' },
},
],
messages: [
...ctx.userConversationHistory,
{ role: 'user', content: ctx.currentQuery },
],
});
return response;
}
3. Cache Invalidation Mechanics
When a tenant updates their internal HR policy handbook, you must invalidate Tier 2 without busting Tier 1:
- Because Tier 1 is an immutable prefix, global cache tokens remain warm.
- Only downstream tokens for that specific tenant undergo a one-time cache re-computation.
- Session IDs and timestamps are relegated strictly to the final user message, ensuring they never bust upstream cache blocks.
4. Key Takeaways
- Order Matters Absolutely: Universal static text at the top; dynamic volatile data at the bottom.
- Use Multi-Breakpoint Caching: Modern APIs allow placing up to 4 cache breakpoints in a single request.
- Monitor Per-Tenant Hit Rates: Track cache performance metrics to identify which customer tenants have poorly organized prompts.