GEMINI LABJP
CLI — As of Jun 18, Gemini CLI and the Gemini Code Assist IDE extensions stop serving AI Pro/Ultra and free individual users; Antigravity CLI is the successorFLASH — The Gemini 3.5 series begins with 3.5 Flash, built for agents and coding with strength on long-horizon tasksDEEPTHINK — Gemini 3 Deep Think is rolling out to Google AI Ultra as the top reasoning mode for math, science, and logicAPP — The Gemini app gains a Daily Brief, a redesigned interface, the Gemini Omni video model, and a personal agent called Gemini SparkDESIGN — A new design language, Neural Expressive, rebuilds the experience for richer visuals and faster switching between modalitiesULTRA — Google AI Ultra bundles top model access, Deep Research, Veo 3 video, and a 1M-token context windowCLI — As of Jun 18, Gemini CLI and the Gemini Code Assist IDE extensions stop serving AI Pro/Ultra and free individual users; Antigravity CLI is the successorFLASH — The Gemini 3.5 series begins with 3.5 Flash, built for agents and coding with strength on long-horizon tasksDEEPTHINK — Gemini 3 Deep Think is rolling out to Google AI Ultra as the top reasoning mode for math, science, and logicAPP — The Gemini app gains a Daily Brief, a redesigned interface, the Gemini Omni video model, and a personal agent called Gemini SparkDESIGN — A new design language, Neural Expressive, rebuilds the experience for richer visuals and faster switching between modalitiesULTRA — Google AI Ultra bundles top model access, Deep Research, Veo 3 video, and a 1M-token context window
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 API138Managed AgentsInteractions APIAgents6Sandbox

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-06-17
Watching the 'Voice' of Generated Text: Catching a Silent Default-Model Swap Through Style Drift
When the default model changes over your head, the output can stay factually correct while its voice quietly shifts. This walks through fingerprinting the style of generated text and detecting drift statistically, with a dependency-free implementation you can drop into your pipeline.
API / SDK2026-06-15
Put Help Docs and Screenshots in One File Search Store and Return Answers That Cite the Image Too
Your text help docs and your screenshots live in separate stores, so a single question can never return both the steps and the matching screen. With gemini-embedding-2 going multimodal in File Search, here is how I merged them and returned the cited screenshot alongside the answer.
API / SDK2026-06-14
Generate With Flash, Escalate to Deep Think Only When Unsure: A Two-Stage Pipeline
With Deep Think opening up on the API, the move is not to route every request through the heavy model but to have Deep Think verify only when Flash's output looks shaky. Here is the cost reasoning and 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 →