The latest Cloudflare example shows how to host the Codex agent loop inside a Durable Object, turning the previously stateless Workers model into a stateful, recoverable service. By wiring the loop into the Agents SDK, engineers gain automatic persistence of conversations, files, and model/tool results, which is critical for long‑running or interrupted workflows.
How the Durable Object Harness Works
The example adapts Codex’s Rust turn‑processing code to WebAssembly and embeds it in a DurableObject subclass. The object is instantiated by the Workers runtime and receives a storage context (this.ctx.storage) that backs the Agents SDK primitives. The harness creates a CodexHarness instance, passing in the SDK components so that each turn of the loop can read and write durable state.
import { DurableObject } from "cloudflare:workers";
import { Workspace } from "@cloudflare/computer";
import { Tasks } from "agents/tasks";
import { Sessions } from "agents/sessions";
import { Streams } from "agents/streams";
import { CodexHarness } from "./codex-harness";
export class Coder extends DurableObject {
tasks = new Tasks();
streams = new Streams();
sessions = new Sessions();
workspace = new Workspace({ storage: this.ctx.storage });
codex = new CodexHarness({ tasks: this.tasks, streams: this.streams, sessions: this.sessions, workspace: this.workspace });
}
Key Agents SDK Primitives
- Tasks – Journals each model call and tool execution. Completed steps are replayed from the journal after a restart, while interrupted steps can be retried.
- Sessions – Holds the conversation history and performs context compaction, reducing token usage for subsequent model invocations.
- Streams – Records ordered events, enabling deterministic replay when a client reconnects.
- Workspace – Exposes a file‑system‑like API backed by Durable Object storage, allowing agents to read and write persistent files.
These primitives are combined to give the Codex loop a durable execution environment without additional infrastructure.
Operational and Security Implications
From an operations perspective, the durable state means that a single instance can survive edge restarts, scaling events, or transient failures. Engineers should monitor the storage quota of the Durable Object and plan for periodic compaction of Sessions and Streams to avoid unbounded growth.
Security considerations stem from the fact that all persisted data lives in the Durable Object’s storage namespace. Access is limited to the object’s own code, but any secret used to configure the AI model (for example, an API key for OpenAI’s GPT‑6.1 Sol via AI Gateway) must be injected through environment variables or Workers secrets, not stored in the object’s journal. The SDK does not perform additional authorization checks on the persisted data, so developers should treat the storage as a trusted boundary and avoid exposing it to unvalidated input.
Related CloudNinjas coverage: AI engineering.
What This Means For Practitioners
Practitioners can now build edge‑native agents that survive interruptions without external databases. The pattern is useful for any workflow that requires multi‑turn reasoning, file manipulation, or tool usage where state continuity is essential. Evaluate the storage cost model of Durable Objects, implement periodic cleanup of session and stream logs, and keep model credentials out of persisted state. Future updates to the Agents SDK may add richer lifecycle hooks, so keep an eye on the repository for enhancements that simplify cleanup or add observability features.
