Long-running agents are in preview. APIs and package versions are subject to change.
Two layers of state
The rule: metadata is a checkpoint index, not a checkpoint store. Small writes are cheap and fast; bulk writes hit task-store payload limits and slow recovery.
Use task metadata for watermarks
ctx.metadata is a small key-value namespace that survives crashes and is visible across turns of a chain. Values must be JSON-serializable.
flush() when the metadata write must land before a side effect that can’t be deduplicated. Names beginning with _ are reserved for the framework and raise ValueError.
Three useful scopes:
Store bulk state in the Foundry state store
FoundryStateStore is a durable, server-backed key-value store for state that must survive crashes and idle-eviction. A store is bound to one caller-chosen name; encode your scope (session, thread, or run) into that name.
get_or_create()fetches or creates the store in one call; it appliesuser_isolation/item_ttl_secondsonly on first creation.- Store name = scope. Names can contain
/; use it as a hierarchy separator and choose a stable scheme up front. - Optimistic concurrency. Use
if_match=item.etagfor mutable items like counters; skip it on append-only checkpoints. A failed precondition raisesFoundryStoragePreconditionError. - Limits. Item value ≤ 1 MB serialized; store name 1–128 chars; up to 16 tags per item.
Back a framework checkpointer
Point a LangGraph or Microsoft Agent Framework (MAF) checkpointer atFoundryStateStore and the framework’s own recovery becomes durable across crashes - no custom recovery code.
Checkpoints are append-only - each save uses a fresh ID, so there’s no write contention and you never need
if_match on the checkpoint path.
Keep inputs small
Task inputs are persisted before the handler runs (that’s what recovery rests on), so keep them small - the per-input limit is about 10 MiB after JSON serialization, and larger inputs raiseInputTooLarge before any network call. Externalize big payloads to blob storage and pass a reference.