Skip to content
lunduniversityPublic

About

OpenAI-compatible gateway to coding models on shared GPUs, with per-user keys and fair-share budgets.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

40 Commits

Folders and files

Repository files navigation

modlypass

OpenAI-compatible gateway to coding models on shared GPUs, with per-user keys and fair-share budgets.

Status: early pilot. One user can do agentic coding with OpenCode or Pi through the gateway and an Ollama worker on an HPC GPU node, connected by SSH tunnels, with streaming. Next: the worker connecting out to the gateway (PROTOCOL.md). No API keys or multi-user features yet. This README is the design overview; requirements are in PRD.md. modlypass is a pilot at Lund University and not an official university service.

Why

Open-weight coding models such as Qwen3-Coder are good enough to drive a coding agent, but they need a large GPU. Universities often have such GPUs in an HPC centre, but students do not have accounts there, and HPC clusters are built for batch jobs, not for interactive services shared by a class.

modlypass sits in between. It lets a teacher give every student in a course a personal API key that works directly in a coding agent such as OpenCode, while the GPU time stays fairly shared, budgeted and accounted for.

How it works

 student laptop                gateway host (department server)          HPC centre
 ┌──────────────┐  HTTPS       ┌───────────────────────────────┐  wss    ┌────────────────────────┐
 │ OpenCode     │ ───────────► │ nginx (TLS)                   │ ◄────── │ GPU node (Slurm job)   │
 │ apiKey=…     │  /v1/chat/…  │   └─► modlypass               │ opened  │  worker                │
 └──────────────┘  streaming   │        keys, queue, budgets,  │ by the  │  Ollama or vLLM        │
                               │        usage log, workers     │ worker  │  qwen3-coder:30b       │
                               └───────────────────────────────┘         └────────────────────────┘
  1. The teacher creates one API key per student from the course list and sends it out, together with a ready-to-paste opencode.json snippet.
  2. The student's OpenCode sends OpenAI-compatible requests (/v1/chat/completions, streaming, tool calls) to modlypass with Authorization: Bearer <key>.
  3. modlypass checks the key and the student's budget, queues the request fairly, and hands it to a worker. The worker runs in a job on an HPC GPU node next to the model server, and has connected out to modlypass over WebSocket (PROTOCOL.md). It passes the request to the model server, and the streamed reply comes back unchanged. (Iterations 0 and 1 use an SSH tunnel from the gateway host instead.)
  4. When the request finishes, modlypass records who, when, GPU time and token counts. It never stores prompts or code.

Design decisions

These are the decisions so far. They will be refined in PRD.md.

Students and keys

  • Students are indirect users. They get no account at the HPC centre and no shell access. The teacher running the gateway holds the HPC account and is responsible for the service. Every request is traceable to one student through the gateway's log.
  • One API key per student. Version 1: an admin command reads the course list (CSV with name and e-mail), creates the keys, and writes one ready-to-send message per student containing the key, a complete opencode.json snippet and a link to a getting-started page. Messages are e-mailed automatically if the host can send mail, otherwise by hand.
  • Only a hash of each key is stored. A lost or leaked key is revoked and reissued with one command.
  • No central single sign-on. An e-mail login link (one-time link to a course-list address) may come later.

Fair sharing

  • At most one request in flight per student (configurable), with a fair queue that serves active students in turn. This alone prevents most hogging.
  • Budgets in GPU-seconds: how long each request held a slot on the GPU. This is the real cost. Tokens are a poor measure with coding agents, which resend the whole conversation and file contents on every turn.
  • Tokens are logged for statistics, read from the usage data in the last streamed chunk.
  • GET /me shows a student their own usage and remaining budget. Admins can top up budgets.
  • Group priority (group A has priority in time slot X, but others still get unused capacity) is a possible later addition, only if measured queue waits call for it.

Availability

  • The model server runs as chained Slurm jobs during set service hours, so the GPU does not burn allocation at night. The worker in each job connects to modlypass when it starts, so modlypass always knows which GPUs are up, without any login at the HPC centre.
  • Extra capacity during scheduled labs: a second GPU job, ideally under a Slurm reservation. modlypass spreads requests over the available backends.
  • Honest unavailability. When no model server is up, modlypass answers with a clear message ("model offline, back at 08:00") instead of letting requests time out, and /health reports the backend state it actually probed.

Model and backend

  • First model: qwen3-coder:30b (mixture of experts, about 3B active parameters, about 19 GB at 4-bit).
  • First backend: Ollama, because it is quick to set up. vLLM will be measured before real use, since it batches many users' requests far better.
  • The backend is anything OpenAI-compatible, so modlypass is not tied to one HPC centre or one inference server.

Privacy and security

See Security and privacy policy below.

Implementation

  • Scala 3 on the JVM, few dependencies: the JDK's built-in HTTP server and HttpClient with virtual threads in the first iterations; Jetty 12 when the gateway also accepts the workers' WebSocket connections (reasons in Design: serverLib in PRD.md). No web framework.
  • Clients speak OpenAI Chat Completions in the pilot, with OpenCode as the reference client; other client APIs can be added later (Design: clientApi).
  • Storage starts simple: an append-only usage log and a small users file, loaded into memory at startup. A database only when needed.
  • Lessons carried over from modly: health checks that probe the backend, failures reported as failures, never an empty success.

Security and privacy policy

This policy is a draft for the pilot, written so that the HPC centre can assess the service. It will be refined together with the centre.

Responsibility

  • One named person (the teacher running the gateway) holds the HPC account and is responsible for the service towards the HPC centre.
  • Students are indirect users: they have no account at the HPC centre, no shell, no file access and no access to the scheduler. The only thing they can reach is a chat-completion API.
  • Every request is traceable to one student through the gateway's log.

Access control, step by step

Stage Who can use the model How it is enforced
Iterations 0 to 2 Only the teacher: on the gateway host, or from their own laptop through an SSH tunnel to it Loopback-only relay (below)
Iteration 3 and later Holders of a valid personal API key Key check on every request, HTTPS

Loopback-only relay. Loopback is the address a computer uses to talk to itself (127.0.0.1 or ::1, also called localhost). Loopback traffic never leaves the machine, so a request that arrives from a loopback address was sent by a program on the gateway host itself. Until API keys exist, the gateway serves its public info page and health check to anyone, but relays requests under /modlypass/v1/ only if they come from a loopback address. Everyone else gets 403 Forbidden, and nothing reaches the HPC centre.

Why this matters: without keys, the source address is the only access control. Bots scan the whole internet for open ports all the time, and exposed language-model servers are actively sought out and abused. An open relay would let strangers spend the HPC allocation, break the HPC centre's rules, and make performance measurements meaningless.

The same principle applies at the HPC centre: the model server on the GPU node listens only on localhost, so other cluster users cannot reach it. In iterations 0 and 1 the gateway reaches it through an SSH tunnel, which makes requests appear to come from loopback on the node, so SSH login is the access control on that side. From iteration 2 a worker next to the model server connects out to the gateway instead, over TLS and with a secret worker key (PROTOCOL.md).

API keys (from iteration 3). Every request must carry a personal key in the Authorization header; requests without a valid key get 401. Only a hash of each key is stored. Keys can be revoked and reissued at any time. Keys never travel over plain HTTP: clients connect through HTTPS or an SSH tunnel.

Limiting misuse and cost

  • At most one request in flight per student (configurable), served through a fair queue.
  • A budget in GPU-seconds per student and period. When it is used up, requests are refused with a clear message.
  • The model server runs only during agreed service hours, within the HPC project's allocation.

What is logged

  • Logged: student id (from the key), time, model, HTTP status, duration, queue wait, GPU-seconds and token counts.
  • Never logged or stored: prompts, code, file contents, replies.
  • Logs stay on the gateway host and are used only to run the service, share GPU time fairly and report usage to the HPC centre.

Exposure

  • The public surface is the gateway's HTTPS endpoint: an info page, a health check that reveals no internal details, and the OpenAI-compatible API behind key checks.
  • The connection between the gateway and the HPC centre is always opened from one side to the other, never from the internet into the HPC centre: first an SSH tunnel from the gateway host, from iteration 2 a WebSocket connection from the worker to the gateway. Nothing at the HPC centre is exposed to the internet.

Reporting a security problem

Please report security problems privately through GitHub's security advisories for this repository, not in a public issue.

Related projects

  • modly: a small single-user model router in front of a local Ollama. modlypass is a separate, multi-user sibling; modly is unchanged.
  • agaton: manages several coding-agent accounts on the client side. A future agaton profile could point OpenCode at a modlypass key.

Try it: the minimal viable gateway (MVG)

Separate scala-cli projects (Scala 3.10.1 with -experimental, JVM 25, which scala-cli downloads itself), each with munit tests. Files with pure rules are compiled in Scala's safe mode (import language.experimental.safe), so the compiler refuses effects in them.

Directory Runs on What it does Dependencies
worker/ HPC GPU node Starts Ollama, checks the GPU, the port and the model, loads the model, reports how much of it is on the GPU, sends a warm-up chat request so the first user does not wait for the cold start, writes node and port to ~/.local/state/modlypass/worker.properties. Then either connects out to the gateway over WebSocket (--gateway, iteration 2) or prints the SSH tunnel command as before. --attach URL uses a model server that already runs instead os-lib, upickle, common/
gateway/ gateway host Jetty 12 behind a thin Scala facade (Server.scala). Relays /modlypass/v1/* to a connected worker (first-come queue, slots, cancel), or to the backend URL (the SSH tunnel) when no worker is connected; accepts workers at /modlypass/worker with a worker key; /modlypass/health shows workers and queue; /modlypass/ is a public info page; one metadata line per request (never content) to the console and ~/.local/state/modlypass/gateway.log; the relay is loopback-only until API keys come Jetty 12, upickle, common/
client/ your machine Dumb terminal chat: reads prompts from stdin, keeps the conversation, prints the streamed reply and the time to first token (/reset, /quit) ujson
common/ (shared) Code used by more than one project, included with //> using file ../common/src: the protocol messages of PROTOCOL.md, the worker's side of the connection (WorkerLink.scala), the key rules, and thin wrappers over the JDK's concurrency classes upickle

scala build.scala in the repository root checks that Java 21+ and scala-cli are installed, then compiles and tests all projects and prints a summary. scala build.scala -- clean test starts from a clean state; scala build.scala -- test worker client tests some projects; -- help lists the commands. No sbt or Mill (reasons in Design: buildTool in PRD.md).

Settings live in ~/.config/modlypass/settings.conf (KEY=value lines), shared by all parts:

ACCOUNTNAME=<slurm-account>   # worker/start-interactive.sh (see: projinfo)
PARTITION=gpua40              # optional, default gpua40
PORT=8099                     # gateway port (default 8099)
BACKEND=http://127.0.0.1:11500  # gateway backend: the local end of the tunnel to the worker
# From iteration 2: the worker connects out instead of the tunnel
GATEWAY_URL=wss://<gateway-host>/modlypass/worker  # worker: where to connect (unset = tunnel mode)
WORKER_KEY_FILE=/home/<you>/.config/modlypass/worker-key  # worker: its key (the default; chmod 600)
WORKER_NETS=<hpc-range>/24    # gateway: where workers may connect from (empty = loopback only)
TRUSTED_PROXY=<proxy-address> # gateway: the reverse proxy whose X-Forwarded-For is believed
PLAIN_WORKERS=false           # gateway: true allows unencrypted workers, only until HTTPS works

A worker key is created on the gateway host with scala run gateway -- worker-key new <name>; it is shown once and only its hash is stored, in ~/.config/modlypass/keys. Copy the key to the worker's key file on the HPC side and chmod 600 it. worker-key list and worker-key revoke <name> manage keys.

With GATEWAY_URL set, the worker connects out and no tunnel is needed: start it in a terminal with worker/start-interactive.sh, or as a batch job that runs for TIME (default 8 hours) with worker/submit.sh; its output goes to ~/.local/state/modlypass/worker-<jobid>.log. Two minutes before the time limit the worker closes its connection normally.

Steps (the HPC side as at LUNARC COSMOS):

  1. HPC login node: clone the repo, run scala compile worker once (so dependencies are cached in the shared home), then worker/start-interactive.sh. It prints projinfo and sinfo, starts one Slurm task with 8 cores on a GPU node, loads the Ollama module and runs the worker. The worker ends by printing the tunnel command.
  2. Gateway host: run that tunnel command (in tmux), then scala run gateway. Check with curl localhost:8099/modlypass/health.
  3. Your machine: ssh -N -L 8099:localhost:8099 <gateway-host>, then scala run client.

First measurements (qwen3-coder:30b, Q4_K_M, 64k context, one A100 40 GB):

  • 25 GB of GPU memory, 100% on the GPU, model load 9 s.
  • 47 tokens/s generation measured on the node; through the whole chain about 75 tokens/s and 0.5 s to the first token for a long answer.
  • Lesson: Slurm CPU binding matters. Eight one-core tasks instead of one eight-core task locked Ollama to a single core and made it about 10× slower.
  • The model's code is good but not reliable: in a "many variants" answer, about half the variants were wrong or did not compile. Students must compile and test what they get.

First deployment

The pilot runs at the Department of Computer Science, LTH, Lund University, for an elective course with about 25 students in study period 3 (January to March 2027). The backend is the COSMOS cluster at LUNARC, part of NAISS, through the NAISS project ICCIS (Interactive Code Generation for Students at LTH).

Roadmap

The iterations and their features are in PRD.md; this is the short version. Versions: 0.0.x while iterations 0 to 2 are built (teacher only), 0.1.0-alpha when others can test (iteration 3, target 14 December 2026, demo for the HPC centre), 0.x.0 after each round of testing, and 1.0.0 for the students (target 11 January 2027; the course starts on 18 January 2027 and students get their keys at the first lecture).

  • PRD.md in reqT-lang: goals, design decisions, iterations, features, tests, risks.
  • Iteration 0, minimal viable gateway (2026-11-27): dumb chat client on a laptop → gateway → model on an HPC GPU node, with streaming, over SSH tunnels.
  • Iteration 1, one user with OpenCode (2026-11-29): OpenCode and Pi on the teacher's laptop doing real coding work with tool calls through the gateway; metadata log file with token counts; info page; worker warm-up; scala build.scala and shared code in common/; measurements with prompts up to 65k tokens and 128k context (see PRD.md). The vLLM trial moved to the course pilot.
  • Iteration 2, the worker connects out (planned 2026-11-29, ends as 0.0.3): PROTOCOL.md (reviewed; messages implemented and tested), WebSocket transport, Jetty 12 behind a thin Scala facade, a request queue in the gateway with several workers and cancel, worker keys in a key store shared with the later API keys, HTTPS through the department's nginx, Slurm job script; Java 25 and Scala safe mode for pure code. Steps and decisions in PRD.md.
  • Iteration 3, several users: API keys, admin command, one request at a time per key, fair queue, usage per key; two OpenCode clients at once.
  • Course pilot: GPU-second budgets, GET /me, student messages, offline message, several workers with sticky routing, getting-started page.
  • Pilot report after the course: GPU-hours per student, peak load, queue waits, feedback.

License

Apache-2.0. See LICENSE.

About

OpenAI-compatible gateway to coding models on shared GPUs, with per-user keys and fair-share budgets.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages