Back to library
AI
best practice

Ensure Numerical Accuracy with Internal Code Execution

Activate built-in code execution for error-free mathematical and data processing tasks.

Enable the Code Execution tool in Gemini to let the model write and run code to solve complex reasoning and math problems with 100% accuracy.

Google Gemini

The Scenario

You have a messy list of financial transactions and need an exact calculation of the weighted average and statistical outliers.

Before & after

The old way

A user would manually open Excel, write a formula, or write a Python script locally to calculate complex proportions, taking 10–15 minutes.

With AI

By enabling the 'Code Execution' tool, Gemini writes and runs the script internally, providing the verified result in 30 seconds.

The Prompt

I have a list of [NUMBER] transactions: [PASTE_LIST]. Please calculate the weighted average of the transaction amounts and identify any outliers more than 2 standard deviations from the mean.

The Gemini API provides a managed Code Execution tool. This allows the model to solve math problems or process data by running code in a sandboxed environment rather than just predicting the next word.

Source

Release notes  |  Gemini API  |  Google AI for Developers
"Models can use tools in both standard request-response interactions and real-time streaming sessions using the Live API."