Build Stateful Agentic Apps with the Gemini Interactions API

80 min remaining
Step 3 of 12•10 min

3. Set Up Your Environment

You will work entirely in Google Cloud Shell, a free browser-based Linux terminal with Python, the google-genai SDK and an editor already installed. Nothing needs to be installed on your machine.

Launch Cloud Shell

  1. Open the Google Cloud Console.
  2. Select your project in the top-left dropdown.
  3. Click the Activate Cloud Shell icon (>_) in the top-right toolbar. If you are asked to authorise the terminal, click Authorize.

Update the system packages

The google-genai SDK is pre-installed in Cloud Shell. Update the system packages so you are on the latest release (this can take a few minutes):

Terminal
❯sudo apt-get update && sudo apt-get upgrade -y
Optional: Python Virtual Environment

If you are running locally or want to ensure an isolated Python environment with google-genai >= 2.3.0 and pydantic, you can create and activate a virtual environment inside your project directory: python3 -m venv .venv && source .venv/bin/activate && .venv/bin/pip install -U "google-genai>=2.3.0" pydantic

Create a Gemini API key

  1. Open Google AI Studio in a new browser tab and sign in.
  2. Click Create API key and copy the key.
  3. Back in Cloud Shell, export the key. The SDK reads GEMINI_API_KEY automatically:
Terminal
❯export GEMINI_API_KEY="your_api_key_here"
Session Persistence

Environment variables are cleared when Cloud Shell restarts or when you open a new tab. If you see an authentication error later, run the export command again.

Project Structure

Create a folder for the project and the files you will fill in:

Terminal
❯mkdir -p ~/orbital7-agent && cd ~/orbital7-agent
❯touch simulator.py tools.py agent.py main.py resume.py branch.py
  • simulator.py: The station. It holds the true state, the hidden faults and a verdict() method that reports ground truth. In production, this is your infrastructure.
  • tools.py: The tool declarations the model sees, and a dispatcher that runs them and turns failures into results the model can read.
  • agent.py: The model configuration, the orchestration loop and the structured incident report.
  • main.py: Runs an incident from start to finish and saves the session to session.json.
  • resume.py: A separate process that continues a stored session using only its ID.
  • branch.py: Rewinds a session to an earlier turn and replays it with a different outcome.

To edit the files, click Open Editor in the Cloud Shell toolbar and open the orbital7-agent folder.

Environment Setup Checklist
0/3 Completed