Agent Sandboxesagentsandboxes.org
Sandbox profile · Cloud or model provider service

Gemini API code execution

Answer in brief

Built-in Gemini API tool that lets the model write and run Python in a Google-hosted environment at no charge beyond tokens. A free, bounded Python scratchpad for Gemini calls; reach for a real sandbox when you need more than 30 seconds, state or network.

By the Agent Sandboxes editors · Updated

Overview

Code execution is a built-in tool of the Gemini API (the documentation now lives under AI Studio at aistudio.google.com/docs/code-execution). Passing tools=[{"type": "code_execution"}] lets the model generate Python, run it in a Google-hosted environment and iterate on the result; if the code errors the model may regenerate it up to five times. The environment ships a fixed library set including numpy, pandas, matplotlib, scipy, scikit-learn, sympy, opencv-python, pillow, tensorflow and seaborn, and you cannot install your own. Python is the only language that executes, graphs render with matplotlib only, and the maximum runtime of the environment is 30 seconds.

Input files can be supplied inline or through the Files API (the docs say it works best with text and CSV files) and output files are returned as inline data. Multi-turn use goes through previous_interaction_id; the docs do not say whether interpreter state persists between turns, and do not mention internet access or the isolation technology. Code execution costs nothing beyond the model's standard token rates and can be combined with Grounding with Google Search and, on Gemini 3 and Gemini 4 models, with custom function calling. This is the Gemini API tool; Google's separate Agent Platform code execution service is covered by the google-code-execution record.

Notable

  • No additional charge: the docs state "There's no additional charge for enabling code execution"
  • Hard limits documented: Python only, 30-second maximum runtime, fixed library list, up to five automatic retries on error
  • File input and graph output are documented as available since Gemini 2.0 Flash; image zoom, crop and annotation via code execution is documented for Gemini 3 Flash with Thinking enabled

Gemini API code execution specifications

AttributeDetails
IsolationNot documented; Google describes a hosted Python environment with a fixed library set, a 30-second runtime limit and no package installation, but does not name the sandboxing technology
DeploymentCloud or model provider service
Open sourceNo
SDKs and interfacesPython, JavaScript/TypeScript, Go, Java, REST API
Cold start (vendor claim)Not documented
Max session lengthEach code execution has a maximum runtime of 30 seconds
PersistenceNone documented; executions run inside a model turn, with multi-turn use via previous_interaction_id, and output files are returned as inline data
GPUNot documented
NetworkingNot documented; the docs say you cannot install your own libraries
PricingNo additional charge for enabling code execution; billed at the model's standard token rates (the prompt as input, generated code, execution results and final summary as output, intermediate tokens as input)
Free tierNo additional charge beyond model tokens
ComplianceNot documented
RegionsNot documented

Facts from Gemini API code execution’s public documentation, verified November 9, 2026. Performance numbers are vendor claims.

Strengths and trade-offs

Strengths

  • You want Gemini to check its own maths or produce a chart without paying for or operating a sandbox
  • You need a zero-setup option inside an existing Gemini API integration
  • You are processing small CSV or text inputs that fit in the model's context window

Trade-offs

  • 30-second maximum runtime and Python only, so it is not suitable for builds, long scripts or other languages
  • No custom packages, no documented filesystem persistence and no documented network access
  • Isolation technology is not documented, so it cannot be assessed for sensitive workloads
  • The docs warn that enabling code execution can cause regressions in other outputs, such as creative writing

Good fit for: Calculation, data analysis and chart generation inside a Gemini API call with no sandbox to operate.

Use cases Gemini API code execution fits

  • Code interpreters: The Gemini API tool that runs Python for calculation, analysis and charts inside the model call; the isolation technology is not documented.

How Gemini API code execution compares

SandboxIsolationOpen sourceSelf-host / BYOCCold start (vendor)Max sessionSnapshots / pauseGPUSDKs
Gemini API code executionHosted Python runtimeNoNo / NoNot documented30 s per executionNo / NoNoPython, JavaScript/TypeScript, Go, Java, REST API
Google Agent Platform Code ExecutiongVisorNoNo / No<1 s (warm)14-day stateNo / Not documentedNoPython (Agent Platform SDK), REST API
OpenAI Code Interpreter & Hosted ShellVM (unspecified)NoNo / NoNot documented20 min idleNo / NoNoOpenAI Responses API, Containers API, OpenAI SDKs
Claude code execution toolHosted Linux containerNoNo / NoNot documented30 days (container)No / Not documentedNoPython, JavaScript/TypeScript, Go, Java, C#, PHP, Ruby, REST API, CLI
Mistral code interpreterIsolated containerNoNo / NoNot documentedNot documentedNot documented / Not documentedNot documentedPython, JavaScript/TypeScript, REST API
Azure Container Apps dynamic sessionsHyper-V VMNoNo / NoSub-second (pooled)220 s / execNo / NoNoREST API, Azure CLI, LangChain, LlamaIndex, Semantic Kernel

See all Gemini API code execution alternatives →

Frequently asked questions

What is Gemini API code execution?
Built-in Gemini API tool that lets the model write and run Python in a Google-hosted environment at no charge beyond tokens. A free, bounded Python scratchpad for Gemini calls; reach for a real sandbox when you need more than 30 seconds, state or network.
How does Gemini API code execution isolate code?
Gemini API code execution documents its isolation as: Not documented; Google describes a hosted Python environment with a fixed library set, a 30-second runtime limit and no package installation, but does not name the sandboxing technology.
Is Gemini API code execution open source?
No, Gemini API code execution is not open source.
How much does Gemini API code execution cost?
No additional charge for enabling code execution; billed at the model's standard token rates (the prompt as input, generated code, execution results and final summary as output, intermediate tokens as input) Check the official pricing page for current rates.
What are the alternatives to Gemini API code execution?

Sources

  1. aistudio.google.com/docs/code-execution
  2. ai.google.dev/gemini-api/docs/code-execution

Cite this page

Agent Sandboxes. (2026). Gemini API code execution. agentsandboxes.org. Last reviewed November 9, 2026. https://agentsandboxes.org/sandboxes/gemini-code-execution/

Found an error or a missing sandbox? Submit a correction — every entry links to the sources it relies on.