agentleFS
botirkhaltaevbotirkhaltaev/botir-skills/skills/modal-servers/SKILL.md

modal-servers

Skill4 starsChanged 3 months agoApache-2.0View on GitHub

Low-latency Modal Servers via @app.server() and modal.Server. Use when serving long-lived HTTP/WebSocket/gRPC processes with regional routing, proxy-token auth, sticky sessions, or migrating from @modal.web_server. Prefer this over Function web endpoints when request latency and concurrent in-process serving matter. Triggers on: "@app.server", "modal.Server", "Modal Server", "modal.direct", "target_concurrency", "Modal-Session-ID", "proxy token", "low-latency HTTP Modal".

We split this skill into the rules it gives an agent

8 rules. About 0 are common, 3 are distinctive, and 5 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈3

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 5

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Use this skill when deploying a process that listens on a port inside Modal with the Server primitive (SDK 1.5.1+), optimized for …

    line 14
  2. Very specific:

    Prefer unset target_concurrency over max_containers=1 for singletons (allows rolling replacement).

    line 98
  3. Distinctive:

    When no containers are warm, requests are rejected with 503 (they do not queue like Function inputs).

    line 105
  4. Very specific:

    Do not use @modal.method() or Function web decorators on a Server class.

    line 141
  5. Very specific:

    Check startup_timeout and that the process binds the right port

    line 147
  6. Very specific:

    Ensure app isn’t doing cold model load on every request — use @modal.enter + snapshots if appropriate

    line 155
  7. Very specific:

    Always bind 0.0.0.0, not 127.0.0.1

    line 171
  8. Distinctive:

    Do not assume request queueing or built-in retries

    line 172

≈ “Common” is an estimate: at least 1 in 5 of the 308,232 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Modal Servers

Use this skill when deploying a process that listens on a port inside Modal with the Server primitive (SDK 1.5.1+), optimized for ultra-low-latency HTTP between clients and containers.

For Function-based web endpoints (fastapi_endpoint / asgi_app / wsgi_app / web_server), see modal-web-serving. For one-command LLM inference products, see modal-endpoints.

When to Use This Skill

  • Serving vLLM, SGLang, or any HTTP server with minimal proxy latency
  • Need regional request routing (routing_region) close to clients
  • Want sticky sessions via Modal-Session-ID
  • Migrating from @modal.web_server to @app.server
  • Need default authenticated ingress (proxy tokens) or explicit unauthenticated=True
  • Building custom inference behind the same infrastructure as Auto Endpoints

Quick Start

import modal
import subprocess

app = modal.App("my-server")

@app.server(port=8000, unauthenticated=True)
class FileServer:
    @modal.enter()
    def start(self):
        # Must bind 0.0.0.0 and listen on the configured port
        self.proc = subprocess.Popen(
            ["python", "-m", "http.server", "8000", "--bind", "0.0.0.0"]
        )

    @modal.exit()
    def stop(self):
        self.proc.terminate()

Deploy: modal deploy server_app.py

URL shape: https://<workspace>--<app>-<server>.<routing_region>.modal.direct/...

Retrieve programmatically: modal.Server.get_url() (see reference).

Servers vs Functions vs Web Decorators

Concern@app.server@modal.web_server / web Function.remote() Function
Request modelStateless reverse proxy → portFunction inputs / ASGI bridgeQueued inputs
Cold zero→oneFirst requests may 503 until readyInputs queue while cold-startingInputs queue
Concurrency defaultProcess handles many requests; set target_concurrency to autoscaleOne input unless @modal.concurrentOne input unless concurrent
Auth defaultRequires Modal-Key / Modal-Secret unless unauthenticated=TrueOften public URL patternsN/A
.remote() / mapNoLimited / N/A for web-onlyYes
Retries / input timeoutsApp/client responsibilityModal input systemModal input system
Best forLow-latency long-lived serversSimple ASGI/WSGI/FastAPI FunctionsBatch/RPC compute

Core Configuration

@app.server(
    image=image,
    gpu="H100",
    port=8000,
    startup_timeout=30 * 60,
    target_concurrency=32,       # soft target for autoscaling
    min_containers=0,
    max_containers=10,
    buffer_containers=1,
    scaledown_window=10 * 60,
    exit_grace_period=30,        # finish in-flight before SIGTERM
    routing_region="us-east",    # us-east | us-west | ca-central | eu-west | ap-south
    compute_region="us",         # optional: pin container placement (cost multiplier)
    unauthenticated=False,       # default: proxy-token auth required
    h2_enabled=False,
    secrets=[...],
    volumes={...},
    enable_memory_snapshot=False,
)
class InferenceServer:
    @modal.enter()
    def start(self):
        ...

Autoscaling notes

  • Omit target_concurrency → no concurrency-based scale-out (still distributes if min_containers > 1).
  • Prefer unset target_concurrency over max_containers=1 for singletons (allows rolling replacement).
  • Dynamic tuning: modal.Server.update_autoscaler(...) (reset on next deploy).
  • Containers are not ready until the port is listening; failed readiness within startup_timeout → failed container.
  • Health checks verify the port stays up; repeated failures replace the container.

Zero-to-one

When no containers are warm, requests are rejected with 503 (they do not queue like Function inputs). Clients must retry. The first request still triggers a cold start.

Authentication

Default: require headers:

Modal-Key: wk-...
Modal-Secret: ws-...

Create tokens:

modal workspace proxy-tokens create
# With RBAC, scope to the Server's environment:
modal workspace proxy-tokens allow wk-... main

Testing helper (adds auth for you; higher latency): modal curl <url>/path

Public traffic: set unauthenticated=True.

RBAC: tokens must be scoped to the Environment or requests get 403.

Sticky Sessions

Send header Modal-Session-ID: <any-string> so related requests hit the same container (useful for WebSockets / conversational state).

Lifecycle

  1. @modal.enter() starts the process binding 0.0.0.0:<port>
  2. Modal waits until the port accepts connections
  3. Traffic flows through the regional routing plane
  4. Scale-down: stop new requests → exit_grace_period for in-flight → SIGTERM → @modal.exit() → hard kill after grace

Do not use @modal.method() or Function web decorators on a Server class. Request handling is entirely the child process’s job.

Symptom Triage

503 on first traffic

  • Expected at zero containers; keep min_containers>=1 or retry with backoff
  • Check startup_timeout and that the process binds the right port

401 / 403

  • Missing proxy token headers, or token not allowed for this Environment
  • Accidentally left unauthenticated=False for public clients

High latency still

  • Move routing_region (and optionally compute_region / colocate) nearer clients
  • Ensure app isn’t doing cold model load on every request — use @modal.enter + snapshots if appropriate

Migrating from @modal.web_server

  • Move startup into @modal.enter on an @app.server class
  • Revisit auth defaults and 503 cold-start behavior
  • Drop Function input concurrency decorators; use target_concurrency instead

Reference Map

  • references/server-basics.md — definition rules, URL, vs Cls/web_server
  • references/autoscaling-auth.md — concurrency, auth, sticky sessions, routing
  • references/migration-from-web-server.md — migration checklist

Guardrails

  • Requires Modal Python SDK 1.5.1+
  • Always bind 0.0.0.0, not 127.0.0.1
  • Do not assume request queueing or built-in retries
  • Serialization, timeouts, and retries are application concerns
  • Mark regional compute pinning cost multipliers when advising users

Key References

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.