LangChain Reference home pageLangChain ReferenceLangChain Reference
  • GitHub
  • Main Docs
Deep Agents
LangChain
LangGraph
Integrations
LangSmith
LangSmith
  • Client
  • Run Trees
  • Traceable
  • Evaluation
  • Schemas
  • Langchain
  • Jest
  • Vitest
  • Wrappers
  • Anonymizer
  • Traceable
  • Jestlike
  • Vercel
  • Anthropic
  • Sandbox
⌘I

LangChain Assistant

Ask a question to get started

Enter to send•Shift+Enter new line

Menu

LangSmith
ClientRun TreesTraceableEvaluationSchemasLangchainJestVitestWrappersAnonymizerTraceableJestlikeVercelAnthropicSandbox
Language
Theme
JavaScriptlangsmithindexPromptCache
Class●Since v0.7

PromptCache

LRU cache with background refresh for prompts.

Features:

  • In-memory LRU cache with configurable max size
  • Background refresh using setInterval
  • Stale-while-revalidate: returns stale data while refresh happens
  • Uses the most recently used client for a key for refreshes
  • JSON dump/load for offline use
Copy
class PromptCache

Example

Copy
const cache = new Cache({
  maxSize: 100,
  ttlSeconds: 3600,
});

// Use the cache
cache.set("my-prompt:latest", promptCommit);
const cached = cache.get("my-prompt:latest");

// Cleanup
cache.stop();

Constructors

constructor
constructor

Properties

property
hitRate: number
property
metrics: Readonly<CacheMetrics>
property
size: string

Storage size (e.g., "1Gi", "10Gi").

property
totalRequests: number

Methods

method
clear

Clear all cache entries.

method
configure
method
dump

Dump cache contents to a JSON file for offline use.

method
get→ PromptCommit | undefined

Get a value from cache.

Returns the cached value or undefined if not found. Stale entries are still returned (background refresh handles updates).

method
invalidate

Remove a specific entry from cache.

method
load→ number

Load cache contents from a JSON file.

Loaded entries get a fresh TTL starting from load time.

method
resetMetrics

Reset all metrics to zero.

method
set

Set a value in the cache.

method
startRefreshLoop

Start the background refresh loop.

method
stop

Stop background refresh. Should be called when the client is being cleaned up.

View source on GitHub