@sveltejs/adapter-cloudflare: streaming chat UI, subagent detail views, thread history, and the Agent Streaming Protocol exposed under /api/threads/.... No separate backend process is required.
Source: js-sveltekit in the deployment cookbook.
Deploy to Cloudflare
svelte.config.js uses adapter-cloudflare(). wrangler.jsonc points Wrangler at .svelte-kit/cloudflare/_worker.js and serves assets from .svelte-kit/cloudflare, matching the SvelteKit Cloudflare adapter docs. The build script appends the ThreadSession Durable Object export to that generated Worker entry because Durable Object classes must be exported by the Worker module.
nodejs_compat and nodejs_compat_populate_process_env are enabled because the LangChain runtime and tracing integrations expect Node-compatible APIs and environment access.
Optionally enable LangSmith tracing by adding the variables from .env.example as Worker secrets or vars.
Required API endpoints
The app exposes the Agent Streaming Protocol under/api/threads/.... SvelteKit route handlers live in src/routes/api/threads/.
Minimum (streaming chat)
| Method | Path | Purpose |
|---|---|---|
POST | /api/threads/:threadId/commands | Accept protocol commands (run.start, …) and start agent runs |
POST | /api/threads/:threadId/stream | SSE stream of protocol events for a run |
GET / POST | /api/threads/:threadId/state | Read and bootstrap checkpointed thread state |
Optional (sidebar)
| Method | Path | Purpose |
|---|---|---|
GET | /api/threads | List threads known to the checkpointer |
DELETE | /api/threads/:threadId | Delete a thread’s session and checkpoints |
POST | /api/threads/:threadId/history | Paginated checkpoint history |
Cloudflare backend design
| Concern | Implementation |
|---|---|
| Frontend | SvelteKit client routes and components |
| API layer | SvelteKit server endpoints in src/routes/api/threads/ |
| Runtime | Workers V8 + nodejs_compat |
| SSE replay | Per-thread Durable Object (ThreadSession) |
| Agent runs | Worker isolate; protocol events POSTed to the DO |
| Static assets | Workers Static Assets via adapter-cloudflare |
| Secrets | wrangler secret / local .env |
Production persistence
Out of the box, the agent uses an in-memoryMemorySaver checkpointer (src/lib/server/agent/index.ts). The per-thread SSE replay/session log lives in a Durable Object so streaming clients reconnect to one coordination point instead of a process-local map.
The checkpointer is still isolate-local demo state. Cloudflare isolates are ephemeral and may scale horizontally, so checkpointed conversation state is not durable across deploys, cold starts, or isolates.
For production:
- Swap in a durable checkpointer (for example Postgres via Hyperdrive, or a custom Durable Object-backed store).
- Persist long-lived replay/history if clients need to reconnect after the Durable Object has been evicted from memory.
Local development
Project layout
Project structure
Project structure
src/lib/server/agent/— deep agent (createDeepAgent) withresearcherandmath-whizsubagents and mock tools.src/lib/server/durable-objects/thread-session.ts— per-thread Durable Object event log for SSE replay.src/lib/server/protocol/— Agent Streaming Protocol helpers: checkpointer-backed state/history, run publishing, serialization, and registry.src/routes/api/threads/— SvelteKit route handlers for the protocol endpoints.src/lib/chat/threads-client.ts— browser thread bootstrap and sidebar helpers.src/lib/components/— Svelte chat UI using@langchain/svelte.svelte.config.js— SvelteKit configured with@sveltejs/adapter-cloudflare.scripts/export-durable-objects.mjs— postbuild patch that re-exports the Durable Object class from the generated Worker entry.wrangler.jsonc— Cloudflare Workers Static Assets and Durable Object config.
See also
- Frameworks and platforms overview
- SvelteKit Cloudflare adapter
- Agent Streaming Protocol
@langchain/svelte
Connect these docs to Claude, VSCode, and more via MCP for real-time answers.

