ANTIGRAVITY LABJP
Articles/Agents & Manager
Agents & Manager/2026-06-13Advanced

Building Idempotent Scheduled Agents with the Antigravity SDK

Scheduling an Antigravity SDK agent is almost a one-liner. The hard part is making it idempotent — so a double trigger never runs the job twice, a crash never drops a day, and the result always converges to one. Here is how I build idempotent scheduled agents, learned from the maintenance jobs I run as an indie developer.

agents90antigravity-sdkscheduled-execution3idempotency6automation42

Premium Article

The first week my scheduled agent ran fine. The problem showed up the morning I manually triggered it again during a deploy. Two copies of the same report-aggregation job ran at once, one overwrote the file the other had just written, and that day's numbers ended up being a copy of the day before.

Adding a cron expression to run an agent on a schedule is nearly a single line in the Antigravity SDK. The genuinely hard part is making the result converge to exactly one, whether the agent fired once or twice.

As an indie developer running several apps, I want an agent to handle dependency bumps, crash-report triage, and AdMob report aggregation every night. Idempotency is the foundation that makes that safe.

The minimal scheduled definition

Start with the smallest form of a scheduled agent. The key point is that you separate the agent itself (what it does) from the schedule (when it runs).

# schedule_agent.py
from antigravity import Agent, Schedule, run
 
agent = Agent(
    name="daily-report-aggregator",
    model="gemini-3.5-flash",
    instructions="""
    Aggregate the AdMob report for the given date and
    write it to output/report-<date>.json.
    If the file already exists, exit without overwriting.
    """,
)
 
schedule = Schedule(
    agent=agent,
    # Daily at 09:00 JST (00:00 UTC)
    cron="0 0 * * *",
    timezone="Asia/Tokyo",
    # Cap a single run so a runaway job is physically stopped
    max_duration_seconds=600,
)
 
if __name__ == "__main__":
    run(schedule)

With cron and timezone set, the scheduler starts the agent each time the clock matches. max_duration_seconds looks minor but matters: it is the safety valve that kills a job that has wandered into an unexpected loop.

This much is close to the official examples. The trouble started when I misread how the startup actually works.

Don't misread the "fresh session every run" assumption

Antigravity scheduled execution creates a new session on every trigger. No prior conversation history, no in-memory variables carry over. This is good design for stopping runaway state from propagating — but it also means the agent itself has no idea where the last run left off.

My first version implicitly assumed the session continued. I had written "aggregate from yesterday onward," but a fresh session has no yesterday, so the agent re-aggregated the entire range from scratch every time. The numbers were correct, but the runtime crept up day by day.

The fix is to keep state externally and re-read it on every startup.

import json, pathlib
 
STATE = pathlib.Path("state/last_processed.json")
 
def load_checkpoint() -> str | None:
    if STATE.exists():
        return json.loads(STATE.read_text())["last_date"]
    return None
 
def save_checkpoint(date: str) -> None:
    STATE.parent.mkdir(parents=True, exist_ok=True)
    STATE.write_text(json.dumps({"last_date": date}))

The session is blank each time, but the checkpoint persists on disk (or KV). Tell the agent in its instructions to "read state/last_processed.json and process from the day after," and a fresh session can still resume.

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
Understand the minimal code to define a scheduled Antigravity SDK agent, and how cron triggering, fresh-session startup, and external state fit together
Copy a working idempotency setup — execution lock, output-key dedup, and checkpoint ordering — that guards against double runs, dropped work, and mid-run failures
Add dry-run verification and a pause/resume hook so any scheduled job becomes reviewable in ten minutes
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

Agents & Manager2026-06-13
When a Scheduled Agent Runs Twice — Designing for Idempotency Against Overlap and Retry
A scheduled agent can do the same work twice when the next run triggers before the last one finishes. Here is a design with an overlap lock and an idempotency guard that survives mid-run failures, drawn from a double-publish incident I ran into in production.
Agents & Manager2026-06-13
Instruction Drift in Scheduled Agents — A Three-Layer Design for Keeping Definitions, Docs, and Reality Aligned
Scheduled agents keep logging success even after their instructions diverge from reality. Here is the three-layer drift-detection design — definition, documentation, reality — I built after silent failures in my own operations.
Agents & Manager2026-06-12
Handing Dependency Updates to Antigravity Agents — Risk Tiers, Verification, and Rollback
How far can you trust Antigravity agents with dependency updates? A four-tier risk model that corrects semver optimism, worktree-isolated lots, a fixed verification script, and a rollback-first ledger — the operations design I settled on while maintaining multiple apps.
📚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 →