ANTIGRAVITY LABJP
Articles/Integrations
Integrations/2026-04-30Advanced

Stateful AI Agents on Antigravity × Cloudflare Durable Objects — One Session, One Instance, One Place to Manage Conversation, Tools, and Cost

How to deploy an Antigravity AgentKit 2.0 agent onto Cloudflare Durable Objects so that conversation history, tool-call state, and token spend live inside a single instance per session — with the actual code and production cost numbers from my own deployment.

antigravity429cloudflare6durable-objectsagentkit13production71

Premium Article

The first wall you hit when you deploy an Antigravity agent to Cloudflare Workers is brutal: User A's conversation history starts leaking into User B's request, and even within a single user, every request begins from a blank slate. That's what stateless edge runtimes do.

I spent two solid weeks fighting this. I tried standing up Redis. I tried writing everything to Cloudflare KV. I tried running a SELECT against D1 on every request. Each approach worked, and none of them were something I'd want to put into production. What finally clicked was Cloudflare Durable Objects (DO). One session equals one instance turned out to be the cleanest mental model I'd ever held for stateful agents — and the bill came in at less than a tenth of what I'd budgeted.

This post walks through a design and implementation I now run in production: an Antigravity AgentKit 2.0 agent that lives inside a Durable Object, keeps its conversation history and tool-call state in one place, and bills out to roughly five cents per user per month. The code is excerpted from a working codebase, and you should be able to lift it into your own product the same day.

Why Agents Need to Be Stateful

Stateless workers are fast and cheap, but they fight you when you build agents. Here's the short version of why.

First, conversation history piles up fast. If your front end re-sends the entire history on every request, by turn 20 you're paying 50ms or more in JSON parsing on top of everything else. I measured it.

Second, tool calls suspend and resume. AgentKit 2.0 multi-agent flows happily run for 20 to 40 seconds. Workers have a single-request budget of 30 seconds (10 on the free plan), so when a sub-agent is mid-flight and the request times out, the front end has nothing useful to show. You need a place that holds intermediate state.

Third, cost telemetry must be precise. OpenAI, Gemini, and Claude all bill by token. To know per-user monthly spend, you need a persistent counter keyed on session_id. KV's per-write billing kills you, and D1 hits row-lock contention when many writes target the same key.

Durable Objects solve all three using nothing more than the instance's own memory and storage. No Redis to operate, no KV rate limits to dance around.

What Makes Durable Objects the Right Fit

Three properties matter for agents.

Globally unique instances. When you derive an instance ID with idFromName(sessionId), every request from anywhere on Earth lands in the same instance. The conversation lives in instance memory, so you don't go to a database on the hot path.

Strongly consistent storage. Each DO has a private key-value store with clear transactional boundaries. Use storage.transaction() to atomically update history and the token counter together. KV cannot do this.

Low fixed cost. A DO eats nothing while idle — it gets evicted from memory and re-hydrated on the next call. Roughly $5 a month covers a million requests plus a gigabyte of storage, which is plenty for a few thousand monthly active users on an indie SaaS.

DOs are not great for read-heavy globally distributed caches; KV wins there. Use the right tool per job.

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
If you've been stuck on 'sessions bleed into each other' or 'context resets on every request' after deploying an Antigravity agent to Workers, you'll be able to fix it at the root with a one-session-equals-one-Durable-Object design
You can now lift the conversation-history, tool-state, and token-meter pattern — including the AgentKit 2.0 wiring code — straight into your own SaaS
You'll understand the real production cost (a few cents per user per month) and the design judgments needed when concurrency grows, so you can stop over-engineering with Redis or KV mirrors
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

Antigravity 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

Integrations2026-05-15
Using Claude Opus 4 / Sonnet 4 in Antigravity — Model Selection Strategy and Production Patterns
A practical guide to using Claude Opus 4, Sonnet 4, and Haiku 4.5 in Antigravity. Learn the decision framework and production implementation patterns for balancing cost, speed, and quality in real projects.
Integrations2026-04-22
Three Safeguards Every Antigravity Python API Deployment Needs Before Production
Retries, timeouts, and circuit breakers — the three production safeguards you need around your Antigravity Python API calls, with working code for each.
Integrations2026-04-21
Building Production-Grade Voice Agents with Gemini Live API in Antigravity — Bidirectional Audio, Screen Share, and Latency Patterns
A field guide for shipping production-ready realtime voice agents with Gemini Live API on Antigravity. Covers architecture, latency, resilience, function calling, and cost governance with working TypeScript.
📚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 →