ANTIGRAVITY LABJP
Articles/Integrations
Integrations/2026-06-27Advanced

Rotate Keys Without Stopping an Unattended Agent: An Overlap-Window Design

API keys and tokens are worth rotating on a schedule before they leak. But an unattended agent goes quietly dead the moment auth breaks during the swap. As an indie developer running several sites on autopilot, I lay out an overlap-window design that rotates keys without downtime.

Antigravity275secret managementauthentication8automation65

Premium Article

I once rotated a key and found, the next morning, that the nightly run had quietly stopped. To prevent a leak I had revoked the old API key, but a gap of a few minutes opened between revocation and distributing the new one — and the job that ran at exactly that time fell over with an auth error. I noticed only because the article that should have been generated was not there.

Rotating keys on a schedule is the right habit. The problem is that, for an agent running unattended, the moment of the swap is its weakest point. With a human nearby you see the 401 and fix it by hand; an agent at night or on the move simply stops, unable to tell anyone its auth broke. Here I want to describe a design that rotates keys without stopping.

Stop treating the swap as an instant

The first idea to drop is the instant swap: delete the old key, drop in the new one. No matter how fast you do it, a time gap always opens between revocation and distribution. If a job lands in that gap, it fails.

What you need instead is an overlap window in which both the old and new keys are valid for a while. Enable the new key first, make the readers accept either one, then move the writers to the new key, and only at the end retire the old one. The idea is to replace the instant swap with a gentle migration.

Rotate in four stages

I split key rotation into four stages.

  1. Issue: create the new key, but let nobody use it yet
  2. Accept: bring every reader to a state where it authenticates with either the old or new key
  3. Cut over: move the key that writers (the agent itself) use to the new one
  4. Retire: revoke the old key

The crux is always separating stages 2 and 3. Switch the writers before the readers accept both, and you reproduce exactly the gap from the opening. Do acceptance first, and the cutover can happen at any time without opening a hole.

# secrets.yaml — make the overlap window explicit
api_key:
  primary: "${API_KEY_NEW}"      # used after cutover
  secondary: "${API_KEY_OLD}"    # accepted only during the overlap
  overlap_until: "2026-06-29T15:00+09:00"  # retire at this time

Carrying overlap_until in config lets you decide retirement by a timestamp rather than a hunch. I keep this window at 48 hours: long enough to span a weekend and let a longer job run to completion, yet short enough not to keep the old key alive too long.

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
A procedure that keeps both the old and new keys valid at once so the switch never opens an auth gap
The three pitfalls that turn rotation into an incident under unattended operation (cache, in-flight jobs, re-auth tokens) and how to avoid each
A concrete example from automating my own 90-day rotation: a 48-hour overlap window and the cutover steps
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-06-17
When the Antigravity CLI Stalls on a 401 During Unattended Runs
If your scheduled Antigravity CLI job suddenly stops producing output after a single 401 in the logs, here is how to separate an expired token from a silent re-login prompt and rebuild your unattended setup.
Integrations2026-03-10
GitHub Actions × Antigravity CI/CD Automation Guide — Build and Manage Pipelines with AI
Learn how to build CI/CD pipelines with Antigravity and GitHub Actions. From workflow generation to automated deployment.
Integrations2026-06-25
A Translated Line Had Quietly Reverted to English — Guarding String Resources an Agent's Refactor Touched
Let an agent tidy your values folder and translated strings can silently revert to the source text. Here is a design and implementation that treats the default locale as the source of truth, reads every other locale as a diff, and blocks only dropped keys, reverted translations, and broken format arguments at pre-commit.
📚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 →