The PromptCache class provides an in-memory LRU cache with background refresh functionality, specifically designed for caching prompts from the LangSmith Hub.
PromptCache
Constructor
import { PromptCache } from "langsmith" ;
const cache = new PromptCache ({
maxSize: 100 ,
ttlSeconds: 3600 ,
refreshIntervalSeconds: 60 ,
});
Configuration options for the cache
Maximum entries in cache. LRU eviction when exceeded. Set to 0 to disable caching.
ttlSeconds
number | null
default: "3600"
Time in seconds before entry is stale. Set to null for infinite TTL.
How often to check for stale entries in seconds.
Methods
get()
Get a value from cache.
const prompt = cache . get (
"my-prompt:latest" ,
async () => await client . getPrompt ( "my-prompt:latest" )
);
refreshFunc
() => Promise<PromptCommit>
required
Function to refresh the value when stale.
The cached value or undefined if not found. Stale entries are still returned (background refresh handles updates).
set()
Set a value in the cache.
cache . set (
"my-prompt:latest" ,
promptCommit ,
async () => await client . getPrompt ( "my-prompt:latest" )
);
refreshFunc
() => Promise<PromptCommit>
required
Function to refresh the value when stale.
invalidate()
Remove a specific entry from cache.
cache . invalidate ( "my-prompt:latest" );
clear()
Clear all cache entries.
stop()
Stop background refresh. Should be called when the cache is being cleaned up.
dump()
Dump cache contents to a JSON file for offline use.
cache . dump ( "/path/to/cache.json" );
Path to save the cache file.
load()
Load cache contents from a JSON file. Loaded entries get a fresh TTL starting from load time.
const numLoaded = cache . load ( "/path/to/cache.json" );
Number of entries loaded.
Reconfigure the cache.
cache . configure ({
maxSize: 200 ,
ttlSeconds: 7200 ,
});
Properties
The number of entries in the cache.
Cache performance metrics. interface CacheMetrics {
hits : number ;
misses : number ;
refreshes : number ;
refreshErrors : number ;
}
Total cache requests (hits + misses).
Cache hit rate (0.0 to 1.0).
resetMetrics()
Reset all metrics to zero.
Global prompt cache
The SDK provides a global singleton cache for prompts.
Configure the global prompt cache.
import { configureGlobalPromptCache } from "langsmith" ;
configureGlobalPromptCache ({
maxSize: 200 ,
ttlSeconds: 7200 ,
refreshIntervalSeconds: 120 ,
});
Cache configuration options.
Client integration
By default, the LangSmith client uses the global prompt cache. You can disable it per-client:
import { Client } from "langsmith" ;
const client = new Client ({
disablePromptCache: true ,
});
Features
LRU eviction
When the cache reaches maxSize, the least recently used entry is evicted:
const cache = new PromptCache ({ maxSize: 3 });
cache . set ( "key1" , value1 , refresh1 );
cache . set ( "key2" , value2 , refresh2 );
cache . set ( "key3" , value3 , refresh3 );
// This will evict key1
cache . set ( "key4" , value4 , refresh4 );
Stale-while-revalidate
Stale entries are still returned while being refreshed in the background:
const cache = new PromptCache ({
ttlSeconds: 60 ,
refreshIntervalSeconds: 10 ,
});
// Initial fetch
const prompt1 = cache . get ( "my-prompt" , fetchPrompt );
// After 70 seconds, entry is stale but still returned
const prompt2 = cache . get ( "my-prompt" , fetchPrompt );
// Background refresh happens automatically
Background refresh
The cache automatically refreshes stale entries in the background:
const cache = new PromptCache ({
ttlSeconds: 300 , // 5 minutes
refreshIntervalSeconds: 60 , // Check every minute
});
// Cache will check for stale entries every 60 seconds
// and refresh them if TTL has expired
Persistence
Dump and load cache for offline use:
const cache = new PromptCache ();
// Populate cache
cache . set ( "prompt1" , value1 , refresh1 );
cache . set ( "prompt2" , value2 , refresh2 );
// Save to disk
cache . dump ( "./prompts.cache.json" );
// Later, in another process
const newCache = new PromptCache ();
const numLoaded = newCache . load ( "./prompts.cache.json" );
console . log ( `Loaded ${ numLoaded } prompts` );
Example usage
Basic usage
import { PromptCache , Client } from "langsmith" ;
const cache = new PromptCache ({
maxSize: 100 ,
ttlSeconds: 3600 ,
});
const client = new Client ();
const getPrompt = async ( key : string ) => {
const cached = cache . get ( key , async () => {
return await client . getPrompt ( key );
});
if ( cached ) {
console . log ( "Cache hit!" );
return cached ;
}
console . log ( "Cache miss, fetching..." );
const prompt = await client . getPrompt ( key );
cache . set ( key , prompt , async () => await client . getPrompt ( key ));
return prompt ;
};
const prompt = await getPrompt ( "my-prompt:latest" );
With metrics
const cache = new PromptCache ();
// Use the cache
for ( let i = 0 ; i < 100 ; i ++ ) {
cache . get ( `prompt- ${ i % 10 } ` , fetchPrompt );
}
// Check metrics
console . log ( `Hit rate: ${ ( cache . hitRate * 100 ). toFixed ( 1 ) } %` );
console . log ( `Total requests: ${ cache . totalRequests } ` );
console . log ( `Metrics:` , cache . metrics );
Disable caching
Set maxSize to 0 to disable caching:
const cache = new PromptCache ({ maxSize: 0 });
// This will always return undefined
const result = cache . get ( "key" , fetchFunc );
Cache (deprecated)
The Cache class is deprecated. Use PromptCache instead.
import { Cache } from "langsmith" ;
// This will log a deprecation warning
const cache = new Cache ();