If you have ever tried to run an AI agent that executes real code, you know the biggest headache: security. Letting an LLM write and run custom scripts on the fly is powerful, but running untrusted code on your system can be risky.
As part of a recent hands-on project, I built a personal AI assistant hosted on Google Cloud Run that solves this problem using Cloud Run sandboxes. Here is how it works, what I built, and what I learned along the way.
The Scenario: Managing the Graduation Rush
To make things practical, the project centers on a real-world problem: a coffee shop manager in a college town preparing for a massive graduation weekend.
The assistant's job is to analyze raw Point-of-Sale (POS) data stored in Google Sheets—looking at metrics like drink complexity, pastry sales, and cashier wait times—and cross-reference it with the ceremony schedule to find operational bottlenecks.
How the Tech Works
The assistant isn't just generating text; it dynamically writes Python scripts to process data and update operational tasks.
Cloud Run Sandboxes: Instead of running generated code directly on the host server, the app uses Cloud Run’s isolated sandbox environment (/usr/local/gcp/bin/sandbox). This gives the agent a secure, lightning-fast playground to run Python scripts safely.
Local vs. Cloud Execution: To keep development seamless, the application automatically detects its environment. It runs scripts directly on the terminal during local testing, and switches to the containerized sandbox binary once deployed.
Google Agent Development Kit (ADK) & Gemini: Built using FastAPI and google-adk, the agent interfaces with Gemini on Vertex AI to generate insights and ask for approval before writing TODOs back to Google Sheets.
Live Chat UI: Built with WebSockets, allowing real-time interaction directly with the background agent.
Setting Up Access & Permissions
Security was a major focus. Rather than giving open access, I set up a dedicated service account (coffee-shop-agent-sa).
- IAM Roles: Granted roles/aiplatform.user so the agent could interact with Gemini APIs.
- Local Impersonation: Used roles/iam.serviceAccountTokenCreator to allow local testing through service account impersonation without leaking static credentials.
- Sheet Integration: Shared the target Google Sheet directly with the service account email as an Editor.
Key Takeaways
- Sandboxing is Essential for Code Execution: Letting an AI agent run code in production sounds scary, but isolated sandboxes make it practical and secure.
- Human-in-the-Loop Design: Having the agent ping the user for permission before modifying live spreadsheet data keeps things under control.
Top comments (0)