Agent state

Read session.agentState to show the backend's progress during a run.

session.agentState exposes the backend's state for the current thread, such as its current step. AG-UI STATE_SNAPSHOT replaces this state. STATE_DELTA applies a JSON Patch.

A widget can show these updates before the agent calls a method. The example's test includes this widget:

final class const _AgentStep() extends StatelessWidget {
  @override
  Widget build(BuildContext context) =>
      switch (TandemSessionScope.of(context).agentState) {
        {'step': final String step} => Text(step),
        _ => const SizedBox.shrink(),
      };
}

Read the backend's state

Each backend defines the shape of this state. The reference agents use these conventions:

BackendState
Google ADKIts session state, including adapter keys prefixed with _ag_ui_.
LangGraphThe graph's output keys, including conversation history in messages.
Pydantic AIState emitted by a tool as StateSnapshotEvent or StateDeltaEvent, read back with StateDeps.

Each run sends the latest value back as AG-UI state, so a backend that keeps no state between runs receives it again. The app reads this value. Send app activity to the agent through notes.

State updates continue after Stop while Tandem drains the run, so the next run sends the backend its latest state. The state is empty in a new thread and after a reset.

If a snapshot is not a JSON object or a delta cannot be applied, Tandem reports the error and keeps the previous state. The run continues. Further deltas are ignored until a valid snapshot or a new run, because they refer to a state the app does not have.

In tests, use AgentStep.stateSnapshot and AgentStep.stateDelta with ScriptedAgent to send state updates. This script holds the run between them until the test completes history:

    final history = Completer<void>();
    final agent = ScriptedAgent([
      (_) => [
        const AgentStep.stateSnapshot({'step': 'Reading the order history'}),
        AgentStep.waitFor(history.future),
        const AgentStep.stateDelta([
          {'op': 'replace', 'path': '/step', 'value': 'Filling the order'},
        ]),
        const AgentStep.say('Done.'),
      ],
    ]);