Skip to content

API Reference

createCachedClient(client, options?)

Wraps any OpenAI-compatible client with caching via JavaScript Proxy. Returns a value with the same TypeScript type as the original client.

ts
import { createCachedClient } from 'llm-cacher'

const openai = createCachedClient(new OpenAI(), { ttl: '24h' })

createCachedAnthropicClient(client, options?)

Same as createCachedClient but intercepts client.messages.create.

ts
import { createCachedAnthropicClient } from 'llm-cacher'

const anthropic = createCachedAnthropicClient(new Anthropic(), { ttl: '24h' })

LlmCacheOptions

OptionTypeDefaultDescription
ttlstring | numberundefinedTime-to-live. String: "24h", "30m", "7d", "500ms". Number: milliseconds. No TTL means entries never expire.
storage'memory' | 'file' | 'sqlite' | IStorage'memory'Storage backend. 'file' and 'sqlite' use storagePath for the file location. Pass an IStorage instance for Redis/DynamoDB.
storagePathstringsee belowPath used when storage is 'file' (default ./llm-cacher.json) or 'sqlite' (default ./llm-cacher.db).
maxSizenumber1000Max entries for 'memory' storage. Ignored for other backends.
onStorageError'throw' | 'passthrough''passthrough'What to do when storage read/write fails. 'passthrough' falls through to the LLM silently.
semanticSemanticOptionsundefinedEnable semantic (vector) matching.

SemanticOptions

OptionTypeDefaultDescription
embedderIEmbedderrequiredThe embedding model to use.
thresholdnumber0.92Minimum cosine similarity (0–1) to count as a hit.
indexType'flat' | 'hnsw''flat'Search index. Use 'hnsw' for 10k+ entries.

Storage classes

MemoryStorage

ts
new MemoryStorage({ maxSize?: number, sweepIntervalMs?: number })
OptionDefaultDescription
maxSize1000Max entries. Oldest entry is evicted when full (LRU).
sweepIntervalMs60000How often to run expired-entry cleanup (ms).

FileStorage

ts
new FileStorage({ path: string })

RedisStorage

ts
new RedisStorage({ client?: Redis, url?: string, options?: RedisOptions, keyPrefix?: string })
OptionDefaultDescription
keyPrefix'llm-cacher:'Prefix applied to every Redis key.

SQLiteStorage

ts
new SQLiteStorage({ path?: string, db?: Database, tableName?: string })
OptionDefaultDescription
path'llm-cacher.db'Path to the SQLite file.
tableName'llm_cache'Table name used inside the database.

DynamoDBStorage

ts
new DynamoDBStorage({
  tableName: string,
  region?: string,
  client?: DynamoDBClient,
  config?: DynamoDBClientConfig,
  keyAttribute?: string,
  valueAttribute?: string,
  ttlAttribute?: string,
})
OptionDefaultDescription
keyAttribute'pk'Partition key attribute name.
valueAttribute'value'Attribute used to store the serialized entry.
ttlAttribute'ttl'Attribute used for DynamoDB native TTL.

Embedders

LocalEmbedder

ts
new LocalEmbedder({ model?: string })
OptionDefaultDescription
model'Xenova/all-MiniLM-L6-v2'HuggingFace model ID.

dimensions: 384

OpenAIEmbedder

ts
new OpenAIEmbedder({ client: OpenAI, model?: string, dimensions?: number })
OptionDefaultDescription
model'text-embedding-3-small'OpenAI embedding model.
dimensions1536Output dimensions.

IStorage interface

ts
interface IStorage {
  get(key: string): Promise<CacheEntry | null>
  set(key: string, entry: CacheEntry): Promise<void>
  delete(key: string): Promise<void>
  clear(): Promise<void>
}

CacheEntry

ts
interface CacheEntry {
  key: string
  type: 'full' | 'stream'
  value: unknown
  chunks?: unknown[]
  createdAt: number   // ms timestamp
  expiresAt: number | null
}

IEmbedder interface

ts
interface IEmbedder {
  readonly dimensions: number
  embed(text: string): Promise<number[]>
}

Released under the MIT License.