AI assistant in the Earth Engine Code Editor

The Ask panel in the Earth Engine Code Editor uses Gemini to assist you with writing, understanding, debugging, and optimizing your scripts. It acts as a conversational AI assistant integrated directly into the Code Editor to streamline your geospatial analysis and script development.

Interface and tools

The AI assistant panel is located in the right-hand pane of the Earth Engine Code Editor. To open it, click the Ask tab next to the Inspector, Console, and Tasks tabs.

The Ask pane includes several controls and tools to help you manage your interaction with the AI.

Buttons and controls

  • Send / Stop: sends your prompt to the assistant. The button toggles to a stop icon while generation is in progress, which lets you cancel the request. You can also use the enter / return key to send.
  • New chat: clears the conversation history and starts a fresh session.
  • Help: opens a dialog with information about the feature, billing, and terms.
  • Model selection: a drop-down menu that lets you choose which Gemini model to use. Some models may not work depending on the tier of your API key. Your selection will be saved for your next session.
  • Tool toggles: a menu that lets you enable or disable specific tools for the assistant to use. Your selection will be saved for your next session.
  • Copy (code blocks): copies the generated code to your clipboard.
  • Insert (code blocks): inserts the generated code into your script editor. If the editor is not empty, it shows a difference dialog that highlights changes for your review.
  • Regenerate: a retry button appears at the bottom of the last response to re-run the prompt.
  • Token counter: displays the number of tokens used in the current conversation session. Use this to monitor your usage against your API key limits.

Tool options

You can enable the following tools to improve the assistant's responses. Note that enabling tools adds extra context (such as documentation or search results) to your requests, which may result in greater token usage.

  • Docs: searches over Earth Engine public documentation to provide accurate code and explanations.
  • Dataset search: searches datasets in the Earth Engine Data Catalog.
  • Google Search: grounds responses in Google Search results to provide up-to-date information from the web. Note that the Google Search tool cannot be used simultaneously with Docs or Dataset search tool. Enabling Google Search will automatically make the other tools unavailable in the UI.

Prerequisites

To make requests to the model, this feature uses your own Gemini API key.

How to get a Gemini API key

You can create or view Gemini API keys under your Cloud Project in Google AI Studio.

  1. Go to Google AI Studio API Keys.
  2. Click Create API key and select an option to create your key.

For more information on Gemini API keys, see the Gemini API key documentation.

Configure your API key in the Code Editor

Once you have your API key, you must add it to the Code Editor:

  1. In the Ask panel, click the key icon in the chat input box.
  2. Paste your API key into the dialog that appears.
  3. Optionally check the box to save the key for future sessions and click OK.

Automatic context

To provide relevant assistance, the Ask feature automatically includes the following context with every request:

  • The full text of your current script in the editor.
  • Any imported assets and geometries.
  • The chat history of the current session.

Troubleshooting console errors

The AI assistant is integrated directly with the Code Editor's console to help you quickly resolve run-time errors:

  1. If your script encounters an error, look for the red error card in the Console tab.
  2. Click the Troubleshoot button on the error card.
  3. This automatically opens the Ask panel and queues up an auto-generated prompt message containing the error message and relevant context from your script for you to send.

Example prompts

You can ask the assistant to help with a variety of tasks by typing directly into the chat box. Here are some examples of what you can ask, categorized by task:

Code generation and workflows

  • "Write a script to load Sentinel-2 imagery for the year 2023 over East Africa, filter for cloud cover less than 10%, calculate the median NDVI, and add it to the map with a white-to-green visualization palette."
  • "Create a cloud-free Landsat 9 composite for summer 2024 in Central Europe and export it to my cloud storage bucket."

Earth Engine Apps

  • "Write code to create an Earth Engine App split-panel map to compare imagery from 2020 and 2024 side-by-side."
  • "Add a panel on the left with a drop-down menu to select a year and a button to refresh the map."
  • "Add a custom legend to the map for the displayed land cover classification."

Debugging

  • "I'm getting a 'User memory limit exceeded' error. How can I resolve this?"
  • "My map displays a layer with no variance in pixel values, this is not what I expected, what is going wrong?"
  • "Why am I getting the error 'Image.select: Band 'NDVI' not found' when running this calculation?"

Explanation

  • "Explain what ee.Reducer.mean().combine() is doing on line 15 of my script."
  • "What is the difference between ee.Image.clip() and ee.Image.mask()?"

Optimization

  • "Can you optimize this image collection map operation to run more efficiently?"
  • "How can I rewrite this reduceRegions operation over many polygons and images to avoid timing out?"

Billing and costs

Using this feature requires your own Gemini API key. Costs are subject to Gemini API pricing and your key's quota tier.

  • Free Tier: The Gemini API offers a free tier (with limits).
  • Paid Tier: If you use a paid tier API key, it will incur charges regardless of your specific Earth Engine project registration.

For more details, see Gemini API Pricing.

Monitor usage and control costs

You can manage keys and view usage and costs directly in AI Studio.

  • Check your API key's tier, usage, limits, and spend in the AI Studio interface.
  • Set a monthly spend cap in the Spend tab in AI Studio.

Data and privacy

Your data is handled in accordance with the Gemini API Terms of Service. Be sure to review them regarding the use of prompts and generated content.

Feedback

Help us improve! Share your feedback anytime by clicking the feedback button in the upper right corner of the Code Editor. Use the Send Code Editor feedback option. We actively read this feedback to improve model performance and prioritize features.