一键导入
modal-guidelines
Rules and guidelines for implementing Modal code, including core concepts, coding style, and common commands.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Rules and guidelines for implementing Modal code, including core concepts, coding style, and common commands.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
| name | Modal Guidelines |
| description | Rules and guidelines for implementing Modal code, including core concepts, coding style, and common commands. |
This file provides rules and guidelines for LLMs when implementing Modal code.
Always refer to documentation and examples for up-to-date functionality and exact syntax.
The basic unit of serverless execution on Modal.
Each Function executes in its own container, and you can configure different Images for different Functions within the same App:
image = (
modal.Image.debian_slim(python_version="3.12")
.pip_install("torch", "numpy", "transformers")
.apt_install("ffmpeg")
.run_commands("mkdir -p /models")
)
@app.function(image=image)
def square(x: int) -> int:
return x * x
You can configure individual hardware requirements (CPU, memory, GPUs, etc.) for each Function.
@app.function(
gpu="H100",
memory=4096,
cpu=2,
)
def inference():
...
Some examples specificly for GPUs:
@app.function(gpu="A10G") # Single GPU, e.g. T4, A10G, A100, H100, or "any"
@app.function(gpu="A100:2") # Multiple GPUs, e.g. 2x A100 GPUs
@app.function(gpu=["H100", "A100", "any"]) # GPU with fallbacks
Functions can be invoked in a number of ways. Some of the most common are:
foo.remote() - Run the Function in a separate container in the cloud. This is by far the most common.foo.local() - Run the Function in the same context as the caller. Note: This does not necessarily mean locally on your machine.foo.map() - Parallel map over a set of inputs.foo.spawn() - Calls the function with the given arguments, without waiting for the results. Terminating the App will also terminate spawned functions.Web endpoint: You can turn any Function into an HTTP web endpoint served by adding a decorator:
@app.function()
@modal.fastapi_endpoint()
def fastapi_endpoint():
return {"status": "ok"}
@app.function()
@modal.asgi_app()
def asgi_app():
app = FastAPI()
...
return app
You can run Functions on a schedule using e.g. @app.function(schedule=modal.Period(minutes=5)) or @app.function(schedule=modal.Cron("0 9 * * *")).
Cls)For stateful operations with startup/shutdown lifecycle hooks. Example:
@app.cls(gpu="A100")
class ModelServer:
@modal.enter()
def load_model(self):
# Runs once when container starts
self.model = load_model()
@modal.method()
def predict(self, text: str) -> str:
return self.model.generate(text)
@modal.exit()
def cleanup(self):
# Runs when container stops
cleanup()
requirements.txt/pyproject.toml files, and putting import statements inside the Function def. Any code in the global scope needs to be executable in all environments where that App source will be used (locally, and any of the Images the App uses).import modal, and qualified names like modal.App(), modal.Image.debian_slim().modal run an App that uses deprecated features. When writing new code, never use deprecated features.Running modal --help gives you a list of all available commands. All commands also support --help for more details.
modal run path/to/your/app.py - Run your app on Modal.modal run -m module.path.to.app - Run your app on Modal, using the Python module path.modal serve modal_server.py - Run web endpoint(s) associated with a Modal app, and hot-reload code on changes. Will print a URL to the web endpoint(s). Note: you need to use Ctrl+C to interrupt modal serve.modal deploy path/to/your/app.py - Deploy your app (Functions, web endpoints, etc.) to Modal.modal deploy -m module.path.to.app - Deploy your app to Modal, using the Python module path.Logs:
modal app logs <app_name> - Stream logs for a deployed app. Note: you need to use Ctrl+C to interrupt the stream.modal app list, modal volume list, and similarly for secret, dict, queue, etc.list - use e.g. modal app --help for more.app.deploy(), you can wrap it in a with modal.enable_output(): block to get more output.