◉GEMINI LABJP
●CLI 0.71 — Gemini CLI v0.71.0 is out as a preview. The stable release is still v0.70.0●TOOL RETR — Tool retrieval (Preview) lets the model load tool definitions only when it needs them●2.5 ROUTE — From 12/1, gemini-2.5-pro routes to 3.8 Flash and 2.5-flash to 3.5 Flash-Lite, billed at the new rates (9 days left)●OAUTH — Users report a stored MCP OAuth token being sent to a repointed server URL without asking●PICK — Choosing a daily Gemini 4 model gets easier when you weigh reasoning, long context and price●AG NEON — Antigravity App v2.34.0 adds Gemini 4 Neon for Gemini API keys●CLI 0.71 — Gemini CLI v0.71.0 is out as a preview. The stable release is still v0.70.0●TOOL RETR — Tool retrieval (Preview) lets the model load tool definitions only when it needs them●2.5 ROUTE — From 12/1, gemini-2.5-pro routes to 3.8 Flash and 2.5-flash to 3.5 Flash-Lite, billed at the new rates (9 days left)●OAUTH — Users report a stored MCP OAuth token being sent to a repointed server URL without asking●PICK — Choosing a daily Gemini 4 model gets easier when you weigh reasoning, long context and price●AG NEON — Antigravity App v2.34.0 adds Gemini 4 Neon for Gemini API keys
Articles/API / SDK
◈ API / SDK/2026-06-19Advanced

Your Managed Agents Bill Has a Second Axis: Drawing a Budget Boundary Around Sandbox Runtime

Managed Agents in public preview bills for tokens and for how long its Google-hosted sandbox stays alive. A single hung run quietly drains your budget on that second axis. Here is a working Python design for wall-clock caps, idle teardown, and a concurrency ceiling.

gemini-api291managed-agents4cost-management8production140automation55agent-design3

✦ Premium Article

One morning I was reading through a Managed Agents bill, and I stopped.

Token consumption sat almost exactly where I had estimated it. And yet the total refused to line up with the figure in my head. Tracing the gap, the culprit was not tokens at all — it was the time the Google-hosted sandbox had spent alive.

As an indie developer, I run content updates for several sites on a schedule alongside my own product work. I dropped the Managed Agents public preview into that pipeline to shave a layer off my own agent loop. Then one run quietly hung while waiting on a tool response, and the sandbox sat there, alive, for about 18 minutes. Not a single extra token was spent — only seconds of runtime, piling up.

What I want to share here is a budgeting approach that starts from one premise: a Managed Agents bill moves along two axes — tokens and runtime. Watch only one, and the other erodes your budget in silence.

Managed Agents bills along two axes

Start by separating what you are actually paying for. A single Managed Agents call spins up an isolated, Google-hosted Linux sandbox and runs reasoning, tool calls, code execution, and file operations inside it. The convenience comes with a second thing to meter.

AxisDriven byWhere it balloonsCaught by usual monitoring?
Token costInput / output / thinking tokensLong context, verbose retriesYes — your existing cost tooling sees it
Sandbox runtimeWall-clock seconds the sandbox was aliveHangs on tool waits, idling, over-parallelismEasy to miss

Your instincts from past API work carry over fine to the token side. The runtime side is the problem. An agent that is "thinking and stuck," "waiting on an external API," or "done but with a sandbox that never got torn down" accrues seconds while spending zero tokens.

In my case, one hung run kept a sandbox alive for 18 minutes, while the healthy runs that same morning averaged around 40 seconds. A single missed teardown turned into the runtime of 27 normal runs. The scarier part was not the money — it was realizing I had never been watching this axis at all.

The budget boundary starts at a per-run wall-clock cap

The first boundary to draw is the maximum wall-clock seconds a single run is allowed to stay alive. Not a token cap — a time cap.

The idea is simple. Launch the run asynchronously, put a deadline on the whole thing with asyncio.wait_for, and explicitly tear the sandbox down if it overruns. Even when it hangs, it is guaranteed to fold at the cap.

import asyncio
import time
from dataclasses import dataclass
 
@dataclass
class RuntimeBudget:
    max_wall_seconds: float = 180.0   # ceiling on how long one run may live
    idle_seconds: float = 45.0        # grace period once progress stalls
    max_concurrent: int = 2           # ceiling on simultaneous live sandboxes
 
class RuntimeBudgetError(Exception):
    """Raised when a wall-clock or idle ceiling is exceeded."""
 
async def run_with_wall_clock(client, *, model, instruction, budget):
    """Put a wall-clock deadline on a single Managed Agents run.
 
    client.agents reflects the public-preview surface; map
    create_run / poll_run / cancel_run to your SDK's names.
    """
    started = time.monotonic()
    run = await client.agents.create_run(model=model, instruction=instruction)
    try:
        result = await asyncio.wait_for(
            _poll_until_done(client, run.id, budget),
            timeout=budget.max_wall_seconds,
        )
        return result, time.monotonic() - started
    except asyncio.TimeoutError:
        # Skip this and the sandbox keeps billing seconds in silence
        await client.agents.cancel_run(run.id)
        raise RuntimeBudgetError(
            f"run {run.id} exceeded wall-clock cap {budget.max_wall_seconds}s"
        )

The single most important line here is cancel_run in the except block. When the deadline trips wait_for, the sandbox itself keeps living quietly. Only an explicit teardown stops the clock. That one line is exactly what I missed the first time.

✦

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
✦If your Managed Agents bill never matches your token estimate, you will learn to separate out the second billing axis — sandbox wall-clock runtime — and pinpoint exactly where the cost is leaking
✦You get a per-run wall-clock cap, automatic idle teardown of the sandbox, and a concurrency ceiling, all as runnable asyncio Python you can drop in today
✦A per-stage JSONL runtime ledger lets you say, in numbers and before the invoice arrives, which automation is eating your time budget
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-23
When to Hand Conversation State to the Server: previous_interaction_id vs. Pruning History Yourself
With the Interactions API, passing previous_interaction_id lets the server hold conversation state so you stop resending history. But a large tool output that lands mid-conversation can't be pruned afterward, and every later turn drags its weight. Here is a branching design that mixes server-side state with client-side pruning, plus a thin, working Python wrapper.
◈ API / SDK2026-07-15
Sample what you already accepted — an audit budget that catches silent quality drift
A confidence gate only ever looks at output the model hesitated on. Silent drift sinks into the batch that sailed through. Working from a fixed 30-minutes-a-day review budget, this walks through deriving detection time from the binomial, reallocating the same budget across risk strata, and catching slow decay with a cumulative monitor.
◈ API / SDK2026-07-04
When Gemini API Leaks Japanese Into Your English Output Once in a While — Field Notes on Measuring the Contamination Rate and Tightening It in Stages
You told Gemini to answer in English, and 3 out of 100 runs slip a Japanese sentence into the tail. Here is why you cannot stop that 'once in a while', and a production pattern that measures the contamination rate as an SLO and tightens it with graded recovery, with working code.