GEMINI LABJP
FLASH35 — Gemini 3.5 Flash is now GA and powers gemini-flash-latest, making everyday generation faster and more affordableAGENTS — Managed Agents launch in public preview in the Gemini API, running in secure, isolated Google-hosted Linux sandboxesMEDIA — Nano Banana 2 Lite and Gemini Omni Flash bring faster image and high-quality video generation across AI Studio and the APITTS — Streaming speech generation is now supported for gemini-3.1-flash-tts-preview via streamGenerateContentTRANSLATE — A new audio model detects 70+ languages for live speech-to-speech translation while preserving natural intonationSPENDCAP — Project-level spend caps for billing have been added in Google AI Studio to keep costs under controlFLASH35 — Gemini 3.5 Flash is now GA and powers gemini-flash-latest, making everyday generation faster and more affordableAGENTS — Managed Agents launch in public preview in the Gemini API, running in secure, isolated Google-hosted Linux sandboxesMEDIA — Nano Banana 2 Lite and Gemini Omni Flash bring faster image and high-quality video generation across AI Studio and the APITTS — Streaming speech generation is now supported for gemini-3.1-flash-tts-preview via streamGenerateContentTRANSLATE — A new audio model detects 70+ languages for live speech-to-speech translation while preserving natural intonationSPENDCAP — Project-level spend caps for billing have been added in Google AI Studio to keep costs under control
Articles/API / SDK
API / SDK/2026-06-16Advanced

Wiring Gemini Managed Agents Into Your Automation: Keeping Conversation State and Environment State Apart

Managed Agents spin up a Linux sandbox, run an agent loop, and return a result in a single API call. The first thing that trips you up when moving off a hand-rolled loop is that conversation state and file state are two separate things. Here's that design, worked through live.

Gemini API183Managed Agents5Interactions API3Agents7Sandbox

Premium Article

The first time I wired the public-preview Managed Agents into my own automation, the thing that confused me most in the opening half hour wasn't an error or a cost estimate. It was figuring out where state actually lives. A single call to client.interactions.create(...) stands up a Linux sandbox on Google's side, has Gemini 3.5 Flash write and run code inside it, and hands back a block of text. That part is almost anticlimactically easy. The trouble started on the second call. Trying to ask the agent to "continue where it left off," I assumed the conversation history and the sandbox files were the same thing, carried only one of them forward, and lost the better part of an hour.

This memo takes that misunderstanding head-on, because anyone who has hand-written an agent loop is likely to stub their toe on exactly this. The running example is a small formatting helper I use in my own work as an indie developer: it takes text, cleans it up, generates one chart, and writes the result out.

What actually happens in a single call

Start with the smallest possible round trip. For grounding alone, it's worth firing one off yourself and looking at the shape of the response.

# pip install google-genai
from google import genai
 
client = genai.Client()  # reads GEMINI_API_KEY from the environment
 
interaction = client.interactions.create(
    agent="antigravity-preview-05-2026",   # the default general-purpose Managed Agent
    input="Generate the first 20 Fibonacci numbers and save them to fibonacci.txt. "
          "Then read the file back and print its contents.",
    environment="remote",                  # provision a fresh sandbox every time
)
 
print("interaction.id =", interaction.id)
print("environment_id =", interaction.environment_id)
print("output_text    =", interaction.output_text)
print("steps          =", len(interaction.steps))  # reasoning, tool calls, code execution

That one create call provisions the sandbox, runs the agent loop, and returns the result, all at once. On the returned Interaction object, three fields are the ones I keep my eye on in practice. output_text is the final answer; steps is the array of each step the agent took (reasoning, tool call, code execution); and environment_id identifies the sandbox you just used. That last one is the key to doing anything "next."

On my formatting task, steps typically ran between six and eleven entries. Even a short instruction goes through plan → generate code → run → inspect file, so rather than stopping at output_text, it pays to glance at steps to see how the agent actually solved it.

The real snag: there are two axes of state

Managed Agents track state across two independent dimensions. Treating them as one is the classic trap for anyone coming from a hand-rolled loop.

The first is conversation context: chat history, the reasoning trace, the flow of tool use. You carry it forward by passing the previous interaction.id as previous_interaction_id.

The second is environment state: the files in the sandbox, the installed packages, the contents of the working directory. You carry it forward by passing the previous environment_id as environment.

You pass them separately, not mixed together.

interaction_2 = client.interactions.create(
    agent="antigravity-preview-05-2026",
    previous_interaction_id=interaction.id,          # continue the conversation
    environment=interaction.environment_id,          # keep the files too
    input="Now plot that sequence as a line chart and save it as chart.png.",
)
print(interaction_2.output_text)

Here fibonacci.txt still exists on the second turn, and the agent remembers what "that sequence" refers to. My first failure was passing only previous_interaction_id while setting environment="remote" — a brand-new box. The conversation continues, but the files are gone, so the agent honestly reports that it can't find the earlier file. Once "state has two axes" clicks, that's exactly the behavior you'd expect.

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
How previous_interaction_id (conversation) and environment (files) are two independent axes, shown across all four useful combinations
Before/after code for moving a hand-rolled agent loop onto Managed Agents, with a tally of the boilerplate that disappears
Field notes on the 7-day sandbox TTL, automatic ~135k-token compaction, and pulling generated files back out
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

Gemini 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

API / SDK2026-07-04
When Two Managed Agents Fight Over the Same Repo: External Leases and Fencing for Isolated Sandboxes
Every Managed Agents run gets its own isolated sandbox, so a local lock cannot stop two runs from touching the same repo or record. Here is how I serialize them safely with an external lease and a fencing token.
API / SDK2026-07-01
Keeping Unattended Jobs From Failing Silently: A Preflight Gate for Gemini's Platform Changes
Unrestricted API keys are now rejected, the old CLI reached end of life, and the Interactions API is becoming the default entry point. These 2026 platform shifts stop working automation without raising an error. Here is a preflight gate, with runnable code, that catches the failure before the batch runs.
API / SDK2026-06-30
Fire-and-Forget on a Cron That Never Loses a Result: Reclaiming Gemini Background Executions with a Submission Ledger
A design for running the Interactions API's background execution safely from a cron-driven runner. We reserve a row in a ledger by idempotency key before submitting, then reclaim only outstanding handles on the next tick — shown with working code.
📚RECOMMENDED BOOKS
Build a Large Language Model (From Scratch)
Sebastian Raschka
LLM Dev
Prompt Engineering for LLMs
Berryman & Ziegler
Prompting
AI Engineering
Chip Huyen
AI Eng
* Contains affiliate links
See all →