LangChain Reference home pageLangChain ReferenceLangChain Reference
  • GitHub
  • Main Docs
Deep Agents
LangChain
LangGraph
Integrations
LangSmith
  • Overview
  • Client
  • AsyncClient
  • Run Helpers
  • Run Trees
  • Evaluation
  • Schemas
  • Utilities
  • Wrappers
  • Anonymizer
  • Testing
  • Expect API
  • Middleware
  • Pytest Plugin
  • Deployment SDK
  • RemoteGraph
⌘I

LangChain Assistant

Ask a question to get started

Enter to send•Shift+Enter new line

Menu

OverviewClientAsyncClientRun HelpersRun TreesEvaluationSchemasUtilitiesWrappersAnonymizerTestingExpect APIMiddlewarePytest PluginDeployment SDKRemoteGraph
Language
Theme
Pythonlangsmithprompt_cacheAsyncPromptCache
Class●Since v0.7

AsyncPromptCache

Thread-safe LRU cache with asyncio task refresh.

For use with the asynchronous AsyncClient.

Features:

  • In-memory LRU cache with configurable max size
  • Asyncio task for refreshing stale entries
  • Stale-while-revalidate: returns stale data while refresh happens
  • Thread-safe for concurrent access
Copy
AsyncPromptCache(
  self,
  *,
  max_size: int = DEFAULT_PROMPT_CACHE_MAX_SIZE,
  ttl_seconds: Optional[float] = DEFAULT_PROMPT_CACHE_TTL_SECONDS,
  refresh_interval_seconds: float = DEFAULT_PROMPT_CACHE_REFRESH_INTERVAL_SECONDS
)

Bases

_BasePromptCache

Example:

async def fetch_prompt(key: str) -> PromptCommit: ... return await client._afetch_prompt_from_api(key) cache = AsyncPromptCache( ... max_size=100, ... ttl_seconds=3600, ... fetch_func=fetch_prompt, ... ) await cache.start() cache.set("my-prompt:latest", prompt_commit) cached = cache.get("my-prompt:latest") await cache.stop()

Parameters

NameTypeDescription
max_sizeint
Default:DEFAULT_PROMPT_CACHE_MAX_SIZE

Maximum entries in cache (LRU eviction when exceeded).

ttl_secondsOptional[float]
Default:DEFAULT_PROMPT_CACHE_TTL_SECONDS

Time before entry is considered stale. Set to None for infinite TTL (offline mode - entries never expire).

refresh_interval_secondsfloat
Default:DEFAULT_PROMPT_CACHE_REFRESH_INTERVAL_SECONDS

How often to check for stale entries.

Constructors

constructor
__init__
NameType
max_sizeint
ttl_secondsOptional[float]
refresh_interval_secondsfloat

Methods

method
aset

Set a value in the cache.

method
start

Start async background refresh loop.

Must be called from an async context. Creates an asyncio task that periodically checks for stale entries and refreshes them. Does nothing if ttl_seconds is None (infinite TTL mode).

method
shutdown

Stop background refresh task.

Synchronous wrapper that cancels the refresh task. For proper cleanup in async context, use stop() instead.

method
stop

Stop async background refresh loop.

Cancels the refresh task and waits for it to complete.

method
configure

Reconfigure the cache parameters.

View source on GitHub