This guide walks you through hosting a Swarms agent on Google Cloud Run — a fully managed container platform that auto-scales from zero. The setup uses a slim Docker image, an api/api.py Flask endpoint, and gcloud for deployment.
Project structure
.
├── Dockerfile
├── requirements.txt
└── api/
└── api.py
Step 1: Prerequisites
- Google Cloud account — sign in at console.cloud.google.com and enable billing on a project.
- gcloud SDK — install via the official guide.
- Docker — install via the Docker docs.
- Project — create a new Google Cloud project and note the Project ID.
- APIs — enable the following in the API Library:
- Cloud Run API
- Cloud Build API
- Artifact Registry API
Step 2: Build the agent
api/api.py
from flask import Flask, request, jsonify
from swarms import Agent
app = Flask(__name__)
agent = Agent(
agent_name="Stock-Analysis-Agent",
model_name="gpt-5.4",
max_loops="auto",
interactive=True,
streaming_on=True,
)
@app.route("/run-agent", methods=["POST"])
def run_agent():
data = request.json
task = data.get("task", "")
result = agent.run(task)
return jsonify({"result": result})
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8080)
requirements.txt
Dockerfile
FROM python:3.10-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY api/ ./api/
EXPOSE 8080
CMD ["python", "api/api.py"]
Cloud Run sends traffic to port 8080 by default — keep that as the listening port.
Step 3: Authenticate
gcloud auth login
gcloud config set project [PROJECT_ID]
Step 4: Push the image to Artifact Registry
# 1. Create the registry
gcloud artifacts repositories create my-repo \
--repository-format=Docker \
--location=us-central1
# 2. Authenticate Docker against the registry
gcloud auth configure-docker us-central1-docker.pkg.dev
# 3. Build and tag the image
docker build -t us-central1-docker.pkg.dev/[PROJECT_ID]/my-repo/my-image .
# 4. Push it
docker push us-central1-docker.pkg.dev/[PROJECT_ID]/my-repo/my-image
Step 5: Deploy to Cloud Run
gcloud run deploy my-agent-service \
--image us-central1-docker.pkg.dev/[PROJECT_ID]/my-repo/my-image \
--platform managed \
--region us-central1 \
--allow-unauthenticated
--allow-unauthenticated makes the service publicly reachable. Drop it to require IAM-authenticated calls.
Step 6: Test the deployment
Cloud Run prints a URL after deploy. Hit the endpoint:
curl -X POST [CLOUD_RUN_URL]/run-agent \
-H "Content-Type: application/json" \
-d '{"task": "Summarise the latest performance of AAPL"}'
Step 7: Update the service
Iterating is just rebuild → push → redeploy:
docker build -t us-central1-docker.pkg.dev/[PROJECT_ID]/my-repo/my-image .
docker push us-central1-docker.pkg.dev/[PROJECT_ID]/my-repo/my-image
gcloud run deploy my-agent-service \
--image us-central1-docker.pkg.dev/[PROJECT_ID]/my-repo/my-image
Troubleshooting
-
Permission errors — your account needs the Cloud Run Admin and Artifact Registry Reader roles.
-
Port issues — Cloud Run expects port
8080. Make sure your Flask app binds to it.
-
Logs:
gcloud logs read --project [PROJECT_ID]
See also