Caching, Batching, and Prompt Reuse
Caching can reduce latency and cost when inputs or outputs are safely reusable. Cache keys must include all factors that affect correctness: tenant, permissions, prompt version, model, retrieval corpus version, and relevant user input. Never share cached private answers across users or tenants. Use TTLs and invalidation when underlying data changes.
Batching can improve throughput for independent tasks but may increase latency for individual users. Use bounded concurrency and provider batch facilities when their latency and availability trade-offs fit. Prompt caching features are provider-specific; follow current provider documentation for eligible content, cache lifetime, and billing behavior.
const cacheKey = hash({
tenantId,
userScope,
promptVersion,
modelId,
corpusVersion,
normalizedInput
});- Cache only when reuse is correct and permission-safe.
- Include prompt/model/corpus versions in cache identity.
- Use batching only when latency requirements allow it.
Why should tenant or permission scope be represented in a cache key?