All Agent Engineering

Managing Durable Work and Retries with Cloudflare Agent Queue

A checklist for queuing, retrying, observing and cancelling Agent tasks, written from reading the official Queue page only.

AINavWritten / Updated
On this page

At a glance

The built-in Agent queue stores tasks in SQLite and runs them in creation order. This guide is for the case where you already have pending tasks and callbacks that can fail, and it splits queuing, retry, observation and cancellation into steps you can record one by one. The only basis is the Queue tasks page; behavior it does not state is marked as unknown.

Runtime and applicability

Article type
Engineering practice
Verified
Applies to
  • Cloudflare Agents SDK Managing Durable Work and Retries with Cloudflare Agent Queue

Execution locations and responsibilities

  • Cloudflare Agent server and connected client Use the workflow below to distinguish responsibilities of the caller, runtime, and external actions.
Engineering workflow order
Engineering workflow order1. Inventory the callbacks → 2. Shrink the payload → 3. Choose the retry policy → 4. Make the callback idempotent and throw on failure → 5. Record the ID and watch the lifecycle → 6. Cancel and clean up. Arrows show the order of checks in the guide. Follow each step’s prerequisites, branches and stop conditions; unexecuted steps are not completed work.1. Inventory the callbacks2. Shrink the payload3. Choose the retry policy4. Make the callback idempotent and throw on failure5. Record the ID and watch the lifecycle6. Cancel and clean up

Arrows show the order of checks in the guide. Follow each step’s prerequisites, branches and stop conditions; unexecuted steps are not completed work.

When to use

  • queue() takes a callback method name, a payload and an optional retry, and returns the task ID. It validates at queue time that the callback exists; the callback receives payload first and queueItem second. The payload is stored as JSON in the cf_agents_queues table, so keep it small and serializable.

When not to use

  • The queue is sequential, so one long callback delays everything behind it; this plan does not cover parallel or priority processing
  • Work that must run at a given time or on a cycle belongs to schedule(), not the queue
  • The page does not describe how a task is kept after retries are exhausted, so this guide gives no query or replay procedure for failed tasks
  • getQueues() reads everything and filters afterwards, so it is a poor choice for frequent polling of a large queue

Prerequisites and input

  • A server-side class extending Agent, with a clear list of callbacks that should run through the queue
  • Access to that Agent's logs, or a connected client that can call its methods to read queue state

Engineering checks

Contract layer: callbacks and payloads — queue() takes a callback method name, a payload and an optional retry, and returns the task ID. It validates at queue time that the callback exists; the callback receives payload first and queueItem second. The payload is stored as JSON in the cf_agents_queues table, so keep it small and serializable.

Execution layer: order and retries — Tasks run sequentially in FIFO order by created_at, with no priority. If a callback throws, it is retried with exponential backoff; without retry, the class-level static options.retry defaults apply (3 attempts, 100ms base delay, 3s max delay). Successful tasks are removed automatically, and a callback that no longer exists at execution time is logged and skipped.

Recovery observation layer: lookup and cleanup — getQueue() looks up by ID and returns undefined when the task is absent; getQueues() loads all items and filters in memory by a payload key and value. dequeue(), dequeueAllByCallback() and dequeueAll() are synchronous. Tasks live in a table and survive an Agent restart.

1. Inventory the callbacks

Where: the Agent server class. Input: names of the pending tasks and failing callbacks. Action: confirm each name is a real method on the class and accepts payload then queueItem. Completion check: every task maps to one method. Stop condition: a name has no method; the documentation says it is logged and skipped at execution, so fix it before queuing.

2. Shrink the payload

Where: the code that calls queue(). Input: the data the task needs. Action: keep only small JSON-serializable data and include a stable key, such as a user identifier, so getQueues() can filter later. Completion check: no large objects or non-serializable values. Stop condition: the data is large; store it elsewhere and pass a reference.

3. Choose the retry policy

Where: the options argument of queue(). Input: the category of failure. Action: decide whether to pass retry explicitly (with maxAttempts, baseDelayMs, maxDelayMs) or rely on the class-level defaults, and note why. Completion check: the record states the attempts and delays actually used. Stop condition: if the callback cannot safely run twice, do not add more retries yet.

4. Make the callback idempotent and throw on failure

Where: inside the callback method. Input: the external actions the callback performs. Action: throw on a failed request so the retry fires, and design the work so a second run of the same task has no duplicate effect. Completion check: each external action has a written outcome for repeated execution. Stop condition: an action cannot be made safe to repeat; stop enabling retries and add separate deduplication.

5. Record the ID and watch the lifecycle

Where: the connected client caller and Agent logs. Input: the task ID returned by queue(). Action: store the ID, log with queueItem inside the callback, and use getQueue() when needed. Completion check: you can tell "still queued" from "no longer present". Stop condition: undefined with no success log is not a conclusion; the page does not say where a task goes after retries run out, and it cannot be told apart from one removed by dequeue().

6. Cancel and clean up

Where: the Agent server. Input: the task ID or callback name to withdraw. Action: use dequeue() for one task, dequeueAllByCallback() for one callback, and dequeueAll() only after confirming no other callback has pending work. Completion check: getQueue() returns undefined for the cancelled ID. Stop condition: if the affected scope is unclear, do not clear the queue.

Task example

This is a synthetic walkthrough, not an actual run. Suppose an Agent has a callback syncRecords whose payload carries userId and a batch number. Step 1 confirms the method exists, and retry sets maxAttempts to 4. Expected: while an external service is briefly unavailable the callback throws and the task is retried with backoff; after recovery a success log appears and getQueue() returns undefined for that ID. If every attempt fails, an observer sees only repeated error logs, and the task's final state has to be confirmed some other way because the documentation does not define it.

Completion checks

  • Every queued callback maps to a real method and carries a small JSON payload
  • Attempts and delays are written down, with a note on whether they come from explicit retry or class-level defaults
  • Idempotency was checked per action rather than assumed
  • The observation record holds the task ID, the success log and the getQueue() result
  • The cleanup scope is stated: one task, one callback, or everything

Verification scope

This guide comes from reading the official Queue tasks page (updated 27 October 2026). Installation, login, API execution and output were not tested, and every observation in the example is hypothetical.

Sources

All links are official pages; verification dates are listed per source, and links point to the version checked.

  1. Cloudflare Agents · Cloudflare Agents documentation: cf-queue

Found an error? Tell us →