Skip to Content
DevelopSnapshotting

User-Defined Snapshotting

Golem recovers agent state by replaying the oplog — the log of all operations performed by the agent. For long-running or CPU-heavy agents, this replay can become slow. Golem 1.5 introduces user-defined snapshotting, allowing agents to opt in to periodic snapshots so that recovery only needs to replay entries recorded after the last snapshot.

Snapshotting also enables manual agent updates between incompatible versions — see Updating Agents for details.

Snapshotting is optional. If you do not configure it, Golem will continue to recover agents by replaying the full oplog.

Default snapshotting implementation

Each language SDK provides a built-in snapshotting implementation that requires no custom code. As long as your agent’s state is serializable, you can rely on the default implementation.

Simply do not define loadSnapshot and saveSnapshot on your agent class. The SDK will automatically serialize the agent instance as JSON.

Custom snapshotting

You can implement custom save/load logic to control exactly how agent state is serialized. The following example shows a simple counter agent that saves its count as raw bytes:

Override saveSnapshot and loadSnapshot on your agent class:

override async saveSnapshot(): Promise<Uint8Array> { const snapshot = new Uint8Array(4); const view = new DataView(snapshot.buffer); view.setUint32(0, this.value); return snapshot; } override async loadSnapshot(bytes: Uint8Array): Promise<void> { let view = new DataView(bytes.buffer); this.value = view.getUint32(0); }

Configuring snapshot-based recovery

To enable snapshotting, add a snapshotting configuration to your agent definition annotation:

@agent({ snapshotting: { periodic: '5s' } })

The available snapshotting options are:

OptionDescription
disabledSnapshotting is turned off. Recovery replays the full oplog.
enabledUses the server-side default snapshotting configuration.
every(N)Takes a snapshot every N oplog entries.
periodic(duration)Takes a snapshot every duration interval (e.g. 5s, 1m).

Observability

When using the default (JSON-based) snapshotting implementation, snapshot entries recorded in the oplog contain the serialized JSON state of the agent. This makes it easy to inspect agent state at any snapshot point for debugging purposes.

Snapshotting is also used when updating agents across incompatible versions, allowing Golem to capture the agent’s state before applying the update.

Last updated on