- Published on
What is Modal.com? How to Deploy AI Models in Seconds
Modal.com is a cloud platform that lets you run code in the cloud instantly without managing any servers or infrastructure. Most developers use it to deploy AI models or heavy data processing tasks in under 60 seconds by writing simple Python decorators (special functions that start with @). For beginners, this means you can go from a script on your laptop to a powerful GPU-backed application in the cloud for less than $0.01 per run.
What makes Modal different from other cloud providers?
Traditional cloud providers like AWS (Amazon Web Services) require you to set up virtual machines, manage Docker containers (packages that hold your code and its dependencies), and configure complex networking. This often takes hours or days for a beginner to get right.
Modal changes this by using "serverless" technology, which means the computer only exists while your code is running. When your script finishes, the computer disappears, and you stop paying immediately.
The platform is designed specifically for Python, making it accessible if you already know the basics of the language. You don't have to learn how to manage Linux servers or write complex configuration files to get your code into the cloud.
Why should you use Modal for AI projects?
Training or running modern AI models like Claude Sonnet 4 or Llama 3 requires massive amounts of computing power called GPUs (Graphics Processing Units). Most personal laptops do not have enough memory to run these models locally.
Modal provides instant access to high-end GPUs on a pay-as-you-go basis. Instead of buying a $2,000 graphics card, you can rent one for a few cents to run a specific task.
We've found that the biggest hurdle for new developers is "environment hell," where code works on your machine but breaks in the cloud. Modal solves this by automatically syncing your local Python environment to the cloud every time you run your script.
What do you need to get started?
Before you write your first cloud function, you need a few basic tools installed on your computer. Don't worry if you haven't used these much; the setup process is very forgiving.
- Python 3.12+: Ensure you have a modern version of Python installed on your system.
- A Modal Account: You can sign up at Modal.com using your GitHub account.
- Terminal Access: You'll need to use your Command Prompt, Terminal, or PowerShell.
- An API Key: Modal will provide this during the setup process to link your computer to their cloud.
Step 1: How to install the Modal library?
The first step is to install the Modal software on your local computer so your code can communicate with their servers. Open your terminal and type the following command:
pip install modal
# pip is the standard tool for installing Python packages
Once the installation finishes, you need to authenticate your computer. Type this command and follow the instructions in your web browser:
modal setup
# This opens a browser window to log you in and save your credentials
You should see a message saying "Token created" in your terminal. This means your computer is now authorized to send code to Modal's cloud.
Step 2: How to write your first cloud function?
Create a new file named hello_modal.py in your favorite code editor. We will write a simple function that tells Modal to run a piece of code on their servers instead of your laptop.
import modal
# Create a 'Stub' which is the blueprint for your cloud application
app = modal.App("example-app")
# The @app.function() decorator tells Modal to run this in the cloud
@app.function()
def square_number(x):
print("This is running in the cloud!")
return x * x
# This part tells Python what to do when you run the file
@app.local_entrypoint()
def main():
# Call the cloud function and see the result
result = square_number.remote(10)
print(f"The result is {result}")
In this code, the .remote() method is the magic part. It tells Python not to run the function on your CPU, but to ship it off to a Modal server, execute it there, and send the answer back to you.
Step 3: How to run your code in the cloud?
To execute your script, go back to your terminal and run the following command. You will see Modal "building" your environment in real-time.
modal run hello_modal.py
# This command triggers the execution on Modal's servers
What you should see is a series of logs showing a container starting up. Within a few seconds, it will print "This is running in the cloud!" followed by the result (100).
Notice that you didn't have to set up a server or install Python on a remote machine. Modal handled all the heavy lifting in the background.
How does Modal handle dependencies and GPUs?
One of the most powerful features for beginners is the Image system. In programming, an "Image" is a snapshot of all the software and libraries your code needs to run.
If you want to use a specific library like torch (for AI) or numpy (for math), you define it at the top of your file. Modal will build that environment for you automatically.
# Define a custom environment with specific libraries
my_image = modal.Image.debian_slim().pip_install("numpy")
@app.function(image=my_image, gpu="A10G")
def run_ai_task():
# This code now has access to a powerful A10G GPU
pass
By adding gpu="A10G" to the decorator, you are telling Modal: "When this function runs, give it a professional-grade AI chip." You only pay for the seconds that the chip is actually working.
What are some common mistakes to avoid?
When starting out, it's normal to run into a few bumps. We have seen that most beginners struggle with these three areas:
- Forgetting .remote(): If you call
square_number(10)instead ofsquare_number.remote(10), the code will run on your local laptop instead of the cloud. - Large Local Files: Modal tries to sync your local folder to the cloud. If you have gigabytes of data in your folder, the "mount" process will be very slow. Keep your data in specialized cloud storage or use Modal's "Volumes" feature.
- Global Variables: Code inside a Modal function can't easily see variables defined outside of it unless they are passed as arguments. Always pass the data your function needs directly into the function call.
Next Steps
Now that you have run your first cloud function, you can explore more advanced features like "Web Endpoints," which turn your Python functions into a live website URL. This allows you to build your own AI APIs (Application Programming Interfaces) that other people can use.
You should also look into "Volumes," which act like a hard drive in the cloud that stays active even when your code isn't running. This is perfect for storing large AI model weights or datasets.
To continue your journey, check out the official Modal documentation.