Cloudflare Agents, Durable Objects & Stateful Workflows in TypeScript
course lessons.
1 Section · 2 LessonsCore Architecture & Implementation
- 1.1Durable Objects Mental Model: Single-Threaded Actors + Embedded SQLite
What if every single AI agent session or collaborative room in your app had its own dedicated, zero-latency SQLite database co-located inside the exact same thread of compute? That is Cloudflare Durable Objects.
CloudflareAI & AgentsTypeScript+2▶ Video + LessonStart Lesson → - 1.2Real-Time Streaming & WebSocket Hibernation with the Cloudflare Agents SDK
Keeping ten thousand WebSockets open to an AI agent used to mean paying for idle server memory all day long. Let's see how WebSocket Hibernation and the Cloudflare Agents SDK keep sockets open while billing zero during idle pauses.
CloudflareAI & AgentsTypeScript+2▶ Video + LessonStart Lesson →
course overview.
1. Production Architecture Problem & Why We Built This Course
Build stateful, long-running AI agents and resilient multi-step workflows using the Cloudflare Agents SDK, Durable Objects with embedded SQLite, WebSockets hibernation, and Workflows. In production engineering teams, the gap between a quick weekend demo and a resilient, multi-tenant system comes down to three architectural boundaries: cold-start latency, compile-time contract validation, and predictable operational cost. Throughout Cloudflare Agents, Durable Objects & Stateful Workflows in TypeScript, we strip away marketing buzzwords and build the exact TypeScript, edge compute, and structured data patterns that power CodingCat.dev in production.
Every lesson in this course pairs a dark-mode Architecture Diagram (contrasting the legacy anti-pattern on the left against the recommended 2026 production pattern on the right) with copy-pasteable TypeScript modules, automated verification checks, and an explicit Trade-Off Matrix. Whether you are reading the official Cloudflare Agents SDK Official Documentation or pairing this course with our deep dive on Blog Post: The 2026 Persistent AI Agent Landscape, you can apply every pattern directly to your own repository today.
🐾 AJ's Course Architecture Rule: Never adopt a tool just because it trended on social media. Measure its cold start in milliseconds, verify its TypeScript schema boundary at compile time, and check what happens to your cloud bill when traffic spikes 100x.
2. Lesson-by-Lesson Architectural Walkthrough (2 Lessons)
Work through the lessons in sequence to build the complete system from first principles, or jump directly to the specific architectural boundary you are solving right now:
- Lesson 1: Durable Objects Mental Model: Single-Threaded Actors + Embedded SQLite: Replaces the
RACE CONDITIONS (Stateless Workers, External Redis Hop, Lock Contention)anti-pattern withDurable Object -> 1 Actor per Room/Agent + In-Memory + SQLiteinsidesrc/agents/session-do.ts. What if every single AI agent session or collaborative room in your app had its own dedicated, zero-latency SQLite database co-located inside the exact same thread of compute? That is Cloudflare Durable Objects. - Lesson 2: Real-Time Streaming & WebSocket Hibernation with the Cloudflare Agents SDK: Replaces the
IDLE WS BILLS (10k Open Sockets, Charged for Idle, Dropped Reconnect)anti-pattern withctx.acceptWebSocket -> Socket Stays Open + Actor Sleeps Freeinsidesrc/agents/codingcat-agent.ts. Keeping ten thousand WebSockets open to an AI agent used to mean paying for idle server memory all day long. Let's see how WebSocket Hibernation and the Cloudflare Agents SDK keep sockets open while billing zero during idle pauses.
3. Alex's 2026 Ecosystem & Architectural Tier List
Before writing a single line of configuration, you need to know how the available platforms and patterns stack up in real production environments. Here is how I rank the ecosystem from S-Tier (our default production choice on CodingCat.dev) down to D-Tier anti-patterns to avoid:
Alex's Stateful Edge Compute & Realtime Agent Memory Tier List
Comparing 4 architectures for maintaining per-agent conversation memory, live WebSockets, and long-running background tasks.
Cloudflare Agents SDK + Durable Objects Embedded SQLite + WS Hibernation ↗
Single-Threaded Actor + Co-Located SQLiteGives every individual agent session or collaborative room its own dedicated SQLite database in the same thread of compute, with WebSocket Hibernation dropping idle socket cost to $0.
Cloudflare Workflows + D1 Global Relational Store ↗
Durable Multi-Step OrchestrationPersists step outputs automatically and supports durable sleep timers lasting minutes or days without holding a compute isolate open.
Stateless Serverless Functions + External Redis Pub/Sub
Networked State StoreWorks for basic caching, but every state transition requires a network roundtrip to Redis and distributed locking to prevent race conditions.
Always-On Node.js Container Holding In-Memory Map() State
Fragile Stateful ContainerDrops every active WebSocket and wipes in-memory conversation state on every deploy, scale-down event, or process crash.
4. Production Starter Configuration & Local Setup
To follow along with the code examples in Cloudflare Agents, Durable Objects & Stateful Workflows in TypeScript, start with this baseline wrangler.jsonc configuration. It enables strict TypeScript verification, observability tracing, and local development bindings that match production 1-to-1:
{
"$schema": "node_modules/wrangler/config-schema.json",
"name": "codingcat-stateful-agent",
"main": "src/agents/codingcat-agent.ts",
"compatibility_date": "2026-03-01",
"compatibility_flags": ["nodejs_compat"],
"durable_objects": {
"bindings": [
{ "name": "CODINGCAT_AGENT", "class_name": "CodingCatAgent" }
]
},
"migrations": [
{ "tag": "v1", "new_sqlite_classes": ["CodingCatAgent"] }
]
}5. Course-Wide Production Trade-Off Matrix
Every architectural decision involves trade-offs. Use this reference table during design reviews with your team to justify why we choose edge-native TypeScript, explicit schema validation, and deterministic workflows over legacy alternatives:
| Architectural Dimension | 2026 CodingCat.dev Course Pattern | Legacy Industry Default | Production Impact at Scale |
|---|---|---|---|
| Compute & Cold Starts | V8 Isolates / Native Browser Compositor (< 5ms) | Heavy Container VMs / Main-Thread JS Bloat (500ms+) | Eliminates tail-latency spikes on every request and interaction. |
| Contract & Type Safety | End-to-End TypeScript + Zod / GROQ TypeGen | Untyped JSON payloads & manual interface drift | Catches breaking schema changes in CI before deployment. |
| State & Database Topology | Cloudflare D1 Batch / Durable SQLite / Sanity Content Lake | Exhausted TCP pools & unindexed N+1 loops | Predictable sub-10ms reads with zero connection pool errors. |
| Security & Multi-Tenancy | OAuth 2.1 PKCE + Better-Auth Scoped Sessions | Shared static API keys & unescaped DOM strings | Zero-trust tenant isolation and $0 per-MAU SaaS tax. |
| AI & Agent Readiness | Streamable HTTP MCP + WebMCP + RFC 9727 + Daily Spend Caps | 20k-token headless DOM scraping & uncapped retry loops | Cuts token spend by 95%+ while preventing runaway bills. |
6. Official Specifications, Docs & Related CodingCat.dev Deep Dives
Bookmark these official specifications and related CodingCat.dev Courses, Blog Posts, and Podcast Episodes as you work through the lessons:
- Blog Post: The 2026 Persistent AI Agent Landscape: How stateful actor memory, sandbox isolation, and durable scheduling compare across 2026 agent platforms.
- Course: The Zero-Cold-Start Edge Stack: Combine global Cloudflare D1 tables with per-session Durable Object SQLite actors.
- Course: MCP in Production: Connect your stateful Cloudflare Agent to remote OAuth 2.1 Model Context Protocol servers.
- Course: Full-Stack AI Agents in TypeScript: Pair Durable Objects with Zod schemas, hybrid routing, and daily token spend caps.
- Cloudflare Agents SDK Official Documentation: Building stateful AI agents with embedded SQLite, WebSocket hibernation, and scheduled alarms.
- Cloudflare Durable Objects: SQLite Storage Backend (ctx.storage.sql): Zero-latency synchronous SQLite queries co-located inside a single-threaded actor isolate.
- Cloudflare WebSockets Hibernation API Reference: Keeping tens of thousands of WebSockets open at the edge while paying zero idle compute.
- Cloudflare Workflows: Durable Multi-Step Execution Engine: Step-level retries, durable sleep timers, and human-in-the-loop approval gates.
Ready to write code? Jump straight into Lesson 1: Durable Objects Mental Model: Single-Threaded Actors + Embedded SQLite →
how this course connects together.
Follow AJ through the layered stack topology from input contract to production output.
