Courses / Agentic AI / Module 14
Module 14: Durable execution and deployment: Temporal, queues, retries, long-running tasks
12 h · theory 4 · hands-on 5 · project 2 · revision 1
You will be able to
- Run long tasks as durable workflows with Temporal
- Retry, time out and compensate safely
- Deploy the agent service with Docker and CI/CD
- Scale workers
Capstone increment: Capstone live at a public HTTPS URL with Temporal workers and CI/CD.
Job-description skills: Durable execution, Distributed systems, Cloud · Lab tools: temporalio, docker, docker-compose, gh
1. Agent runs last minutes to days
A real incident run calls tools that time out, waits hours for an approval and spans deploys of your own service. A Python process holding the run in memory loses everything when it restarts. Durable execution keeps a run’s progress outside the process so any worker can continue it after a crash, a deploy or a week-long wait.
Temporal is the most widely used open-source engine for this. Workflows are your control flow; activities are the model calls and tool calls; the Temporal service records every result in an event history.
2. Workflows, activities and replay
Workflow code must be deterministic: no direct network calls, random numbers or clock reads; those go in activities or through the SDK’s safe APIs. Activities do the real work and can fail and be retried. When a worker crashes, another worker replays the workflow from its history: completed activities return their recorded results instead of running again, and the workflow continues from where it was.
Signals send data into a running workflow (an approval, a cancellation); queries read its state; durable timers wait for hours or days without holding a process. Try the crash simulator to see what restarting from scratch costs compared with resuming.
Duplicate side effects 3 · model calls paid twice ₹18 · extra hours waiting for a second approval 3
- Fetch logs and metrics read onlyran 2×
- Diagnose with the model model call ₹18ran 2×
- Open a change ticket side effect: ticketran 2×
- Wait for human approval waits about 3 hran 2×
- Apply the config change side effect: config changeran 2×
- Notify the on-call channel side effect: messageran 2× (crashed here)
Guided experiments
- Crash during the last step and restart from the top. Count the duplicate tickets, config changes and messages. Who has to approve the change again?
- Now resume from recorded progress. Which step still runs twice, and why can no engine avoid that for a step that crashed after its side effect?
- Turn on idempotency keys. Why does “at-least-once” execution plus idempotent steps behave like “exactly once”?
- Crash while waiting for approval and resume. Why does the wait survive? What would it take to get that with a plain Python script and a cron job?
3. Retries, timeouts and compensation
Give every activity a start-to-close timeout and a retry policy with backoff, and mark errors that must not be retried (invalid arguments, permission denied). Long activities should heartbeat so a stuck one is detected and retried. Activities run at least once, so side-effecting ones need idempotency keys (Module 2).
When a multi-step change fails partway, compensate: undo completed steps in reverse order (the saga pattern). Some actions can’t be undone; gate those behind approval.
4. Deploy the agent service
Package the API and the workers as Docker images. For development, run the Temporal dev server, Postgres and your services with docker compose; in production, use Temporal Cloud or a self-hosted cluster. Keep model keys and tool credentials in a secret manager, and pin model versions in config.
Deploy from CI after tests and the eval gate pass. Changing workflow code that has runs in flight needs care: use the SDK’s versioning so old runs replay with the code they started with.
5. Scale workers
Workers poll task queues, so scaling is adding workers. Use separate queues for model calls and for tools, so a slow provider doesn’t starve fast tools, and cap concurrency per queue to stay inside provider rate limits. Scale on queue backlog rather than CPU.
Watch schedule-to-start latency (how long tasks wait for a worker) and activity failure rates on the dashboard, alongside the agent metrics from Module 12.
Hands-on: coding window
The coding window and the full VS Code lab are available after you sign in.
Sign in to codeLabs
Lab 14.1: Remediation as a workflow
Model the incident run as a Temporal workflow with activities for model calls, tools and ticketing, each with timeouts, retries and idempotency keys; approval arrives as a signal.
Expected: A workflow that waits for approval and completes after it.
Lab 14.2: Kill the worker
Start a run, kill the worker during an activity, start another worker, and show the run completing. Check the ticketing system for duplicates.
Expected: The run completes; history shows one recorded result per activity; no duplicate tickets.
Lab 14.3: Deploy (capstone increment)
Deploy the API and workers with docker compose behind Caddy, connected to Temporal, with a CI/CD workflow gated on tests and evals.
Expected: Capstone live at a public HTTPS URL with Temporal workers and CI/CD.
Go deeper
Videos
- Temporal in 7 Minutes - the TL;DR Intro · Temporal · ~7 min. Temporal in seven minutes: workflows, activities and why durable execution matters.
- AI and the Mechanics of Durable Execution · Temporal · ~42 min. How durable execution works under the hood, applied to AI agents.
- Build an AI Agent with Temporal · Temporal · ~5 min. A short build of an AI agent on Temporal.
Further reading
- Docs Temporal documentation · Temporal. Workflows, activities, retries, timers and signals.
- Docs Temporal Workflow · Temporal docs. Why workflow code must be deterministic and how replay rebuilds state after a crash.
- Docs Saga design pattern · Azure Architecture Center. Compensating actions for multi-step work that can't be one transaction.
- Paper Sagas · Garcia-Molina and Salem, 1987. The original paper: long-lived transactions as steps with compensations.
GitHub repositories
- temporalio/sdk-python: Workflow sandbox rules, activity options and signals in the Python SDK.
- temporalio/samples-python: Samples for signals, sagas, schedules and AI agents; run the hello samples first.
- temporalio/temporal: The server; read the docs on event history and task queues.
- docker/compose: Running the dev server, workers and API together with healthchecks.
Checkpoint quiz
Interview questions
- Why do agents need durable execution? What breaks without it?
- Explain workflows, activities and replay in Temporal.
- How would you implement a human approval that may take two days?
- What is a saga, and when would you use one in an ops agent?
- How do you deploy a change to workflow code while runs are in flight?
Ask the professor
Sign in to ask questions about this module.
Discussion
Community discussion appears here once giscus is configured (see README).