New:Microsoft Teams Notifications Are Now Available in Socket.Learn more
Get Started

ai-prompt-cache-krxgu

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install
Package was removed
Sorry, it seems this package was removed from the registry

ai-prompt-cache-krxgu

A tiny middleware for the Vercel AI SDK that sets deterministic prompt cache hints across providers. It automatically:

latest
npmnpm
Version
0.1.0
Version published
Maintainers
1
Created
Source

@krishgupta/ai-prompt-cache

A tiny middleware for the Vercel AI SDK that sets deterministic prompt cache hints across providers. It automatically:

  • Generates a stable SHA-256 digest for the reusable system prefix.
  • Sets promptCacheKey for OpenAI models.
  • Marks Anthropic messages with the cacheControl metadata they expect.

Usage

import { wrapLanguageModel } from 'ai';
import { openai } from '@ai-sdk/openai';
import { withPromptCache } from '@krishgupta/ai-prompt-cache';

const model = wrapLanguageModel({
  model: openai('gpt-4o-mini'),
  middleware: withPromptCache({
    select: 'system-head',
    openai: { enable: true },
    anthropic: { enable: false },
    extraKeySalt: 'my-app',
  }),
});

Provider notes

  • OpenAI caches prompts once you pass 1024 tokens and reuses the cache in 128 token increments. Setting promptCacheKey greatly improves hit rate.
  • Anthropic caches when the pre-user messages include cacheControl markers and you exceed the model's minimum token requirements (e.g. 1024 tokens for Claude 3.5 Sonnet). This middleware sets the markers on the head system messages.

Options

OptionDefaultDescription
select'system-head'Which prefix of messages to hash. Supply a custom function to control the split.
openai.enabletrueToggle the OpenAI integration.
anthropic.enabletrueToggle the Anthropic integration.
anthropic.ttlundefinedOptional TTL for Anthropic's ephemeral cache.
extraKeySaltundefinedExtra data mixed into the hash (use RAG chunk IDs here).

Observability

Use Vercel AI Gateway dashboards to track TTFT improvements, cache reads, and write volume. OpenAI responses also expose providerMetadata.openai.cachedPromptTokens when the cache is being hit.

FAQs

Package last updated on 07 Nov 2025

Related posts