Close-up of a red and black graphics card, the kind of GPU Modal rents per second for Python functions
Photo by Rohan on Unsplash · source · credits

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 workflow: setup, modal run, modal serve, modal deploy
From Modal docs (Introduction, Web Functions, Apps). Illustration, not a screenshot.

Test it with curl -X POST -H 'Content-Type: application/json' -d '{"x": 1}' <your URL>.

Control cost and cold starts

Modal GPU prices per second and per hour
From modal.com/pricing. Illustration, not a screenshot.

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

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.

About the author

Suraj Kumar builds and edits AIUseFlow, a directory of AI tools for vibe coders, agent builders and creators. Guides like this one walk through a practical workflow with cited sources — not a news wire rewrite. How we rank tools.

Get the weekly stack

Rank movers and new AI tools for builders, once a week. Free, no spam, unsubscribe anytime.