Middleware that persists per-checkpoint state needed to resume a thread.
Registers two checkpointed, schema-private channels and writes them from
after_model:
_context_tokens — total context tokens from the latest
AIMessage.usage_metadata. Powers /tokens and the status bar._model_spec — the provider:model spec that was effectively in use for
the turn, read from runtime.context["effective_model"]. Lets dcode -r
restore the model the resumed thread was actually using instead of falling
back to the user's global default.Both are facts the CLI reads back from state_values on thread resume so it
can rehydrate the session without replaying or re-tokenizing history.
Persisting from inside the graph (rather than via a separate client-side
aupdate_state call) keeps the write on the same checkpoint as the model
response and avoids creating a standalone UpdateState run in LangSmith.
Because both values are versioned channel state, resuming a specific
checkpoint yields the values as of that checkpoint — not a thread-level
aggregate. It also works identically against local and remote (HTTP) graphs.