swr with a key and a loader function, and the server caches the result, revalidates stale entries in the background, and returns fresh data on every read.
All cache APIs are server-side only and require the LangGraph Agent Server runtime. Values must be JSON-serializable.
swr requires Agent Server runtime v0.7.79 or later and is currently in beta.
cache_get and cache_set require v0.7.29 or later.Quick start
Pass a key and an async loader function.swr returns the cached value if available, or calls your loader to fetch it:
swr awaits load_config() and caches the result. On subsequent calls, it returns the cached value instantly and revalidates in the background.
Configure freshness
Control how long cached values are considered fresh and when they expire:How revalidation works
Use with Pydantic models
Pass amodel parameter to automatically serialize and deserialize Pydantic models:
swr calls model_dump(mode="json") before storing and model.model_validate() when reading back.
Cache auth credentials
You can cache credential validation in a custom auth handler to avoid hitting your identity provider on every request:validate_and_fetch_user completes.
Inspect cache status
swr returns an SWRResult object with the value and cache status:
.mutate() to update the cached value or force a revalidation:
Low-level cache API
For simple get/set caching without revalidation, usecache_get and cache_set directly:
cache_get
None if the key does not exist or has expired.
cache_set
Next steps
- Add custom authentication to your deployment.
- Add custom lifespan events to initialize resources at server startup.
- Learn about the agent server architecture.
Connect these docs to Claude, VSCode, and more via MCP for real-time answers.

