This document shows you how to build and deploy a smolagents application to Cloud Run.
By following the steps in this quickstart, Cloud Run automatically builds a Dockerfile for you when you deploy from source code.
Install the Google Cloud CLI.
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
To initialize the gcloud CLI, run the following command:
gcloud initCreate or select a Google Cloud project.
Roles required to select or create a project
roles/resourcemanager.projectCreator), which contains the
resourcemanager.projects.create permission. Learn how to grant
roles.
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace PROJECT_ID with a name for the Google Cloud project you are creating.
Select the Google Cloud project that you created:
gcloud config set project PROJECT_ID
Replace PROJECT_ID with your Google Cloud project name.
If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.
Verify that billing is enabled for your Google Cloud project.
Install the Google Cloud CLI.
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
To initialize the gcloud CLI, run the following command:
gcloud initCreate or select a Google Cloud project.
Roles required to select or create a project
roles/resourcemanager.projectCreator), which contains the
resourcemanager.projects.create permission. Learn how to grant
roles.
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace PROJECT_ID with a name for the Google Cloud project you are creating.
Select the Google Cloud project that you created:
gcloud config set project PROJECT_ID
Replace PROJECT_ID with your Google Cloud project name.
If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.
Verify that billing is enabled for your Google Cloud project.
If you are under a domain restriction organization policy restricting unauthenticated invocations for your project, you will need to access your deployed service as described under Testing private services.
Enable the Cloud Run Admin API and Cloud Build APIs:
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM
role (roles/serviceusage.serviceUsageAdmin), which contains the
serviceusage.services.enable permission. Learn how to grant
roles.
gcloud services enable run.googleapis.comcloudbuild.googleapis.com
After the Cloud Run Admin API is enabled, the Compute Engine default service account is automatically created.
To get the permissions that you need to complete this quickstart, ask your administrator to grant you the following IAM roles:
roles/run.admin)
on the projectroles/run.sourceDeveloper)
on the projectroles/iam.serviceAccountUser)
on the service identityroles/logging.viewer)
on the projectFor more information about granting roles, see Manage access to projects, folders, and organizations.
You might also be able to get the required permissions through custom roles or other predefined roles.
Cloud Build automatically uses the Compute Engine default service account as the default Cloud Build service account to build your source code and Cloud Run resource, unless you override this behavior.
For Cloud Build to build your sources, grant the Cloud Build service
account the Cloud Run
Builder
(roles/run.builder) role on your project:
gcloud projects add-iam-policy-binding PROJECT_ID \ --member=serviceAccount:SERVICE_ACCOUNT_EMAIL_ADDRESS \ --role=roles/run.builder
Replace PROJECT_ID with your Google Cloud
project ID and SERVICE_ACCOUNT_EMAIL_ADDRESS with the
email address of the Cloud Build service account. If you're using the
Compute Engine default service account as the Cloud Build service account, then
use the following format for the service account email address:
PROJECT_NUMBER-compute@developer.gserviceaccount.com
Replace PROJECT_NUMBER with your Google Cloud
project number.
For detailed instructions on how to find your project ID, and project number, see Creating and managing projects.
Granting the Cloud Run builder role takes a couple of minutes to propagate.
To write an application in Python:
Create a new directory named my-agent-app and change directory
into it:
mkdir my-agent-app
cd my-agent-app
Create a file named main.py and paste the following code into it:
import os
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from smolagents import CodeAgent, LiteLLMModel, tool
# 1. Define a simple tool for the agent
@tool
def get_greeting(name: str) -> str:
"""
Returns a special greeting for the user.
Args:
name: The name of the person to greet.
"""
return f"Hello {name}, welcome to the agentic world running on Cloud Run!"
# 2. Initialize the Gemini Model via LiteLLM
# Make sure GEMINI_API_KEY is set in your environment variables
model = LiteLLMModel(
model_id="gemini/gemini-2.5-flash", # This is the model name. If a newer model is available, you can use that.
api_key=os.environ.get("GEMINI_API_KEY")
)
# 3. Create the CodeAgent
agent = CodeAgent(
tools=[get_greeting],
model=model,
add_base_tools=True # Adds basic python tools like print
)
# 4. Setup FastAPI
app = FastAPI()
class AgentRequest(BaseModel):
task: str
@app.get("/")
def health_check():
return {"status": "running", "service": "smolagents-fastapi"}
@app.post("/run")
def run_agent(request: AgentRequest):
try:
# Run the agent with the user's task
response = agent.run(request.task)
return {"response": str(response)}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
Create a file named requirements.txt and paste the following code into it:
fastapi
uvicorn
smolagents[toolkit]
litellm
Deploy from source automatically builds a container image from source code and deploys it.
To deploy from source using the following command:
gcloud run deploy smolagents-demo \
--source . \
--region us-central1 \
--no-allow-unauthenticated \
--set-env-vars GEMINI_API_KEY=API_KEY
Replace API_KEY with your Google AI Studio API key.
Cloud Run displays the service URL, such as https://smolagents-demo-xyz-uc.a.run.app,
after you successfully deploy the app.
Test your service by sending a task to the agent using the following curl command:
curl -X POST YOUR-SERVICE-URL/run \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $(gcloud auth print-identity-token)" \
-d '{"task": "Use the greeting tool to say hello to Gemini User"}'
Replace YOUR-SERVICE-URL with your service's URL.
The agent sends the following response:
{
"response": "Hello Gemini User, welcome to the agentic world running on Cloud Run!"
}
To verify your service is working correctly, check
the Logs tab of the Cloud Run service.
To avoid incurring charges to your Google Cloud account for
the resources used on this page, delete the Google Cloud project with the
resources.
To avoid additional charges to your Google Cloud account, delete all the resources
you deployed with this quickstart. Cloud Run doesn't charge you when your deployed service isn't in use.
However, you might still be charged for storing the container image in
Artifact Registry. To delete Artifact Registry repositories,
follow the steps in Delete
repositories in the Artifact Registry
documentation. Cloud Run services don't incur costs until they receive requests.
To delete your Cloud Run service, follow one of these steps: To delete a service: In the Google Cloud console, go to the Cloud Run Services page: Locate the service you want to delete in the services list, and click
its checkbox to select it. Click Delete. This deletes all revisions of the service. To delete a service, run the following command: Replace the following: Deleting your Google Cloud project stops billing for all resources in that
project. To release all Google Cloud resources in your project, follow these steps: Delete a Google Cloud project: For more information on building a container from code source and pushing to
a repository, see:Clean up
Delete your repository
Delete your service
Console
gcloud
gcloud run services delete SERVICE --region REGION
Delete your test project
gcloud projects delete PROJECT_ID
What's next
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2026-06-09 UTC.