◉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.

agents150antigravity-sdkscheduled-execution3idempotency11automation96

✦ 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-05-27
Running 50+ AdMob Mediation Groups Alone: Four Tasks I Handed to an Agent, Four Decisions I Kept
Two wallpaper apps on iOS and Android put me past 50 AdMob mediation groups. Here are the four tasks I handed to an agent, the four decisions I kept, and the sample-size hole I later found in the evaluation code I had published.
◈ Agents & Manager2026-04-20
Building a Business Automation Agent with Antigravity × Google Workspace API — Gmail, Drive, Sheets and Docs in Production
Learn how to build AI agents that automate Gmail responses, generate documents from Drive templates, and create intelligent Sheets reports using Antigravity, Google Workspace APIs, and Gemini.
◈ Agents & Manager2026-10-30
I moved the stop conditions for a nightly Antigravity CLI run outside the CLI
A wrapper script that gives a nightly agy -p run three outer stop conditions: time, output size and overlap. Testing four ways of ending with stub scripts showed that a timeout can exit with 137 instead of 124.