Back to AI App Dev Series

Gemini SDK Track Part 10: Autonomous Agents & Antigravity SDK

May 24, 2026 Wasil Zafar 45 min read

Build autonomous agents with the Antigravity SDK — managed remote Linux sandboxes, custom agents with inline environments and skills, the hook interception engine, system instructions, and multimodal agent inputs.

Table of Contents

  1. The Antigravity Agent
  2. Basic Antigravity Usage
  3. Building Custom Agents
  4. The Hook Interception Engine
  5. Managed Agents Runtime
  6. Multimodal Agent Inputs
What You’ll Learn: Building responsible AI means configuring safety guardrails that protect users without over-blocking legitimate use. Gemini provides configurable safety filters across categories (harassment, hate speech, dangerous content, sexually explicit), plus custom classifiers for your specific needs. This article teaches you to tune safety settings for your use case — strict for children’s apps, balanced for professional tools.

1. The Antigravity Agent

The Antigravity SDK is Google’s managed agent framework built on top of the Gemini API. It provides a general-purpose autonomous agent that can write code, execute it in a managed Linux sandbox, iterate on results, and deliver completed outputs — all from a single API call.

Key Concept: Unlike standard generateContent calls where the model generates text and stops, an Antigravity agent runs an autonomous tool-use loop. It plans, writes code, executes it, observes results, and iterates — continuing until the task is complete or a timeout is reached.

1.1 Architecture Overview

ComponentPurposeDetails
Agent ModelPlanning & reasoningantigravity-preview-05-2026
Linux SandboxCode execution environmentEphemeral, isolated, pre-installed packages
Tool LoopAutonomous iterationPlan → Code → Execute → Observe → Repeat
HooksSafety & customizationIntercept, inspect, block, or transform actions

2. Basic Antigravity Usage

2.1 Python Examples

The simplest Antigravity call — give the agent a task and let it autonomously execute:

from google import genai

client = genai.Client()

# Basic agent call — the agent will write and execute code autonomously
interaction = client.interactions.create(
    agent="antigravity-preview-05-2026",
    input="Scrape the top 5 stories from Hacker News and summarize each in one sentence.",
    environment="remote"
)

print("Agent Output:")
print(interaction.output_text)
from google import genai

client = genai.Client()

# Agent with a data analysis task
interaction = client.interactions.create(
    agent="antigravity-preview-05-2026",
    input="""Create a Python script that:
1. Generates sample sales data for 12 months (random but realistic)
2. Calculates month-over-month growth rates
3. Identifies the best and worst performing months
4. Creates a matplotlib bar chart of monthly sales
5. Save the chart as 'sales_chart.png'""",
    environment="remote"
)

print("Agent Output:")
print(interaction.output_text)

# The agent executed code in a sandbox and produced results
# Access any generated files from the interaction
for step in interaction.steps:
    if step.type == "model_output":
        for part in step.parts:
            if part.inline_data:
                print(f"Generated file: {part.inline_data.mime_type}")

2.2 JavaScript Example

import { GoogleGenAI } from "@google/genai";

const client = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });

// Same pattern in JavaScript
const interaction = await client.interactions.create({
    agent: "antigravity-preview-05-2026",
    input: "Generate a PDF report with a table of the top 10 programming languages by popularity in 2026.",
    environment: "remote"
});

console.log("Agent Output:", interaction.outputText);
Sandbox Note: Each Antigravity call provisions a fresh Linux sandbox. The environment is ephemeral — files created during execution are available in the response but not persisted between calls. For persistent state, use the Interactions API previous_interaction_id to chain agent calls.
Real-World Application

Child-Safe Educational AI

An EdTech company building for K-12 students configured maximum safety thresholds plus custom classifiers that flag age-inappropriate complexity, scary content, and discussions of adult topics. They added a “teacher review” queue for borderline content. Result: zero safety incidents across 1M student interactions.

SafetyEdTechContent Moderation

3. Building Custom Agents

3.1 Inline Configuration

Customize the agent’s behavior by providing system instructions and environment sources inline — no separate deployment needed:

from google import genai

client = genai.Client()

# Custom agent with system instruction and environment files
interaction = client.interactions.create(
    agent="antigravity-preview-05-2026",
    input="Analyze the dataset and create a comprehensive report with visualizations.",
    system_instruction="""You are a senior data analyst. Follow these rules:
- Always use pandas for data manipulation
- Create publication-quality matplotlib charts with proper labels
- Use seaborn for statistical visualizations
- Include summary statistics in every report
- Format numbers with appropriate precision""",
    environment={
        "type": "remote",
        "sources": [
            {
                "type": "inline",
                "target": "data/sales.csv",
                "content": "month,revenue,units\nJan,45000,120\nFeb,52000,145\nMar,48000,130\nApr,61000,170\nMay,58000,160\nJun,72000,195"
            }
        ]
    }
)

print("Analysis Report:")
print(interaction.output_text)

3.2 Custom Skills & Instructions via Inline Files

from google import genai

client = genai.Client()

# Pass custom AGENTS.md and SKILL.md files inline
agents_md = """# Data Analyst Agent

## Role
You are a specialized data analyst agent focused on financial metrics.

## Skills
- Statistical analysis (mean, median, std, correlation)
- Time series decomposition
- Anomaly detection using IQR method
- Visualization with matplotlib/seaborn

## Output Format
Always produce:
1. Executive summary (3-5 bullet points)
2. Detailed findings with charts
3. Recommendations section
"""

skill_md = """# Chart Generation Skill

## Instructions
- Use figsize=(10, 6) for all charts
- Use the color palette: ['#3B9797', '#132440', '#BF092F', '#16476A']
- Add gridlines with alpha=0.3
- Save all charts as PNG at 150 DPI
- Include descriptive titles and axis labels
"""

interaction = client.interactions.create(
    agent="antigravity-preview-05-2026",
    input="Analyze the Q1 revenue trends and create a forecast for Q2.",
    environment={
        "type": "remote",
        "sources": [
            {
                "type": "inline",
                "target": ".agents/AGENTS.md",
                "content": agents_md
            },
            {
                "type": "inline",
                "target": ".agents/skills/charts/SKILL.md",
                "content": skill_md
            },
            {
                "type": "inline",
                "target": "data/revenue.csv",
                "content": "week,revenue\n1,12000\n2,13500\n3,11800\n4,14200\n5,15100\n6,14800\n7,16200\n8,15900\n9,17100\n10,16500\n11,18200\n12,17800"
            }
        ]
    }
)

print(interaction.output_text)

4. The Hook Interception Engine

4.1 Hook Patterns

Hooks let you intercept the agent’s actions before they execute. This enables safety boundaries, audit logging, content filtering, and custom policies without modifying the agent itself:

from google import genai
from google.genai import types

client = genai.Client()

# Define a pre-tool-call hook that audits all actions
def audit_hook(tool_call):
    """Log every tool call the agent makes."""
    print(f"[AUDIT] Agent calling: {tool_call.name}")
    print(f"[AUDIT] Arguments: {tool_call.arguments}")
    # Return None to allow the call to proceed
    return None

# Define a policy hook that blocks dangerous operations
def safety_policy_hook(tool_call):
    """Block destructive commands."""
    blocked_commands = ["rm -rf", "sudo", "chmod 777", "curl | bash"]

    if tool_call.name == "code_execution":
        code = tool_call.arguments.get("code", "")
        for cmd in blocked_commands:
            if cmd in code:
                print(f"[BLOCKED] Dangerous command detected: {cmd}")
                return types.HookResponse(
                    action="block",
                    message=f"Policy violation: '{cmd}' is not allowed."
                )
    return None  # Allow all other calls

# Use hooks in an agent call
interaction = client.interactions.create(
    agent="antigravity-preview-05-2026",
    input="List the files in the current directory and show disk usage.",
    environment="remote",
    hooks={
        "on_pre_tool_call": [audit_hook, safety_policy_hook]
    }
)

print("\nAgent Output:")
print(interaction.output_text)

4.2 Safety Boundaries

from google import genai
from google.genai import types

client = genai.Client()

# Comprehensive safety hook with multiple policy layers
def network_policy_hook(tool_call):
    """Control network access from the sandbox."""
    if tool_call.name == "code_execution":
        code = tool_call.arguments.get("code", "")

        # Block outbound network requests to unauthorized domains
        allowed_domains = ["api.github.com", "pypi.org"]
        if "requests.get" in code or "urllib" in code or "httpx" in code:
            # Check if the URL targets an allowed domain
            is_allowed = any(domain in code for domain in allowed_domains)
            if not is_allowed:
                return types.HookResponse(
                    action="block",
                    message="Network requests are restricted to approved domains only."
                )
    return None

def resource_limit_hook(tool_call):
    """Prevent resource-intensive operations."""
    if tool_call.name == "code_execution":
        code = tool_call.arguments.get("code", "")

        # Block potential memory bombs
        if "* 10**9" in code or "* 1000000000" in code:
            return types.HookResponse(
                action="block",
                message="Operation would exceed memory limits."
            )
    return None

# Apply multiple safety hooks
interaction = client.interactions.create(
    agent="antigravity-preview-05-2026",
    input="Download the latest Python release notes and summarize the key changes.",
    environment="remote",
    hooks={
        "on_pre_tool_call": [network_policy_hook, resource_limit_hook]
    }
)

print(interaction.output_text)
Hook Execution Order: Hooks execute in array order. The first hook that returns a non-None HookResponse with action="block" stops execution immediately. Use this to layer policies: audit hooks first (always return None), then safety hooks, then business logic hooks.

5. Managed Agents Runtime

5.1 Sandbox Details

Each Antigravity agent call provisions an isolated Linux environment with pre-installed packages:

FeatureDetails
OSDebian-based Linux (latest stable)
Python3.12+ with pip
Pre-installednumpy, pandas, matplotlib, seaborn, scikit-learn, requests, beautifulsoup4
Package InstallAgent can pip install additional packages at runtime
File SystemRead/write access within sandbox; ephemeral (lost after call)
NetworkOutbound HTTP/HTTPS enabled (controllable via hooks)
IsolationEach call gets a fresh container; no cross-call contamination

5.2 Resource Limits & Timeout

from google import genai

client = genai.Client()

# Configure timeout for long-running agent tasks
interaction = client.interactions.create(
    agent="antigravity-preview-05-2026",
    input="""Train a simple random forest classifier on the Iris dataset:
1. Load iris from sklearn.datasets
2. Split 80/20 train/test
3. Train RandomForestClassifier with 100 estimators
4. Print classification report
5. Create a confusion matrix heatmap""",
    environment="remote",
    config={
        "timeout_seconds": 300  # 5-minute timeout for compute-heavy tasks
    }
)

print("Agent Output:")
print(interaction.output_text)
ResourceLimitNotes
Execution TimeoutUp to 5 minutes (configurable)Default: 60 seconds
Memory~2 GB RAMSufficient for most data tasks
Disk~10 GB ephemeralFor temporary files and packages
Tool Loop IterationsUp to 25 iterationsAgent self-terminates if stuck

6. Multimodal Agent Inputs

6.1 Image Analysis

Pass images directly to the agent for visual analysis, chart interpretation, or image processing tasks:

from google import genai
from google.genai import types
import base64

client = genai.Client()

# Read an image file
with open("dashboard_screenshot.png", "rb") as f:
    image_data = f.read()

# Pass image to the agent for analysis
interaction = client.interactions.create(
    agent="antigravity-preview-05-2026",
    input=[
        types.Part(text="Analyze this dashboard screenshot. Extract all visible metrics, identify trends, and create a summary table in markdown format."),
        types.Part(inline_data=types.Blob(
            mime_type="image/png",
            data=base64.b64encode(image_data).decode()
        ))
    ],
    environment="remote"
)

print("Dashboard Analysis:")
print(interaction.output_text)

6.2 Document Processing

from google import genai
from google.genai import types
import base64

client = genai.Client()

# Pass a PDF document for the agent to process
with open("quarterly_report.pdf", "rb") as f:
    pdf_data = f.read()

interaction = client.interactions.create(
    agent="antigravity-preview-05-2026",
    input=[
        types.Part(text="""Process this quarterly report:
1. Extract all financial figures into a structured table
2. Calculate year-over-year growth for each metric
3. Identify the top 3 risks mentioned
4. Generate an executive summary (max 200 words)
5. Create a bar chart comparing this quarter vs last quarter"""),
        types.Part(inline_data=types.Blob(
            mime_type="application/pdf",
            data=base64.b64encode(pdf_data).decode()
        ))
    ],
    environment="remote"
)

print("Report Analysis:")
print(interaction.output_text)

# Check for generated visualizations
for step in interaction.steps:
    if step.type == "model_output":
        for part in step.parts:
            if part.inline_data and part.inline_data.mime_type.startswith("image/"):
                print(f"\nGenerated chart: {part.inline_data.mime_type}")
                # Save the chart
                with open("quarterly_chart.png", "wb") as f:
                    f.write(base64.b64decode(part.inline_data.data))
                print("Chart saved to quarterly_chart.png")
from google import genai
from google.genai import types

client = genai.Client()

# Multi-turn agent conversation with persistent context
turn1 = client.interactions.create(
    agent="antigravity-preview-05-2026",
    input="Create a Python class called DataPipeline that can load CSV files, clean data (remove nulls, fix types), and export to Parquet format.",
    environment="remote"
)
print(f"Turn 1: {turn1.output_text[:300]}...")

# Continue — agent remembers the code it wrote
turn2 = client.interactions.create(
    agent="antigravity-preview-05-2026",
    previous_interaction_id=turn1.id,
    input="Now add a method called 'profile' that generates summary statistics and data quality metrics for each column.",
    environment="remote"
)
print(f"\nTurn 2: {turn2.output_text[:300]}...")

# Continue — add tests
turn3 = client.interactions.create(
    agent="antigravity-preview-05-2026",
    previous_interaction_id=turn2.id,
    input="Write pytest unit tests for the DataPipeline class. Test all methods with sample data.",
    environment="remote"
)
print(f"\nTurn 3: {turn3.output_text[:300]}...")
Production Pattern: For production agents, combine hooks (safety), custom system instructions (domain expertise), inline environment files (context), and multi-turn chaining (complex workflows). This gives you a controlled, auditable, and capable autonomous agent without managing any infrastructure.
Try It Yourself: Create a content moderation pipeline: (1) Configure safety settings at 3 levels (strict, balanced, permissive), (2) test with 10 prompts ranging from clearly safe to edge cases, (3) document which prompts are blocked at each level, (4) implement a custom classifier that detects domain-specific harmful content (e.g., financial advice without disclaimers).

Next in the Gemini SDK Track

In Part 11: Deep Research & Computer Use, we’ll explore Gemini’s Deep Research agent for autonomous multi-step research tasks and the Computer Use capability for browser automation and GUI interaction.