Agent Server includes a built-in cache you can use inside your deployed graphs. CallDocumentation Index
Fetch the complete documentation index at: https://docs.langchain.com/llms.txt
Use this file to discover all available pages before exploring further.
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:| Parameter | Default | Description |
|---|---|---|
fresh_for | timedelta(0) | Duration to treat a cached value as fresh. During this window, swr returns the cached value with no revalidation. |
max_age | timedelta(days=1) | Maximum lifetime of a cached entry. After this, swr blocks on the loader before returning. Capped at 1 day. |
How revalidation works
| Cache state | Condition | Behavior |
|---|---|---|
| Miss | Key not in cache | Awaits loader(), stores result, returns it. |
| Fresh | age < fresh_for | Returns cached value, no revalidation. |
| Stale | fresh_for <= age < max_age | Returns cached value immediately, triggers background refresh. |
| Expired | age >= max_age | Awaits loader(), stores result, returns it. |
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
| Parameter | Type | Default | Description |
|---|---|---|---|
key | str | required | The cache key |
value | Any | required | Value to cache. Must be JSON-serializable |
ttl | timedelta | None | None | Time-to-live. The server caps this at 1 day. None or zero defaults to 1 day |
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.

