Antigravity 代理程式是 Gemini API 中的一般用途受管理代理程式,只要呼叫單一 API,您就能在 Google 代管的專屬安全 Linux 沙箱中,取得可進行推理、執行程式碼、管理檔案及瀏覽網頁的代理程式。
這項工具採用 Gemini 3.6 Flash 技術,並使用與 Antigravity IDE 相同的架構。您可以使用 agent_config 設定基礎 Gemini 模型。可透過 Interactions API 和 Google 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"
}'
功能
每次呼叫都會佈建 Linux 沙箱,並啟動工具使用迴圈。代理會規劃、行動、觀察結果,並重複執行這些步驟,直到完成工作為止。
- 執行程式碼:執行 Bash、Python 和 Node.js 指令。安裝套件、執行測試、建構應用程式。
- 檔案管理:讀取、寫入、編輯、搜尋及列出沙箱中的檔案。檔案會保留在所有互動中。
- 網路存取權:Google 搜尋和網址擷取功能,可取得資料。
- 內容壓縮:自動壓縮內容 (約 135, 000 個權杖時觸發),支援長時間的多輪對話,不會遺失內容或達到權杖上限。
如要瞭解如何使用多輪對話和串流功能,請參閱快速入門導覽課程。
支援的工具
根據預設,代理程式可以存取 code_execution、google_search 和 url_context。指定 environment 參數時,系統會自動啟用檔案系統工具。您也可以定義自訂函式,將代理程式連結至自己的 API 和工具。自訂或限制預設集,或是新增自訂函式時,才需要指定 tools 參數。
| 工具 | 輸入值 | 說明 |
|---|---|---|
| 程式碼執行 | code_execution |
執行殼層指令 (bash、Python、Node),並擷取 stdout/stderr。 |
| Google 搜尋 | google_search |
搜尋公開網路。 |
| 網址背景資訊 | url_context |
擷取及閱讀網頁。 |
| 檔案系統 | (透過 environment 啟用) |
在沙箱中讀取、寫入、編輯、搜尋及列出檔案。設定 environment 時,系統會自動啟用這些工具。 |
| 自訂函式 | function |
定義代理可要求執行的自訂函式。請參閱函式呼叫。 |
| 遠端 MCP 伺服器 | mcp_server |
將外部 Model Context Protocol (MCP) 伺服器註冊為工具。請參閱「MCP 伺服器」。 |
您可以使用同步 Hook,在遠端沙箱中攔截並驗證 code_execution 和 filesystem 工具的執行作業。
如要限制代理程式只能使用特定工具,請只傳遞您需要的工具:
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"}
]
}'
多模態輸入
Antigravity 代理程式支援多模態輸入,目前僅支援 text 和 image 輸入內容。圖片必須以內嵌的 Base64 編碼字串 (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\"
}"
函式呼叫
您可以定義代理可呼叫的自訂工具,透過函式呼叫將 Antigravity 代理程式連結至外部 API 和資料庫。如要瞭解一般概念,請參閱「使用 Gemini API 進行函式呼叫」。
以下範例說明 2 輪互動。代理會先要求自訂 get_weather 函式呼叫,用戶端執行該函式後,會在第二輪傳回結果。
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\"
}
}
]
}"
MCP 伺服器
註冊遠端 Model Context Protocol (MCP) 伺服器,即可將 Antigravity 代理程式連結至外部工具。代理程式支援透過可串流的 HTTP 連至遠端 MCP 伺服器。
註冊 MCP 伺服器時,您必須在 tools 陣列中指定下列欄位:
| 欄位 | 類型 | 必要 | 說明 |
|---|---|---|---|
type |
字串 | 是 | 必須為 "mcp_server"。 |
name |
字串 | 是 | 伺服器的專屬 ID。必須是嚴格的小寫英數字元 (與 ^[a-z0-9_-]+$ 相符)。 |
url |
字串 | 是 | 遠端 MCP 伺服器的端點網址。 |
headers |
物件 | 否 | 隨要求傳送的自訂標頭 (例如驗證)。 |
allowed_tools |
陣列 | 否 | 允許執行的工具名稱清單。如果省略,系統會允許所有工具。 |
Python
from google import genai
client = genai