GEMINI LABJP
NANOLITE — Nano Banana 2 Lite is here: Google's fastest and most cost-efficient Gemini Image model, made for running lightweight image generation cheaplyOMNIFLASH — Gemini Omni Flash is in public preview, a natively multimodal model that lets enterprises and developers build custom, dynamic video workflowsAGENTS — Managed Agents expand with background: true for async server-side runs and polling, remote MCP server integration, and refreshing credentials across interactionsMEMORY — The Memory Bank IngestEvents API is generally available, decoupling event ingestion from memory generation so you can stream content continuouslyTHROUGHPUT — Provisioned Throughput now lets you submit up to seven pending orders for the same model and regionDEPRECATE — Image generation models shut down on August 17, and the Grok 4.1 family on the Gemini Enterprise Agent Platform on August 20NANOLITE — Nano Banana 2 Lite is here: Google's fastest and most cost-efficient Gemini Image model, made for running lightweight image generation cheaplyOMNIFLASH — Gemini Omni Flash is in public preview, a natively multimodal model that lets enterprises and developers build custom, dynamic video workflowsAGENTS — Managed Agents expand with background: true for async server-side runs and polling, remote MCP server integration, and refreshing credentials across interactionsMEMORY — The Memory Bank IngestEvents API is generally available, decoupling event ingestion from memory generation so you can stream content continuouslyTHROUGHPUT — Provisioned Throughput now lets you submit up to seven pending orders for the same model and regionDEPRECATE — Image generation models shut down on August 17, and the Grok 4.1 family on the Gemini Enterprise Agent Platform on August 20
Articles/Dev Tools
Dev Tools/2026-04-27Advanced

Building Stateful AI Agents with Gemini API and Cloudflare Durable Objects — A

A complete production guide to building a stateful AI agent that remembers conversation history, using Cloudflare Durable Objects, WebSocket Hibernation, and the Gemini API streaming endpoint.

gemini-api277cloudflare4durable-objectswebsocket4agent10

Premium Article

"Can the AI remember what I told it yesterday?" is a request that comes up in almost every chat app I have shipped as a solo developer. The Gemini API itself does not retain conversation state between calls, so the server has to keep that history. Over the past year I have tried KV-backed, D1-backed, and Postgres-backed approaches, and the combination of Cloudflare Durable Objects with the Gemini API has consistently come out on top in terms of cost and operational simplicity.

This article walks through the production design I have settled on. The structure is deliberately built around the mistakes I made — the things I had to fix at 2am, the architectures I tore out and rebuilt — so you can short-circuit those steps.

Why Durable Objects: the Workers state problem

Cloudflare Workers are a wonderful edge runtime, but they are stateless. Every request might land on a different worker instance, so any in-memory data disappears between calls. That is fine for stateless API gateways, but a chat agent is not stateless.

You could keep the history in KV or in D1 (Cloudflare's distributed SQLite). The trouble is concurrency. When two requests for the same conversation arrive nearly simultaneously, both read the current history, both append to it, and both write it back. Whichever finishes second silently overwrites the first — the classic lost update. In a KV-backed chat I ran in 2025, this manifested as user complaints like "wait, my last message just disappeared." It took me an embarrassing amount of time to realize the issue was structural, not a UI bug.

Durable Objects solve this by guaranteeing that for any given key — say userId:sessionId — there is exactly one live instance in the world. Every request for that key is routed to the same instance, and the instance can hold both in-memory state and SQLite-backed durable state, in a strictly serialized order. For a conversational agent, that single-writer property is exactly what you want.

The pricing also lands in a sensible place for indie developers. The Workers Paid plan at $5/month includes Durable Objects with generous request and compute allowances. For a hundred-user-scale app, you are looking at a few extra dollars a month rather than the $25+ floor of a managed Postgres tier.

What you will build

Here is the system the rest of the article walks through:

  • A browser chat UI that connects via WebSocket
  • One Durable Object per session, persisting history in built-in SQLite
  • User messages routed to Gemini 2.5 Pro, with replies streamed back over the WebSocket
  • Automatic summarization once the history exceeds a threshold, so the context window does not blow up
  • Hibernation enabled, so idle sessions cost nothing in compute

The whole thing fits in three files: wrangler.toml, src/worker.ts, and src/agent.ts. Seeing the small surface area first helps each subsequent section feel less abstract.

Thank you for reading this far.

Continue Reading

What follows includes implementation code, benchmarks, and practical content we hope you'll find useful. This site runs without ads — server and development costs are supported entirely by members like you. If it's been helpful, we'd be truly grateful for your support.

WHAT YOU'LL LEARN
Get a working stateful AI chat backend that solo developers can ship to production this week, not the kind of toy you abandon after a weekend.
Avoid the WebSocket Hibernation, SQLite transaction, and concurrency pitfalls I hit running a chat agent in production for a year.
Run a 24/7 conversational AI service for a few dollars a month rather than the $50+ a Postgres-backed setup costs at the same scale.
Secure payment via Stripe · Cancel anytime

Unlock This Article

Get full access to the rest of this article. Buy once, read anytime. This site is ad-free — your support goes directly toward keeping it running.

or
Unlock all articles with Membership →
Share

Thank You for Reading

Gemini Lab is ad-free, supported entirely by members like you. We publish practical guides daily with implementation code, benchmarks, and production-ready patterns. If you've found it useful, we'd love to have you on board.

  • Copy-paste ready implementation code
  • New advanced guides published daily
  • $5/mo or $10 for lifetime access
View Membership →

Related Articles

Dev Tools2026-07-11
Never Generate the Same Narration Twice: Cache-Key Design and Invalidation for Gemini TTS Output
For apps that replay the same audio over and over—meditation, language learning, storytelling—caching the Gemini TTS output itself drives the variable cost to near zero. This is a working design: how to build a cache key that text alone can't cover, a two-tier server-plus-device cache, and an invalidation policy that survives model shutdowns like the August 17 image-model deprecation.
Dev Tools2026-06-30
Building an AI-Powered Content Site with Gemini API and Astro
Combine Astro Server Endpoints and Content Collections with the Gemini API to add AI summaries, related-article recommendations, and auto-tagging.
Dev Tools2026-06-28
Gemini API × PWA Complete Implementation Guide — Service Workers, Offline AI, and Web Push Notifications for App Store-Quality Web Apps
Build production-grade AI web apps by combining Gemini API with Progressive Web App techniques — Service Worker caching, IndexedDB for AI context persistence, offline fallbacks, and Web Push notifications — plus instrumentation to surface API cost from your cache hit rate.
📚RECOMMENDED BOOKS
Build a Large Language Model (From Scratch)
Sebastian Raschka
LLM Dev
Prompt Engineering for LLMs
Berryman & Ziegler
Prompting
AI Engineering
Chip Huyen
AI Eng
* Contains affiliate links
See all →