agent_with_reconnect
Focused demo of GetAgentStream: start a Temporal stream, simulate a mid-run crash after the first text chunk, then reconnect from a saved runID + event offset. Events print their offsets so you can see what to persist.
Shared agent/worker options live in opts/opts.go. For crash/kill scenarios across separate processes, see durable_agent.
Prerequisites
This example requires Temporal (WithTemporalConfig in shared opts). Local runtime has no stream offsets, so reconnect is unsupported.
- Configuration — set up
examples/.env first: Configuration in examples/README.md.
- Working directory — run commands from
examples/.
- Temporal — start the server:
task infra:temporal:up && task infra:temporal:wait
Set (or accept defaults from .env.defaults):
AGENT_RUNTIME=temporal
TEMPORAL_HOST=127.0.0.1
TEMPORAL_PORT=7233
TEMPORAL_NAMESPACE=default
TEMPORAL_TASKQUEUE=agent-sdk-go # or your override
Quick start (single process)
Embedded worker — no second terminal:
AGENT_RUNTIME=temporal go run ./agent_with_reconnect/agent "What time is it?"
What happens
- Phase A —
Stream starts; runID is taken from agentStream.ID() before events are consumed. The example tracks each event’s Offset(), then cancels the stream context after the first TEXT_MESSAGE_CONTENT chunk (simulated crash).
- Phase B —
GetAgentStream(ctx, runID) + Events(ctx, WithOffset(lastOffset)) resumes from that offset; remaining events (replay + live) print until RUN_FINISHED.
agentStream, err := a.Stream(ctx, prompt, nil)
runID := agentStream.ID()
eventCh, err := agentStream.Events(ctx)
// ... track Offset() on each event; cancel after first text chunk ...
agentStream, err = a.GetAgentStream(ctx, runID)
eventCh, err = agentStream.Events(ctx, agent.WithOffset(lastOffset))
Live workflow only. Once the workflow completes, the stream log is no longer available for replay. This example cancels only the subscriber context so the embedded worker keeps running and the workflow stays active for Phase B.
Optional: separate worker
# Terminal 1
AGENT_RUNTIME=temporal go run ./agent_with_reconnect/worker
# Terminal 2
AGENT_RUNTIME=temporal go run ./agent_with_reconnect/agent "What time is it?"
The agent binary uses an embedded worker by default. For a true agent/worker split (DisableLocalWorker), use agent_with_worker or durable_agent.
Caller-side protocol
- On
Stream start, save runID (agentStream.ID()) with your correlation key.
- Track
Offset() on each received event.
- On restart:
GetAgentStream(ctx, savedRunID), then Events(ctx, WithOffset(savedOffset)).
- Discard events at offset ≤ saved offset if you need strict deduplication.
- Clear saved state on
RUN_FINISHED or RUN_ERROR.