Build and deploy agents with Claude Agent SDK on Cloud Run

The Claude Agent SDK lets you build autonomous AI agents in Python or TypeScript with the same agent loop, context management, and tool-use capabilities that power Claude Code. You can host these agents on Cloud Run services or instances, call Anthropic's Claude models as managed APIs on Gemini Enterprise Agent Platform, and isolate untrusted code execution in Cloud Run sandboxes.

Deployed workloads authenticate to Agent Platform with a service identity and Application Default Credentials (ADC), so you don't need to manage Anthropic API keys. You pay for Claude usage through your Google Cloud project, and Cloud Run services scale container instances automatically with traffic, including down to zero. You can also secure access and audit model traffic with Identity and Access Management (IAM), Identity-Aware Proxy (IAP), and Agent Platform request-response logging.

Build an agent with the Claude Agent SDK and Cloud Run sandboxes

For an example of deploying autonomous agents with the Claude Agent SDK on Cloud Run, see the Claude Agent SDK on Cloud Run codelab.

When your agent executes untrusted shell commands or model-generated code, you can combine the Claude Agent SDK with Cloud Run sandboxes (Preview). The host container runs your FastAPI web server and the Claude Agent SDK. To run untrusted code safely, the agent dispatches commands through an in-process Model Context Protocol (MCP) tool into an isolated gVisor sandbox (/usr/local/gcp/bin/sandbox do --template) in a dedicated sandbox template container image that can't access the host file system, environment variables or service account credentials.

Best practices

When you run agents with Claude in production on Cloud Run, follow these best practices:

  • Use sandbox templates for agent code execution: Omitting --template runs sandbox do in local mode, which mounts the host container's root file system as read-only and exposes files outside /tmp (such as /app source files, Secret Manager volume mounts, or Cloud Storage FUSE shares). Keep sandbox templates and SANDBOX_TEMPLATE configured so untrusted code runs in a dedicated container image with no access to the host file system.
  • Persist sessions: By default, the Claude Agent SDK writes session transcripts to the local container file system, which isn't shared across Cloud Run service instances and is lost when an instance scales down. For multi-turn conversations on Cloud Run services, implement a custom SessionStore backed by external storage such as Cloud Storage, Cloud SQL or Firestore.
  • Plan for model quotas: Claude requests count against per-model quotas. Retry 429 errors with exponential backoff, and request additional quota before launch if needed. For more information, see Quotas for Anthropic Claude models.
  • Log model traffic: to audit prompts and responses, turn on request-response logging in Agent Platform. Anthropic recommends retaining at least 30 days of logs so that you can investigate potential misuse.

What's next