Tác nhân Antigravity

Tác nhân Antigravity là một tác nhân được quản lý đa năng trên Gemini API. Một lệnh gọi API duy nhất sẽ cung cấp cho bạn một tác nhân có khả năng suy luận, thực thi mã, quản lý tệp và duyệt web trong hộp cát Linux bảo mật của riêng bạn, do Google lưu trữ.

Công cụ này dựa trên Gemini 3.6 Flash và sử dụng cùng một cơ chế như Antigravity IDE. Bạn có thể định cấu hình mô hình Gemini cơ bản bằng cách sử dụng agent_config. Có trong Interactions APIGoogle AI Studio.

Python

from google import genai

client = genai.Client()

interaction = client.interactions.create(
    agent="antigravity-preview-05-2026",
    input="Read Hacker News, summarize the top 10 stories, and save the results as a PDF.",
    environment="remote",
)

print(interaction.output_text)

JavaScript

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

const client = new GoogleGenAI({});

const interaction = await client.interactions.create({
    agent: "antigravity-preview-05-2026",
    input: "Read Hacker News, summarize the top 10 stories, and save the results as a PDF.",
    environment: "remote",
}, { timeout: 300000 });

console.log(interaction.output_text);

REST

curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
-H "Content-Type: application/json" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-d '{
    "agent": "antigravity-preview-05-2026",
    "input": "Read Hacker News, summarize the top 10 stories, and save the results as a PDF.",
    "environment": "remote"
}'

Tính năng

Mỗi lệnh gọi có thể cung cấp một hộp cát Linux và bắt đầu một vòng lặp sử dụng công cụ. Tác nhân lập kế hoạch, hành động, quan sát kết quả và lặp lại cho đến khi hoàn thành nhiệm vụ.

  • Thực thi mã: Chạy các lệnh Bash, Python và Node.js. Cài đặt gói, chạy kiểm thử, tạo ứng dụng.
  • Quản lý tệp: Đọc, ghi, chỉnh sửa, tìm kiếm và liệt kê các tệp trong hộp cát. Các tệp vẫn tồn tại trong các lượt tương tác.
  • Quyền truy cập vào web: Google Tìm kiếm và tìm nạp URL để lấy dữ liệu.
  • Nén bối cảnh: Tự động nén bối cảnh (kích hoạt ở khoảng 135.000 mã thông báo) để hỗ trợ các phiên kéo dài nhiều lượt mà không làm mất bối cảnh hoặc vượt quá giới hạn mã thông báo.

Hãy xem phần Bắt đầu nhanh để biết cách sử dụng nhiều lượt và phát trực tuyến.

Các công cụ được hỗ trợ

Theo mặc định, tác nhân có quyền truy cập vào code_execution, google_searchurl_context. Các công cụ hệ thống tệp sẽ tự động được bật khi bạn chỉ định tham số environment. Bạn cũng có thể xác định các hàm tuỳ chỉnh để kết nối tác nhân với các API và công cụ của riêng mình. Bạn chỉ cần chỉ định tham số tools khi tuỳ chỉnh hoặc hạn chế bộ mặc định, hoặc khi thêm các hàm tuỳ chỉnh.

Công cụ Giá trị loại Mô tả
Thực thi mã code_execution Chạy các lệnh shell (bash, Python, Node) với tính năng ghi lại stdout/stderr.
Google Tìm kiếm google_search Tìm kiếm trên web công khai.
Bối cảnh từ URL url_context Tìm nạp và đọc các trang web.
Hệ thống tệp (được bật thông qua environment) Đọc, ghi, chỉnh sửa, tìm kiếm và liệt kê các tệp trong hộp cát. Không có loại công cụ riêng biệt; tự động bật khi bạn đặt environment.
Hàm tuỳ chỉnh function Xác định các hàm tuỳ chỉnh mà tác nhân có thể yêu cầu thực thi. Xem phần Gọi hàm.
Máy chủ MCP từ xa mcp_server Đăng ký các máy chủ Giao thức ngữ cảnh mô hình (MCP) bên ngoài dưới dạng công cụ. Xem phần Máy chủ MCP.

Để giới hạn tác nhân ở các công cụ cụ thể, hãy chỉ truyền những công cụ bạn cần:

Python

from google import genai

client = genai.Client()

interaction = client.interactions.create(
    agent="antigravity-preview-05-2026",
    input="Search for the latest AI research papers on reasoning and summarize them.",
    environment="remote",
    tools=[
        {"type": "google_search"},
        {"type": "url_context"},
    ],
)

print(interaction.output_text)

JavaScript

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

const client = new GoogleGenAI({});

const interaction = await client.interactions.create({
    agent: "antigravity-preview-05-2026",
    input: "Search for the latest AI research papers on reasoning and summarize them.",
    environment: "remote",
    tools: [
        { type: "google_search" },
        { type: "url_context" },
    ],
}, { timeout: 300000 });

console.log(interaction.output_text);

REST

curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
-H "Content-Type: application/json" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-d '{
    "agent": "antigravity-preview-05-2026",
    "input": "Search for the latest AI research papers on reasoning and summarize them.",
    "environment": "remote",
    "tools": [
        {"type": "google_search"},
        {"type": "url_context"}
    ]
}'

Đầu vào đa phương thức

Tác nhân Antigravity hỗ trợ thông tin đầu vào đa phương thức. Hiện tại, chỉ có đầu vào textimage được hỗ trợ. Bạn phải cung cấp hình ảnh dưới dạng chuỗi được mã hoá base64 cùng dòng (data).

Python

import base64
from google import genai

client = genai.Client()

with open("path/to/chart.png", "rb") as f:
    image_bytes = f.read()

interaction_inline = client.interactions.create(
    agent="antigravity-preview-05-2026",
    input=[
        {"type": "text", "text": "Analyze this chart and summarize the trends."},
        {
            "type": "image",
            "data": base64.b64encode(image_bytes).decode("utf-8"),
            "mime_type": "image/png",
        },
    ],
    environment="remote",
)

JavaScript


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

import * as fs from "node:fs";

const client = new GoogleGenAI({});
const base64Image = fs.readFileSync("path/to/chart.png", { encoding: "base64" });

const interactionInline = await client.interactions.create({
    agent: "antigravity-preview-05-2026",
    input: [
        { type: "text", text: "Analyze this chart and summarize the trends." },
        {
            type: "image",
            data: base64Image,
            mime_type: "image/png",
        },
    ],
    environment: "remote",
}, { timeout: 300000 });

REST

BASE64_IMAGE=$(base64 -w0 /path/to/chart.png)

curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
-H "Content-Type: application/json" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-d "{
    \"agent\": \"antigravity-preview-05-2026\",
    \"input\": [
        {\"type\": \"text\", \"text\": \"Analyze this chart and summarize the trends.\"},
        {
            \"type\": \"image\",
            \"mime_type\": \"image/png\",
            \"data\": \"$BASE64_IMAGE\"
        }
    ],
    \"environment\": \"remote\"
}"

Gọi hàm

Tính năng gọi hàm cho phép bạn kết nối tác nhân Antigravity với các API và cơ sở dữ liệu bên ngoài bằng cách xác định các công cụ tuỳ chỉnh mà tác nhân có thể gọi. Để biết các khái niệm chung, hãy xem phần Gọi hàm bằng Gemini API.

Ví dụ sau đây minh hoạ một lượt tương tác gồm 2 lượt. Trước tiên, tác nhân yêu cầu một lệnh gọi hàm get_weather tuỳ chỉnh, sau đó ứng dụng sẽ thực thi lệnh gọi hàm đó và trả về kết quả trong lượt thứ hai.

Python

from google import genai

client = genai.Client()

# 1. Define the custom function
get_weather_tool = {
    "type": "function",
    "name": "get_weather",
    "description": "Gets the current weather for a given location.",
    "parameters": {
        "type": "object",
        "properties": {
            "location": {
                "type": "string",
                "description": "The city and country, e.g. San Francisco, USA",
            }
        },
        "required": ["location"],
    },
}

# 2. Call the agent with the custom tool (Turn 1)
interaction = client.interactions.create(
    agent="antigravity-preview-05-2026",
    input="What is the weather in Tokyo?",
    environment="remote",
    tools=[
        {"type": "code_execution"},  # Enable default code execution
        get_weather_tool,            # Add custom function
    ],
)

# Check if the agent requested a function call
if interaction.status == "requires_action":
    # Find function calls that do not have a matching function result.
    # Filesystem tools (like write_file) are also represented as function calls
    # but are executed automatically by the environment.
    executed_calls = {step.call_id for step in interaction.steps if step.type == "function_result"}
    pending_calls = [step for step in interaction.steps if step.type == "function_call" and step.id not in executed_calls]

    if pending_calls:
        fc_step = pending_calls[0]
        print(f"Function to call: {fc_step.name} (ID: {fc_step.id})")
        print(f"Arguments: {fc_step.arguments}")

        # 3. Execute the function locally (simulated get_weather()) and send the result back (Turn 2)
        function_result = {
            "temperature": 23,
            "unit": "celsius"
        }

        final_interaction = client.interactions.create(
            agent="antigravity-preview-05-2026",
            previous_interaction_id=interaction.id,  # Reference the interaction ID
            environment=interaction.environment_id,
            input=[
                {
                    "type": "function_result",
                    "name": fc_step.name,
                    "call_id": fc_step.id,
                    "result": function_result,
                }
            ],
        )

        print(final_interaction.output_text)
        # Output: The current weather in Tokyo, Japan is 23°C (Celsius).
    else:
        print("No pending function calls.")
else:
    print(f"Interaction completed with status: {interaction.status}")

JavaScript

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

const client = new GoogleGenAI({});

// 1. Define the custom function
const get_weather_tool = {
  type: "function",
  name: "get_weather",
  description: "Gets the current weather for a given location.",
  parameters: {
    type: "object",
    properties: {
      location: {
        type: "string",
        description: "The city and country, e.g. San Francisco, USA",
      },
    },
    required: ["location"],
  },
};

// 2. Call the agent with the custom tool (Turn 1)
const interaction = await client.interactions.create({
  agent: "antigravity-preview-05-2026",
  input: "What is the weather in Tokyo?",
  environment: "remote",
  tools: [
    { type: "code_execution" },
    get_weather_tool,
  ],
}, { timeout: 300000 });

if (interaction.status === "requires_action") {
  // Find function calls that do not have a matching function result.
  // Filesystem tools (like write_file) are also represented as function calls
  // but are executed automatically by the environment.
  const executedCalls = new Set(
    interaction.steps
      .filter(s => s.type === "function_result")
      .map(s => s.call_id)
  );
  const pendingCalls = interaction.steps.filter(
    s => s.type === "function_call" && !executedCalls.has(s.id)
  );

  if (pendingCalls.length > 0) {
    const fcStep = pendingCalls[0];
    console.log(`Function to call: ${fcStep.name} (ID: ${fcStep.id})`);

    // 3. Execute the function locally (simulated get_weather()) and send the result back (Turn 2)
    const functionResult = {
      temperature: 23,
      unit: "celsius"
    };

    const finalInteraction = await client.interactions.create({
      agent: "antigravity-preview-05-2026",
      previous_interaction_id: interaction.id, // Reference the interaction ID
      environment: interaction.environment_id,
      input: [
        {
          type: "function_result",
          name: fcStep.name,
          call_id: fcStep.id,
          result: functionResult,
        }
      ],
    }, { timeout: 300000 });

    console.log(finalInteraction.output_text);
  } else {
    console.log("No pending function calls.");
  }
} else {
  console.log(`Interaction completed with status: ${interaction.status}`);
}

REST

# 1. Turn 1: Request function call
RESPONSE=$(curl -s -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
  -H "Content-Type: application/json" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -d '{
      "agent": "antigravity-preview-05-2026",
      "input": "What is the weather in Tokyo?",
      "environment": "remote",
      "tools": [
          {"type": "code_execution"},
          {
              "type": "function",
              "name": "get_weather",
              "description": "Gets the current weather for a given location.",
              "parameters": {
                  "type": "object",
                  "properties": {
                      "location": {"type": "string"}
                  },
                  "required": ["location"]
              }
          }
      ]
  }')

# Extract interaction ID, environment ID, and call ID (requires jq)
INTERACTION_ID=$(echo $RESPONSE | jq -r '.id')
ENVIRONMENT_ID=$(echo $RESPONSE | jq -r '.environment_id')
CALL_ID=$(echo $RESPONSE | jq -r '.steps[] | select(.type=="function_call") | .id')

# 2. Turn 2: Send function result back using variables
curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
  -H "Content-Type: application/json" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -d "{
      \"agent\": \"antigravity-preview-05-2026\",
      \"previous_interaction_id\": \"$INTERACTION_ID\",
      \"environment\": \"$ENVIRONMENT_ID\",
      \"input\": [
          {
              \"type\": \"function_result\",
              \"name\": \"get_weather\",
              \"call_id\": \"$CALL_ID\",
              \"result\": {
                  \"temperature\": 23,
                  \"unit\": \"celsius\"
              }
          }
      ]
  }"

Máy chủ MCP

Bạn có thể kết nối tác nhân Antigravity với các công cụ bên ngoài bằng cách đăng ký các máy chủ Giao thức ngữ cảnh mô hình (MCP) từ xa. Tác nhân hỗ trợ các máy chủ MCP từ xa qua HTTP có thể truyền phát.

Khi đăng ký máy chủ MCP, bạn phải chỉ định các trường sau trong mảng tools:

Trường Loại Bắt buộc Mô tả
type chuỗi Phải là "mcp_server".
name chuỗi Giá trị nhận dạng riêng biệt của máy chủ. Phải là chữ thường và bao gồm cả chữ và số (phù hợp với ^[a-z0-9_-]+$).
url chuỗi URL điểm cuối của máy chủ MCP từ xa.
headers đối tượng Không Tiêu đề tuỳ chỉnh (ví dụ: xác thực) được gửi cùng với các yêu cầu.
allowed_tools mảng Không Danh sách tên công cụ được phép thực thi. Nếu bạn bỏ qua, tất cả các công cụ đều được phép.

Python

from google import genai

client = genai.Client()

# Register a remote HTTP MCP server
interaction = client.interactions.create(
    agent="antigravity-preview-05-2026",
    input="What is the weather in Tokyo?",
    environment="remote",
    tools=[{
        "type": "mcp_server",
        "name": "weather", # Must be lowercase
        "url": "https://gemini-api-demos.uc.r.appspot.com/mcp"
    }]
)

print(interaction.output_text)

JavaScript

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

const client = new GoogleGenAI({});

const interaction = await client.interactions.create({
    agent: "antigravity-preview-05-2026",
    input: "What is the weather in Tokyo?",
    environment: "remote",
    tools: [{
        type: "mcp_server",
        name: "weather", // Must be lowercase
        url: "https://gemini-api-demos.uc.r.appspot.com/mcp"
    }]
}, { timeout: 300000 });

console.log(interaction.output_text);

REST

curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
  -H "Content-Type: application/json" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -d '{
      "agent": "antigravity-preview-05-2026",
      "input": "What is the weather in Tokyo?",
      "environment": "remote",
      "tools": [{
          "type": "mcp_server",
          "name": "weather",
          "url": "https://gemini-api-demos.uc.r.appspot.com/mcp"
      }]
  }'

Lựa chọn mô hình

Đối với antigravity-preview-05-2026, mô hình mặc định là Gemini 3.6 Flash (gemini-3.6-flash). Nếu bạn bỏ qua agent_config, tác nhân sẽ mặc định là gemini-3.6-flash.

Bạn có thể thiết lập mô hình Gemini cơ bản bằng cách sử dụng agent_config để tối ưu hoá tốc độ, chi phí hoặc khả năng suy luận.

Python

from google import genai

client = genai.Client()

interaction = client.interactions.create(
    agent="antigravity-preview-05-2026",
    input="Summarize the key differences between functional and object-oriented programming.",
    environment="remote",
    agent_config={
        "type": "antigravity",
        "model": "gemini-3.5-flash-lite",
    },
)

print(interaction.output_text)

JavaScript

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

const client = new GoogleGenAI({});

const interaction = await client.interactions.create({
    agent: "antigravity-preview-05-2026",
    input: "Summarize the key differences between functional and object-oriented programming.",
    environment: "remote",
    agent_config: {
        type: "antigravity",
        model: "gemini-3.5-flash-lite",
    },
}, { timeout: 300000 });

console.log(interaction.output_text);

REST

curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
  -H "Content-Type: application/json" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -d '{
      "agent": "antigravity-preview-05-2026",
      "input": "Summarize the key differences between functional and object-oriented programming.",
      "environment": "remote",
      "agent_config": {
          "type": "antigravity",
          "model": "gemini-3.5-flash-lite"
      }
  }'

Sau đây là các giá trị được hỗ trợ cho agent_config.model:

Mô hình Giá trị trong agent_config.model Mô tả
Gemini 3.6 Flash (mặc định) gemini-3.6-flash Mô hình cân bằng mặc định để suy luận, lập trình và sử dụng công cụ.
Gemini 3.5 Flash gemini-3.5-flash Mô hình Flash thế hệ trước cho các quy trình công việc chung của tác nhân.
Gemini 3.5 Flash-Lite gemini-3.5-flash-lite Mô hình gọn nhẹ được tối ưu hoá cho các tác vụ có độ trễ thấp và nhạy cảm về chi phí.

Khi tạo một tác nhân được quản lý bằng agents.create, bạn sẽ định cấu hình mô hình theo cách tương tự bằng cách truyền base_agentagent_config. Xin lưu ý rằng bạn không thể ghi đè mô hình tại thời điểm tương tác cho một tác nhân được quản lý được tạo bằng agents.create. Mô hình này bị khoá theo chế độ cài đặt khi tác nhân được tạo. Điều này đảm bảo hành vi gọi công cụ có thể dự đoán được, gỡ lỗi nhất quán và tuân thủ các ranh giới bảo mật.

Tuỳ chỉnh tác nhân

Bạn có thể mở rộng tác nhân Antigravity bằng cách tuỳ chỉnh các chỉ dẫn, công cụ và môi trường của nó. Tác nhân này hỗ trợ phương pháp tuỳ chỉnh gốc của hệ thống tệp: bạn có thể gắn các tệp như AGENTS.md cho hướng dẫn và kỹ năng trong .agents/skills/ trực tiếp vào hộp cát hoặc truyền cấu hình nội tuyến tại thời điểm tương tác. Bạn có thể lặp lại cấu hình nội tuyến rồi lưu cấu hình đó dưới dạng một tác nhân được quản lý khi đã sẵn sàng.

Để biết đầy đủ thông tin chi tiết về cách tạo các tác nhân tuỳ chỉnh, hãy xem bài viết Tạo tác nhân được quản lý.

Chạy ở chế độ nền

Các nhiệm vụ của tác nhân liên quan đến suy luận đa bước, thực thi mã hoặc thao tác với tệp có thể mất vài phút để hoàn thành. Sử dụng background=True để chạy lượt tương tác không đồng bộ. API này sẽ trả về ngay lập tức một mã nhận dạng tương tác mà bạn thăm dò cho đến khi trạng thái là completed hoặc failed.

Python

import time
from google import genai

client = genai.Client()

# 1. Start the interaction in the background
interaction = client.interactions.create(
    agent="antigravity-preview-05-2026",
    input="Run a complex analysis on the repository.",
    environment="remote",
    background=True,
)

print(f"Interaction started in background: {interaction.id}")

# 2. Poll for completion
while interaction.status == "in_progress":
    time.sleep(5)
    interaction = client.interactions.get(id=interaction.id)

if interaction.status == "completed":
    print(interaction.output_text)
else:
    print(f"Finished with status: {interaction.status}")

JavaScript

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

const client = new GoogleGenAI({});

const interaction = await client.interactions.create({
    agent: "antigravity-preview-05-2026",
    input: "Run a complex analysis on the repository.",
    environment: "remote",
    background: true,
});

console.log(`Interaction started in background: ${interaction.id}`);

let result = interaction;
while (result.status === "in_progress") {
    await new Promise(resolve => setTimeout(resolve, 5000));
    result = await client.interactions.get(interaction.id);
}

if (result.status === "completed") {
    console.log(result.output_text);
} else {
    console.log(`Finished with status: ${result.status}`);
}

REST

# 1. Start the interaction in the background
RESPONSE=$(curl -s -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
  -H "Content-Type: application/json" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -H "Api-Revision: 2026-05-20" \
  -d '{
      "agent": "antigravity-preview-05-2026",
      "input": "Run a complex analysis on the repository.",
      "environment": "remote",
      "background": true
  }')

INTERACTION_ID=$(echo $RESPONSE | jq -r '.id')

# 2. Poll for results (repeat until status is "completed")
curl -s -X GET "https://generativelanguage.googleapis.com/v1beta/interactions/$INTERACTION_ID" \
  -H "x-goog-api-key: $GEMINI_API_KEY"

Chế độ thực thi trong nền yêu cầu store=True (đây là chế độ mặc định). Để biết thông tin cập nhật tiến trình theo thời gian thực trong quá trình thực thi ở chế độ nền, hãy xem phần Truyền trực tuyến các hoạt động tương tác ở chế độ nền.

Bạn có thể huỷ một hoạt động tương tác đang chạy ở chế độ nền bằng phương thức cancel.

Python

client.interactions.cancel(id="INTERACTION_ID")

JavaScript

await client.interactions.cancel("INTERACTION_ID");

REST

curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions/INTERACTION_ID:cancel" \
  -H "x-goog-api-key: $GEMINI_API_KEY"

Nhiều lượt với tính năng thực thi trong nền

Khi một hoạt động tương tác ở chế độ nền liên quan đến các công cụ có trạng thái (chẳng hạn như thực thi mã trong một hộp cát), hãy sử dụng environment_id từ hoạt động tương tác đã hoàn tất để tiếp tục trong cùng một môi trường. Điều này đảm bảo rằng tác nhân sẽ tiếp tục từ nơi đã dừng lại với tất cả các tệp và trạng thái còn nguyên vẹn.

Python

import time
from google import genai

client = genai.Client()

# First turn: run a task in the background
interaction = client.interactions.create(
    agent="antigravity-preview-05-2026",
    input="Clone https://github.com/google/generative-ai-python and run its tests.",
    environment="remote",
    background=True,
)

while interaction.status == "in_progress":
    time.sleep(5)
    interaction = client.interactions.get(id=interaction.id)

# Second turn: continue in the same environment
followup = client.interactions.create(
    agent="antigravity-preview-05-2026",
    input="Fix any failing tests and re-run them.",
    previous_interaction_id=interaction.id,
    environment=interaction.environment_id,
    background=True,
)

while followup.status == "in_progress":
    time.sleep(5)
    followup = client.interactions.get(id=followup.id)

print(followup.output_text)

JavaScript

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

const client = new GoogleGenAI({});

// First turn: run a task in the background
let interaction = await client.interactions.create({
    agent: "antigravity-preview-05-2026",
    input: "Clone https://github.com/google/generative-ai-python and run its tests.",
    environment: "remote",
    background: true,
});

while (interaction.status === "in_progress") {
    await new Promise(resolve => setTimeout(resolve, 5000));
    interaction = await client.interactions.get(interaction.id);
}

// Second turn: continue in the same environment
let followup = await client.interactions.create({
    agent: "antigravity-preview-05-2026",
    input: "Fix any failing tests and re-run them.",
    previous_interaction_id: interaction.id,
    environment: interaction.environment_id,
    background: true,
});

while (followup.status === "in_progress") {
    await new Promise(resolve => setTimeout(resolve, 5000));
    followup = await client.interactions.get(followup.id);
}

console.log(followup.output_text);

REST

# 1. Start first interaction in the background
RESPONSE=$(curl -s -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
  -H "Content-Type: application/json" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -H "Api-Revision: 2026-05-20" \
  -d '{
      "agent": "antigravity-preview-05-2026",
      "input": "Clone https://github.com/google/generative-ai-python and run its tests.",
      "environment": "remote",
      "background": true
  }')

INTERACTION_ID=$(echo $RESPONSE | jq -r '.id')

# 2. Poll until completed (repeat until status is "completed")
RESULT=$(curl -s -X GET "https://generativelanguage.googleapis.com/v1beta/interactions/$INTERACTION_ID" \
  -H "x-goog-api-key: $GEMINI_API_KEY")

ENVIRONMENT_ID=$(echo $RESULT | jq -r '.environment_id')

# 3. Continue in the same environment
curl -s -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
  -H "Content-Type: application/json" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -H "Api-Revision: 2026-05-20" \
  -d "{
      \"agent\": \"antigravity-preview-05-2026\",
      \"input\": \"Fix any failing tests and re-run them.\",
      \"previous_interaction_id\": \"$INTERACTION_ID\",
      \"environment\": \"$ENVIRONMENT_ID\",
      \"background\": true
  }"

Môi trường

Mỗi lệnh gọi sẽ tạo hoặc dùng lại một hộp cát Linux. Tham số environment có 3 dạng:

Biểu mẫu Mô tả
"remote" Cung cấp một hộp cát mới với chế độ cài đặt mặc định.
"env_abc123" Tái sử dụng một môi trường hiện có theo mã nhận dạng, giữ nguyên mọi tệp và trạng thái.
{...} EnvironmentConfig đầy đủ với các quy tắc mạng và nguồn tuỳ chỉnh.

Hãy xem phần Môi trường để biết thông tin chi tiết về các nguồn (Git, GCS, nội tuyến), mạng, vòng đời và hạn mức tài nguyên.

Điều kiện kích hoạt

Trình kích hoạt cho phép bạn lên lịch để tác nhân chạy tự động theo lịch biểu cron. Trình kích hoạt liên kết tác nhân, môi trường, câu lệnh và lịch biểu thành một tài nguyên liên tục hoạt động mà không cần có sự can thiệp thủ công. Mỗi lần thực thi sẽ sử dụng lại cùng một môi trường, vì vậy, các tệp được tạo trong một lần chạy sẽ vẫn tồn tại và hiển thị cho lần chạy tiếp theo.

Tạo trình kích hoạt

Tạo một trình kích hoạt bằng cách chỉ định lịch biểu cron, múi giờ và cấu hình tương tác. Trình kích hoạt bắt đầu ở trạng thái active và sẽ kích hoạt vào thời gian cron khớp tiếp theo. Lưu id được trả về để quản lý điều kiện kích hoạt trong các lệnh gọi tiếp theo.

Python

from google import genai

client = genai.Client()

trigger = client.triggers.create(
    schedule="0 9 * * *",
    time_zone="America/Argentina/Buenos_Aires",
    display_name="issue-solver",
    interaction={
        "agent": "antigravity-preview-05-2026",
        "input": "Review open PRs in my-org/my-app for new comments and address feedback. Close issues whose PRs were merged. Then check for new issues labeled 'accepted', skip any already tracked in /workspace/solved-issues/, fix the rest, and open a PR for each. Save reports to /workspace/solved-issues/.",
        "environment": {
            "type": "remote",
            "network": {
                "allowlist": [
                    {
                        "domain": "api.github.com",
                        "transform": {
                            "Authorization": "Bearer ghp_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
                        },
                    },
                    {"domain": "github.com"},
                ]
            },
        },
    },
)

print(f"Trigger created: {trigger.id}")
print(f"Next run: {trigger.next_run_time}")

JavaScript

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

const client = new GoogleGenAI({});

const trigger = await client.triggers.create({
    schedule: "0 9 * * *",
    time_zone: "America/Argentina/Buenos_Aires",
    display_name: "issue-solver",
    interaction: {
        agent: "antigravity-preview-05-2026",
        input: [{
            type: "text",
            text: "Review open PRs in my-org/my-app for new comments and address feedback. Close issues whose PRs were merged. Then check for new issues labeled 'accepted', skip any already tracked in /workspace/solved-issues/, fix the rest, and open a PR for each. Save reports to /workspace/solved-issues/.",
        }],
        environment: {
            type: "remote",
            network: {
                allowlist: [
                    {
                        domain: "api.github.com",
                        transform: {
                            "Authorization": "Bearer ghp_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
                        },
                    },
                    { domain: "github.com" },
                ],
            },
        },
    },
});

console.log(`Trigger created: ${trigger.id}`);
console.log(`Next run: ${trigger.next_run_time}`);

REST

curl -X POST "https://generativelanguage.googleapis.com/v1beta/triggers" \
  -H "Content-Type: application/json" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -d '{
      "schedule": "0 9 * * *",
      "time_zone": "America/Argentina/Buenos_Aires",
      "display_name": "issue-solver",
      "interaction": {
          "agent": "antigravity-preview-05-2026",
          "input": [{"type": "text", "text": "Review open PRs in my-org/my-app for new comments and address feedback. Close issues whose PRs were merged. Then check for new issues labeled accepted, skip any already tracked in /workspace/solved-issues/, fix the rest, and open a PR for each. Save reports to /workspace/solved-issues/."}],
          "environment": {
              "type": "remote",
              "network": {
                  "allowlist": [
                      {
                          "domain": "api.github.com",
                          "transform": {
                              "Authorization": "Bearer ghp_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
                          }
                      },
                      {"domain": "github.com"}
                  ]
              }
          }
      }
  }'

Yêu cầu CreateTrigger chấp nhận các trường sau:

Trường Loại Bắt buộc Mô tả
schedule chuỗi Biểu thức cron (ví dụ: 0 * * * * cho mỗi giờ, 0 9 * * 1-5 cho các buổi sáng trong tuần).
time_zone chuỗi Múi giờ IANA (ví dụ: UTC, America/Argentina/Buenos_Aires).
display_name chuỗi Không Tên dễ đọc của điều kiện kích hoạt.
max_consecutive_failures số nguyên Không Số lần tối đa mà trình kích hoạt không hoạt động trước khi tự động tạm dừng. Mặc định: 5.
execution_timeout_seconds số nguyên Không Thời gian chờ cho mỗi lần thực thi tính bằng giây. Mặc định: 600.
interaction đối tượng Một CreateInteractionRequest xác định tác nhân, dữ liệu đầu vào, công cụ và môi trường.

Phản hồi bao gồm các trường khoá sau:

Trường Loại Mô tả
id chuỗi Giá trị nhận dạng duy nhất của điều kiện kích hoạt. Hãy sử dụng khoá này trong tất cả các thao tác tiếp theo.
status chuỗi Tiểu bang hiện tại: active, paused hoặc disabled.
next_run_time chuỗi Dấu thời gian ISO 8601 của lần thực thi theo lịch tiếp theo.
consecutive_failure_count số nguyên Số lần thực thi không thành công liên tiếp kể từ lần thực thi thành công gần đây nhất.

Liệt kê các điều kiện kích hoạt

Truy xuất tất cả các điều kiện kích hoạt được liên kết với dự án của bạn.

Python

triggers = client.triggers.list()
for trigger in triggers.triggers:
    print(f"{trigger.id}: {trigger.display_name} ({trigger.status})")

JavaScript

const triggers = await client.triggers.list();
for (const trigger of triggers.triggers) {
    console.log(`${trigger.id}: ${trigger.display_name} (${trigger.status})`);
}

REST

curl -X GET "https://generativelanguage.googleapis.com/v1beta/triggers" \
  -H "x-goog-api-key: $GEMINI_API_KEY"

Nhận điều kiện kích hoạt

Tìm nạp toàn bộ cấu hình và trạng thái hiện tại của một trình kích hoạt.

Python

trigger = client.triggers.get(id="TRIGGER_ID")
print(f"Schedule: {trigger.schedule}")
print(f"Next run: {trigger.next_run_time}")

JavaScript

const trigger = await client.triggers.get("TRIGGER_ID");
console.log(`Schedule: ${trigger.schedule}`);
console.log(`Next run: ${trigger.next_run_time}`);

REST

curl -X GET "https://generativelanguage.googleapis.com/v1beta/triggers/TRIGGER_ID" \
  -H "x-goog-api-key: $GEMINI_API_KEY"

Tạm dừng và tiếp tục

Bạn có thể tạm dừng một điều kiện kích hoạt để dừng các lượt thực thi theo lịch và tiếp tục điều kiện kích hoạt đó để kích hoạt lại lịch. Việc tạm dừng không ảnh hưởng đến các lượt thực thi thủ công.

Python

# Pause
client.triggers.update(id="TRIGGER_ID", status="paused")

# Resume
client.triggers.update(id="TRIGGER_ID", status="active")

JavaScript

// Pause
await client.triggers.update("TRIGGER_ID", { status: "paused" });

// Resume
await client.triggers.update("TRIGGER_ID", { status: "active" });

REST

# Pause
curl -X PATCH "https://generativelanguage.googleapis.com/v1beta/triggers/TRIGGER_ID" \
  -H "Content-Type: application/json" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -d '{"status": "paused"}'

# Resume
curl -X PATCH "https://generativelanguage.googleapis.com/v1beta/triggers/TRIGGER_ID" \
  -H "Content-Type: application/json" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -d '{"status": "active"}'

Xoá điều kiện kích hoạt

Xoá vĩnh viễn một điều kiện kích hoạt. Nhật ký thực thi trước đây sẽ không bị xoá.

Python

client.triggers.delete(id="TRIGGER_ID")

JavaScript

await client.triggers.delete("TRIGGER_ID");

REST

curl -X DELETE "https://generativelanguage.googleapis.com/v1beta/triggers/TRIGGER_ID" \
  -H "x-goog-api-key: $GEMINI_API_KEY"

Chạy một điều kiện kích hoạt ngay lập tức

Kích hoạt một điều kiện kích hoạt theo yêu cầu mà không cần đợi đến thời gian được lên lịch tiếp theo. Điều này vẫn có hiệu lực ngay cả khi điều kiện kích hoạt bị tạm dừng.

Python

client.triggers.run(trigger_id="TRIGGER_ID")

JavaScript

await client.triggers.run("TRIGGER_ID");

REST

curl -X POST "https://generativelanguage.googleapis.com/v1beta/triggers/TRIGGER_ID/executions" \
  -H "x-goog-api-key: $GEMINI_API_KEY"

Liệt kê các lượt thực thi

Xem nhật ký thực thi của một điều kiện kích hoạt. Mỗi lần thực thi đều có một status, dấu thời gian, một interaction_id mà bạn có thể dùng để tìm nạp toàn bộ kết quả tương tác và một environment_id xác nhận rằng tất cả các lần chạy đều dùng chung một hộp cát.

Python

executions = client.triggers.list_executions(trigger_id="TRIGGER_ID")
for ex in executions.trigger_executions:
    print(f"{ex.id}: {ex.status} ({ex.start_time} - {ex.end_time})")

# Fetch the full interaction for an execution
interaction = client.interactions.get(id=ex.interaction_id)
print(interaction.output_text)

JavaScript

const executions = await client.triggers.listExecutions("TRIGGER_ID");
for (const ex of executions.trigger_executions) {
    console.log(`${ex.id}: ${ex.status} (${ex.start_time} - ${ex.end_time})`);
}

// Fetch the full interaction for an execution
const interaction = await client.interactions.get(ex.interaction_id);
console.log(interaction.output_text);

REST

curl -X GET "https://generativelanguage.googleapis.com/v1beta/triggers/TRIGGER_ID/executions" \
  -H "x-goog-api-key: $GEMINI_API_KEY"

Tình trạng còn hàng và giá

Bạn có thể dùng thử tác nhân Antigravity thông qua Interactions API trong Google AI Studio và Gemini API cho cả dự án thuộc cấp miễn phí và cấp có tính phí.

Giá tuân theo mô hình trả tiền theo mức dùng dựa trên các token của mô hình Gemini cơ bản và các công cụ mà tác nhân sử dụng. Không giống như một yêu cầu trò chuyện tiêu chuẩn tạo ra một đầu ra duy nhất, tương tác Antigravity là một quy trình làm việc dựa trên tác nhân. Một yêu cầu duy nhất sẽ kích hoạt một vòng lặp tự động gồm suy luận, thực thi công cụ, chạy mã và quản lý tệp. Các dự án thuộc gói miễn phí có hạn mức sử dụng và hạn mức tốc độ miễn phí.

Các lượt tương tác phản trọng lực chạy các vòng lặp tự động nhiều lượt và có thể tiêu thụ một lượng lớn mã thông báo. Đặt chế độ kiểm soát ngân sách cho yêu cầu của bạn để giới hạn mức sử dụng mã thông báo. Bạn cũng có thể theo dõi tiến trình theo thời gian thực bằng SSE streaming hoặc huỷ các yêu cầu đang chạy.

Kiểm soát ngân sách

Ngoài lựa chọn mô hình, hãy đặt max_total_tokens bên trong agent_config (với "type": "antigravity") để giới hạn tổng số mã thông báo (đầu vào + đầu ra + suy nghĩ) mà một lượt tương tác có thể sử dụng. Các mã thông báo được lưu vào bộ nhớ đệm không được tính vào hạn mức này. Khi đạt đến giới hạn, lượt tương tác sẽ dừng và trả về status: "incomplete". Giới hạn này là giới hạn tốt nhất có thể: mức sử dụng thực tế có thể vượt quá một chút tuỳ thuộc vào thời điểm tác nhân kiểm tra ngân sách giữa các bước.

Đặt ngân sách cho yêu cầu tương tác trong agent_config cùng với agentinput.

Python

from google import genai

client = genai.Client()

interaction = client.interactions.create(
    agent="antigravity-preview-05-2026",
    input="Analyze the dataset in /workspace/data.csv and generate a summary report.",
    agent_config={
        "type": "antigravity",
        "max_total_tokens": 50000
    },
    environment={
        "type": "remote",
        "sources": [
            {
                "type": "inline",
                "target": "/workspace/data.csv",
                "content": "id,name,value\n1,alpha,100\n2,beta,200\n",
            }
        ],
    }
)
print(f"Status: {interaction.status}")  # "incomplete" if budget was hit
print(f"Tokens used: {interaction.usage.total_tokens}")

JavaScript

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

const client = new GoogleGenAI({});

const interaction = await client.interactions.create({
    agent: "antigravity-preview-05-2026",
    input: "Analyze the dataset in /workspace/data.csv and generate a summary report.",
    agent_config: {
        type: "antigravity",
        max_total_tokens: 50000
    },
    environment: {
        type: "remote",
        sources: [
            {
                type: "inline",
                target: "/workspace/data.csv",
                content: "id,name,value\n1,alpha,100\n2,beta,200\n",
            },
        ],
    },
});
console.log(`Status: ${interaction.status}`);
console.log(`Tokens used: ${interaction.usage.total_tokens}`);

REST

curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
  -H "Content-Type: application/json" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -d '{
    "agent": "antigravity-preview-05-2026",
    "input": "Analyze the dataset in /workspace/data.csv and generate a summary report.",
    "agent_config": {
      "type": "antigravity",
      "max_total_tokens": 50000
    },
    "environment": {
      "type": "remote",
      "sources": [
        {
          "type": "inline",
          "target": "/workspace/data.csv",
          "content": "id,name,value\n1,alpha,100\n2,beta,200\n"
        }
      ]
    }
  }'

Tiếp tục một lượt tương tác chưa hoàn tất

Khi một lượt tương tác trả về status: "incomplete", công việc và bối cảnh của nhân viên hỗ trợ sẽ được giữ nguyên. Gửi một lượt tương tác mới tham chiếu đến lượt tương tác ban đầu idenvironment_id để tiếp tục từ nơi dừng lại. Lượt tương tác mới sẽ có ngân sách max_total_tokens riêng.

Python

# Continue from where the agent stopped
continuation = client.interactions.create(
    agent="antigravity-preview-05-2026",
    input="continue",
    previous_interaction_id=interaction.id,
    environment=interaction.environment_id,
    agent_config={
        "type": "antigravity",
        "max_total_tokens": 50000
    }
)
print(f"Status: {continuation.status}")

JavaScript

const continuation = await client.interactions.create({
    agent: "antigravity-preview-05-2026",
    input: "continue",
    previous_interaction_id: interaction.id,
    environment: interaction.environment_id,
    agent_config: {
        type: "antigravity",
        max_total_tokens: 50000
    }
});
console.log(`Status: ${continuation.status}`);

REST

curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
  -H "Content-Type: application/json" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -d '{
    "agent": "antigravity-preview-05-2026",
    "input": "continue",
    "previous_interaction_id": "INTERACTION_ID",
    "environment": "ENVIRONMENT_ID",
    "agent_config": {
      "type": "antigravity",
      "max_total_tokens": 50000
    }
  }'

Chi phí ước tính

Chi phí sẽ khác nhau tuỳ thuộc vào độ phức tạp của nhiệm vụ. Tác nhân tự động xác định số lượng lệnh gọi công cụ, lượt thực thi mã và thao tác tệp cần thiết. Các số liệu ước tính sau đây dựa trên các lần chạy.

Danh mục việc cần làm Mã thông báo nhập Mã thông báo xuất Chi phí thông thường
Nghiên cứu và tổng hợp thông tin 100.000 – 500.000 10.000 – 40.000 0,3 – 1 USD
Tạo tài liệu và nội dung 100.000 – 500.000 15.000 – 50.000 0,3 – 1,3 USD
Thiết kế quy trình và hệ thống 100.000 – 400.000 10.000 – 30.000 0,25 – 0,80 USD
Xử lý và phân tích dữ liệu 300.000 – 3.000.000 30.000 – 150.000 0,70 – 3,25 USD

Thông thường, 50–70% mã thông báo đầu vào được lưu vào bộ nhớ đệm. Các quy trình làm việc phức tạp của tác nhân với nhiều lệnh gọi công cụ có thể tích luỹ từ 3 đến 5 triệu mã thông báo trong một lượt tương tác, với chi phí lên đến khoảng 5 USD.

Điện toán môi trường (CPU, bộ nhớ, thực thi hộp cát) sẽ không bị tính phí trong thời gian xem trước.

Các điểm hạn chế

  • Trạng thái xem trước: Tác nhân Antigravity và Interactions API. Các tính năng và giản đồ có thể thay đổi.
  • Cấu hình tạo không được hỗ trợ: Các tham số sau không được hỗ trợ và trả về lỗi 400: temperature, top_p, top_k, stop_sequences, max_output_tokens.
  • Đầu ra có cấu trúc: Tác nhân Antigravity không hỗ trợ đầu ra có cấu trúc.
  • Các công cụ không dùng được: file_search, computer_usegoogle_maps hiện chưa được hỗ trợ.
  • Các hạn chế đối với MCP từ xa: Không hỗ trợ phương thức truyền Sự kiện được gửi bởi máy chủ (SSE) (hãy dùng HTTP có thể truyền trực tuyến). Ngoài ra, name máy chủ phải hoàn toàn là chữ thường và chữ và số (việc sử dụng chữ hoa sẽ kích hoạt lỗi chung 400 Bad Request).
  • Công cụ hệ thống tệp: Hiện không có công cụ hệ thống tệp. Đây là một phần của environment.
  • Yêu cầu về cửa hàng: Việc thực thi tác nhân bằng background=True yêu cầu store=True.
  • Chỉ gọi hàm có trạng thái: Tính năng gọi hàm chỉ được hỗ trợ ở chế độ có trạng thái. Bạn phải sử dụng previous_interaction_id để tiếp tục lượt trò chuyện; hệ thống không hỗ trợ việc khôi phục nhật ký theo cách thủ công (chế độ không trạng thái).
  • Các loại nội dung đa phương thức không được hỗ trợ. Hiện tại, chúng tôi không hỗ trợ dữ liệu đầu vào là âm thanh, video và tài liệu. Bạn chỉ được phép sử dụng văn bản và hình ảnh.

Bước tiếp theo