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.
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
| Attribute | Details |
|---|---|
| Isolation | 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 |
| Deployment | Cloud or model provider service |
| Open source | No |
| SDKs and interfaces | Python, JavaScript/TypeScript, Go, Java, REST API |
| Cold start (vendor claim) | Not documented |
| Max session length | Each code execution has a maximum runtime of 30 seconds |
| Persistence | None documented; executions run inside a model turn, with multi-turn use via previous_interaction_id, and output files are returned as inline data |
| GPU | Not documented |
| Networking | Not documented; the docs say you cannot install your own libraries |
| Pricing | 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) |
| Free tier | No additional charge beyond model tokens |
| Compliance | Not documented |
| Regions | Not 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
| Sandbox | Isolation | Open source | Self-host / BYOC | Cold start (vendor) | Max session | Snapshots / pause | GPU | SDKs |
|---|---|---|---|---|---|---|---|---|
| Gemini API code execution | Hosted Python runtime | No | No / No | Not documented | 30 s per execution | No / No | No | Python, JavaScript/TypeScript, Go, Java, REST API |
| Google Agent Platform Code Execution | gVisor | No | No / No | <1 s (warm) | 14-day state | No / Not documented | No | Python (Agent Platform SDK), REST API |
| OpenAI Code Interpreter & Hosted Shell | VM (unspecified) | No | No / No | Not documented | 20 min idle | No / No | No | OpenAI Responses API, Containers API, OpenAI SDKs |
| Claude code execution tool | Hosted Linux container | No | No / No | Not documented | 30 days (container) | No / Not documented | No | Python, JavaScript/TypeScript, Go, Java, C#, PHP, Ruby, REST API, CLI |
| Mistral code interpreter | Isolated container | No | No / No | Not documented | Not documented | Not documented / Not documented | Not documented | Python, JavaScript/TypeScript, REST API |
| Azure Container Apps dynamic sessions | Hyper-V VM | No | No / No | Sub-second (pooled) | 220 s / exec | No / No | No | REST API, Azure CLI, LangChain, LlamaIndex, Semantic Kernel |
Frequently asked questions
What is Gemini API code execution?
How does Gemini API code execution isolate code?
Is Gemini API code execution open source?
How much does Gemini API code execution cost?
What are the alternatives to Gemini API code execution?
Sources
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.