इस गाइड में, Gemini API पर मैनेज किए गए एजेंट बनाने और उनका इस्तेमाल करने के बारे में बताया गया है. इसके लिए, Antigravity एजेंट का इस्तेमाल किया गया है. इसमें, एजेंट को पहली बार कॉल करना, सिलसिलेवार बातचीत जारी रखना, जवाब को स्ट्रीम करना, सैंडबॉक्स से फ़ाइलें डाउनलोड करना, और Antigravity मैनेज किए गए एजेंट के साथ काम करना शामिल है.
एजेंट के साथ पहली बार इंटरैक्ट करना
Interactions API को एक बार कॉल करने पर, Linux सैंडबॉक्स उपलब्ध कराया जाता है. साथ ही, एजेंट लूप चलाया जाता है और नतीजा दिखाया जाता है. आपको तीन पैरामीटर तय करने होंगे:
agentको"antigravity-preview-05-2026",के तौर पर पास करें. यह, पहले से तय और सामान्य मकसद के लिए मैनेज किए गए एजेंट का मौजूदा वर्शन है.- नया, ताज़ा सैंडबॉक्स एनवायरमेंट उपलब्ध कराने के लिए,
environment="remote"तय करें. एक इनपुट बनाएं. इसमें तय करें कि आपको एजेंट से क्या काम कराना है.
Python
from google import genai
client = genai.Client()
interaction = client.interactions.create(
agent="antigravity-preview-05-2026",
input="Write a Python script that generates the first 20 Fibonacci numbers and saves them to fibonacci.txt. Then read the file and print its contents.",
environment="remote",
)
# Print the agent's final output
print(f"Interaction ID: {interaction.id}")
print(f"Environment ID: {interaction.environment_id}")
print(f"Output: {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: "Write a Python script that generates the first 20 Fibonacci numbers and saves them to fibonacci.txt. Then read the file and print its contents.",
environment: "remote",
});
console.log(`Interaction ID: ${interaction.id}`);
console.log(`Environment ID: ${interaction.environment_id}`);
console.log(`Output: ${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": [{"type": "text", "text": "Write a Python script that generates the first 20 Fibonacci numbers and saves them to fibonacci.txt. Then read the file and print its contents."}],
"environment": {"type": "remote"}
}'
जवाब में, Interaction ऑब्जेक्ट दिखता है. एक ही सैंडबॉक्स में बातचीत जारी रखने के लिए, interaction.id और interaction.environment_id को सेव करें. एजेंट के आखिरी जवाब को ऐक्सेस करने के लिए, interaction.output_text का इस्तेमाल करें. interaction.steps में, एजेंट की ओर से उठाए गए हर चरण की सूची दिखती है. जैसे, तर्क, टूल कॉल, कोड का एक्ज़ीक्यूशन.
सिलसिलेवार बातचीत जारी रखें
एपीआई, दो अलग-अलग स्टेट डाइमेंशन ट्रैक करता है:
- बातचीत का कॉन्टेक्स्ट: चैट का इतिहास, तर्क का ट्रेस, टूल का इस्तेमाल. इसके लिए,
previous_interaction_idका इस्तेमाल किया जाता है. - एनवायरमेंट की स्थिति: फ़ाइलें, इंस्टॉल किए गए पैकेज, और सैंडबॉक्स की स्थिति. इसके लिए,
environmentका इस्तेमाल किया जाता है.
बातचीत फिर से शुरू करने के लिए, दोनों को उनकी तय की गई जगह पर पास करें:
Python
interaction_2 = client.interactions.create(
agent="antigravity-preview-05-2026",
previous_interaction_id=interaction.id,
environment=interaction.environment_id,
input="Now plot the Fibonacci sequence as a line chart and save it as chart.png.",
)
print(interaction_2.output_text)
JavaScript
const interaction2 = await client.interactions.create({
agent: "antigravity-preview-05-2026",
previous_interaction_id: interaction.id,
environment: interaction.environment_id,
input: "Now plot the Fibonacci sequence as a line chart and save it as chart.png.",
}, { timeout: 300_000 });
console.log(interaction2.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",
"previous_interaction_id": "interaction_id_from_step_1",
"environment": "environment_id_from_step_1",
"input": [{"type": "text", "text": "Now plot the Fibonacci sequence as a line chart and save it as chart.png."}]
}'
पहले चरण (fibonacci.txt) की फ़ाइलें, दूसरे चरण में भी बनी रहती हैं. एजेंट, बातचीत का कॉन्टेक्स्ट भी बनाए रखता है.
इन्हें अलग-अलग तरीके से जोड़ा जा सकता है:
- बातचीत मिटाएं, फ़ाइलें सेव रखें:
previous_interaction_idको छोड़ें. साथ ही, एक ही वर्कस्पेस में नई बातचीत के लिए,environmentका इस्तेमाल करके सिर्फ़ एनवायरमेंट आईडी पास करें. - बातचीत सेव रखें, नया वर्कस्पेस:
previous_interaction_idपास करें. साथ ही, नए सैंडबॉक्स के लिएenvironment="remote"सेट करें.
कॉन्टेक्स्ट को अपने-आप कंप्रेस करना
ज़्यादा समय तक चलने वाली, सिलसिलेवार बातचीत में, गहराई से विश्लेषण के चरणों का रॉ इतिहास, टूल कॉल, और बड़ी फ़ाइल के कॉन्टेंट तेज़ी से बढ़ सकते हैं. साथ ही, ये कॉन्टेक्स्ट की काफ़ी जगह घेर सकते हैं. टोकन की सीमा से जुड़ी गड़बड़ियां रोकने और एजेंट बनाने और मैनेज करने की सुविधा का फ़ोकस बनाए रखने के लिए (कॉन्टेक्स्ट रॉट को रोकने के लिए), एजेंट बनाने और मैनेज करने की सुविधा के एपीआई में, करीब 1,35,000 टोकन पर, कॉन्टेक्स्ट को कंप्रेस करने का एक नेटिव चरण शामिल है. यह अपने-आप होता है.
जवाब को स्ट्रीम करना
ज़्यादा समय तक चलने वाले टास्क के लिए, जवाब को स्ट्रीम किया जा सकता है. इससे, एजेंट के काम करने का तरीका रीयल टाइम में देखा जा सकता है:
Python
from google import genai
client = genai.Client()
stream = client.interactions.create(
agent="antigravity-preview-05-2026",
input="Read Hacker News, summarize the top 5 stories, and save the results as a PDF.",
environment="remote",
stream=True,
)
for event in stream:
print(event)
JavaScript
import { GoogleGenAI } from "@google/genai";
const client = new GoogleGenAI({});
const stream = await client.interactions.create({
agent: "antigravity-preview-05-2026",
input: "Read Hacker News, summarize the top 5 stories, and save the results as a PDF.",
environment: "remote",
stream: true,
});
for await (const event of stream) {
console.log(event);
}
REST
curl -N -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": "Read Hacker News, summarize the top 5 stories, and save the results as a PDF.",
"environment": "remote",
"stream": true
}'
स्ट्रीमिंग से, चरण के डेल्टा का एक इटरेबल मिलता है. यह इंक्रीमेंटल टेक्स्ट, तर्क के टोकन, और टूल कॉल के अपडेट होते हैं. स्ट्रीमिंग गाइड में, जवाबों को स्ट्रीम करने के तरीके के बारे में ज़्यादा जानें.
एनवायरमेंट से फ़ाइलें डाउनलोड करना
जब एजेंट, सैंडबॉक्स में फ़ाइलें बनाता है. इन्हें Files API का इस्तेमाल करके, सीधे एचटीटीपी अनुरोध से डाउनलोड करें. फ़िलहाल, इसके लिए एसडीके टूल का कोई तरीका उपलब्ध नहीं है:
Python
import os
import requests
import tarfile
env_id = interaction.environment_id
api_key = os.environ["GEMINI_API_KEY"]
response = requests.get(
f"https://generativelanguage.googleapis.com/v1beta/files/environment-{env_id}:download",
params={"alt": "media"},
headers={"x-goog-api-key": api_key},
allow_redirects=True,
)
with open("snapshot.tar", "wb") as f:
f.write(response.content)
with tarfile.open("snapshot.tar") as tar:
tar.extractall(path="extracted_snapshot")
JavaScript
import fs from "fs";
import { execSync } from "child_process";
const envId = interaction.environment_id;
const apiKey = process.env.GEMINI_API_KEY || "";
const url = `https://generativelanguage.googleapis.com/v1beta/files/environment-${envId}:download?alt=media`;
const response = await fetch(url, {
headers: {
"x-goog-api-key": apiKey,
},
});
if (!response.ok) {
throw new Error(`Failed to download file: ${response.statusText}`);
}
const buffer = Buffer.from(await response.arrayBuffer());
fs.writeFileSync("snapshot.tar", buffer);
if (!fs.existsSync("extracted_snapshot")) {
fs.mkdirSync("extracted_snapshot");
}
execSync("tar -xf snapshot.tar -C extracted_snapshot");
console.log(fs.readdirSync("extracted_snapshot"));
REST
curl -L -X GET "https://generativelanguage.googleapis.com/v1beta/files/environment-$ENV_ID:download?alt=media" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-o snapshot.tar
tar -xf snapshot.tar -C extracted_snapshot
मैनेज किए गए एजेंट को सेव करना
पिछले चरणों में, हमने डिफ़ॉल्ट Antigravity एजेंट का इस्तेमाल किया और उसे इनलाइन तरीके से पसंद के मुताबिक बनाया. कॉन्फ़िगरेशन (निर्देश, कौशल, मॉडल का चुनाव, और एनवायरमेंट) को दोहराने के बाद, इसे मैनेज किए गए ऐसे एजेंट के तौर पर सेव किया जा सकता है जिसका फिर से इस्तेमाल किया जा सके. इससे, कॉन्फ़िगरेशन को दोहराए बिना, आईडी की मदद से इसे शुरू किया जा सकता है.
किसी एजेंट को सेव करते समय, इनलाइन इंटरैक्शन के साथ आर्किटेक्चरल सिमेट्री पर ध्यान दें: इसमें base_agent: "antigravity-preview-05-2026" तय किया जाता है. साथ ही, interactions.create पर, चुने गए model के साथ agent_config पास किया जा सकता है. इसमें, base_environment भी तय किया जाता है. यह सोर्स से या किसी मौजूदा एनवायरमेंट को फ़ोर्क करके तय किया जा सकता है. एजेंट, हर नए इंटरैक्शन के लिए इस एनवायरमेंट और मॉडल कॉन्फ़िगरेशन का इस्तेमाल करेगा.
सोर्स से: सोर्स को इनलाइन तरीके से या GitHub या Cloud Storage जैसे अन्य सोर्स से तय करें.
Python
agent = client.agents.create(
id="fibonacci-analyst",
base_agent="antigravity-preview-05-2026",
agent_config={
"type": "antigravity",
"model": "gemini-3.6-flash",
},
system_instruction="You are a math analysis agent. Generate sequences, visualize them, and export results as PDF reports.",
base_environment={
"type": "remote",
"sources": [
{
"type": "inline",
"target": ".agents/AGENTS.md",
"content": "Always include a chart and a summary table in your reports.",
},
{
"type": "repository",
"source": "https://github.com/your-org/skills",
"target": ".agents/skills"
}
],
},
)
print(f"Saved agent: {agent.id}")
JavaScript
const agent = await client.agents.create({
id: "fibonacci-analyst",
base_agent: "antigravity-preview-05-2026",
agent_config: {
type: "antigravity",
model: "gemini-3.6-flash",
},
system_instruction: "You are a math analysis agent. Generate sequences, visualize them, and export results as PDF reports.",
base_environment: {
type: "remote",
sources: [
{
type: "inline",
target: ".agents/AGENTS.md",
content: "Always include a chart and a summary table in your reports.",
},
{
type: "repository",
source: "https://github.com/your-org/skills",
target: ".agents/skills"
}
],
},
});
console.log(`Saved agent: ${agent.id}`);
REST
curl -X POST "https://generativelanguage.googleapis.com/v1beta/agents" \
-H "Content-Type: application/json" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-d '{
"id": "fibonacci-analyst",
"base_agent": "antigravity-preview-05-2026",
"agent_config": {
"type": "antigravity",
"model": "gemini-3.6-flash"
},
"system_instruction": "You are a math analysis agent. Generate sequences, visualize them, and export results as PDF reports.",
"base_environment": {
"type": "remote",
"sources": [
{
"type": "inline",
"target": ".agents/AGENTS.md",
"content": "Always include a chart and a summary table in your reports."
},
{
"type": "repository",
"source": "https://github.com/your-org/skills",
"target": ".agents/skills"
}
]
}
}'
मैनेज किए गए एजेंट को शुरू करना
मैनेज किए गए एजेंट को सेव करने के बाद, इसे आईडी की मदद से शुरू किया जा सकता है. हर बार शुरू करने पर, बेस एनवायरमेंट फ़ोर्क होता है. इसलिए, हर बार शुरू करने पर यह साफ़ होता है:
Python
result = client.interactions.create(
agent="fibonacci-analyst",
input="Generate the first 50 prime numbers, plot their distribution, and save a PDF report.",
environment="remote",
)
print(result.output_text)
JavaScript
const result = await client.interactions.create({
agent: "fibonacci-analyst",
input: "Generate the first 50 prime numbers, plot their distribution, and save a PDF report.",
environment: "remote",
}, {
timeout: 300_000,
});
console.log(result.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": "fibonacci-analyst",
"environment": "remote",
"input": "Generate the first 50 prime numbers, plot their distribution, and save a PDF report."
}'
आगे क्या करना है
- Antigravity एजेंट: क्षमताएं, काम करने वाले टूल, मल्टीमॉडल इनपुट, कीमतें, और सीमाएं.
- एजेंट बनाने और मैनेज करने की सुविधा: Antigravity को अपने निर्देश, स्किल, और डेटा के साथ बढ़ाना.
- एनवायरमेंट: सोर्स, नेटवर्किंग, लाइफ़साइकल, संसाधन की सीमाएं.
- Interactions API: मॉडल और एजेंट के लिए, बुनियादी एपीआई.