
Security News
Deno Goes the Way of the Dinosaur
Cloudflare is acquiring Deno’s team, which will provide another year of bug fixes and security updates before ending support for the runtime.
@krishgupta/ai-prompt-cache
Advanced tools
Production-grade prompt caching middleware suite for Vercel AI SDK
Middleware for the Vercel AI SDK that enables prompt caching across providers. Tested improvements show 50%+ reduction in time-to-first-token (TTFT).
npm install @krishgupta/ai-prompt-cache
import { wrapLanguageModel, streamText } from 'ai';
import { openai } from '@ai-sdk/openai';
import { withPromptCache } from '@krishgupta/ai-prompt-cache';
const model = wrapLanguageModel({
model: openai('gpt-4o'),
middleware: withPromptCache({
select: 'system-head',
extraKeySalt: 'my-app-v1',
}),
});
const result = await streamText({
model,
messages: [
{ role: 'system', content: 'You are a helpful assistant...' },
{ role: 'user', content: 'Hello!' },
],
});
Tested with OpenAI gpt-4o and a ~1200 line system prompt:
| Request | Mode | TTFT |
|---|---|---|
| 1 | Baseline | 2223 ms |
| 2 | With Cache | 1090 ms |
Result: 51% faster TTFT
Server-side metrics showed consistent cache key generation and TTFT dropping from 377ms to 337ms on subsequent cached requests.
| Option | Default | Description |
|---|---|---|
| select | system-head | Which prefix to cache. Options: system-head, tools+system, or a custom function |
| extraKeySalt | undefined | Additional data to include in cache key (useful for RAG chunk IDs) |
| onCacheResult | undefined | Callback with cache report after request completes |
| debug | false | Enable debug logging |
For full response caching with streaming replay:
import { withPromptCache, withResponseCache, MemoryStore } from '@krishgupta/ai-prompt-cache';
const store = new MemoryStore({ maxSize: 1000 });
const model = wrapLanguageModel({
model: openai('gpt-4o'),
middleware: [
withPromptCache({ select: 'system-head' }),
withResponseCache({ store, ttlSeconds: 3600 }),
],
});
Built-in stores:
// In-memory LRU cache
import { MemoryStore } from '@krishgupta/ai-prompt-cache';
const store = new MemoryStore({ maxSize: 1000 });
// File-based cache (for development)
import { FileStore } from '@krishgupta/ai-prompt-cache';
const store = new FileStore({ directory: '.cache' });
Custom stores need to implement:
interface CacheStore {
get(key: string): Promise<string | null>;
set(key: string, value: string, ttlSeconds?: number): Promise<void>;
}
The middleware generates a stable SHA-256 hash of the cacheable prefix (system prompt by default) and passes it to the provider:
OpenAI caches prompts with 1024+ tokens and reuses them in 128-token increments. Anthropic requires similar minimum thresholds depending on the model.
| Provider | Minimum Tokens |
|---|---|
| OpenAI | 1024 |
| Anthropic Claude Sonnet | 1024 |
| Anthropic Claude Haiku | 2048 |
MIT
FAQs
Production-grade prompt caching middleware suite for Vercel AI SDK
We found that @krishgupta/ai-prompt-cache demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.

Security News
Cloudflare is acquiring Deno’s team, which will provide another year of bug fixes and security updates before ending support for the runtime.

Security News
Anthropic’s free OSS Scanner uses Mythos and other Claude models to find open source vulnerabilities and send maintainers reports with proposed fixes.

Security News
On Front Lines’ Early Adopters: CISO podcast, Andrew Becherer shares lessons from Datadog, weighs the long-term costs of AI-built tools, and explains how founders can actually gain a CISO’s trust.