
Modal Tutorial: Turn a Python Function Into a Cloud GPU Endpoint
Also available in Hindi: यह लेख हिंदी में पढ़ें
You have a Python model script that needs a GPU, and no GPU. Modal runs that function in a cloud container with the GPU you name in code, bills by the second, and can expose it as a web URL. No Docker files or Kubernetes needed.
Set up
pip install modal
modal setup
modal setup signs you in (if it fails, try python -m modal setup). One rule before you start: using a GPU requires a valid payment method on your Modal account.
Write the endpoint
import modal
app = modal.App("gpu-check")
image = modal.Image.debian_slim().uv_pip_install("fastapi[standard]", "torch")
@app.function(gpu="T4", image=image, scaledown_window=120)
@modal.fastapi_endpoint(method="POST")
def gpu_info(item: dict):
import torch
return {"cuda": torch.cuda.is_available(),
"gpu": torch.cuda.get_device_name(0),
"you_sent": item}
The image is your container, defined in Python. gpu="T4" picks the card; you can also ask for "L4", "A10", "L40S", "A100", "H100" and newer chips, or several with "H100:2". For running (not training) models, Modal recommends starting with the L40S, which has 48 GB of GPU memory. @modal.fastapi_endpoint wraps the function in FastAPI, so the image must include FastAPI.
Run, serve, deploy
- modal run app.py starts a temporary app for a one-off job and stops when your script exits.
- modal serve app.py gives you a temporary public URL that reloads when you edit the file. Press Ctrl-C to stop it.
- modal deploy app.py creates a persistent URL that stays up until you stop the app.
Test it with curl -X POST -H 'Content-Type: application/json' -d '{"x": 1}' <your URL>.
Control cost and cold starts
A deployed function with no requests runs no containers, so you aren't charged for compute. The first request boots a container, which can take a few seconds. Idle containers stay warm for 60 seconds by default; scaledown_window changes that to anything from 2 seconds to 20 minutes. min_containers keeps some always on, which removes cold starts but bills the whole time.
Prices are per second: a T4 is $0.000164 (about $0.59 an hour) and an H100 is $0.001097 (about $3.95 an hour). The free Starter plan includes $30 of credits each month. Store large model weights in a Modal Volume instead of downloading them on every cold start.
Compare other options in AI agent hosting options, free hosting for side projects and how to deploy a vibe-coded app, or visit the Modal tool page.
Sources
- Introduction, Modal docs
- GPU acceleration, Modal docs
- Web Functions, Modal docs
- Apps, Functions, and entrypoints and Cold start performance, Modal docs
- Pricing, Modal
- modal-client, GitHub and modal package, PyPI
FAQ
Is Modal free?
The Starter plan has no monthly fee and includes $30 of compute credits a month.
Do I need Docker?
No. You define the container image in Python with modal.Image.
What is the difference between modal serve and modal deploy?
Serve gives a temporary, live-reloading URL; deploy gives a persistent one.
Why is the first request slow?
It starts a container. Use scaledown_window or min_containers to keep one warm.