AI

Becoming your own model provider: a gateway written and attacked by Claude agents

gw makes one DGX Spark a model provider for my own agents and machines, with one OpenAI-compatible address, a key per client, a record of use and a door through Cloudflare; the language model is live, while the image and speech models are not served yet.

For agentsView .mdOpen in ClaudeOpen in ChatGPT
For agentsView .mdOpen in ClaudeOpen in ChatGPT
Contents
A long prompt through the Cloudflare tunnel, before and after gw sent bytes while the model readsThree timelines in seconds, with Cloudflare's 100-second limit marked. Before the fix, a 256k-token prompt sent nothing for 100 seconds and Cloudflare answered 524. A streamed 388,745-token request got its headers at once and a comment line every 15 seconds; the first token came at 286 seconds. A non-streamed 245,206-token request got a 200 at 31 seconds, then a space every 15 seconds, nine in all, and its JSON body at 156.6 seconds.Cloudflare answers 524 after 100 s without a byteBefore the fix: a 256k-token prompt, nothing sent while the model reads524Streamed, 388,745 tokens: 200 at once, a comment line every 15 s200 at oncefirst token, 286 sNot streamed, 245,206 tokens: 200 after 31 s, a space every 15 s200 at 31 sJSON body, 156.6 s050100150200250300seconds after the request →
A long prompt through the Cloudflare tunnel, before and after gw sent bytes while the model readsThree timelines in seconds, with Cloudflare's 100-second limit marked. Before the fix, a 256k-token prompt sent nothing for 100 seconds and Cloudflare answered 524. A streamed 388,745-token request got its headers at once and a comment line every 15 seconds; the first token came at 286 seconds. A non-streamed 245,206-token request got a 200 at 31 seconds, then a space every 15 seconds, nine in all, and its JSON body at 156.6 seconds.Cloudflare: 524 after100 s without a byteBefore the fix: 256k tokens, nothing sent524Streamed, 388,745 tokens200 at oncefirst token, 286 sNot streamed, 245,206 tokens200 at 31 sJSON, 156.6 s0100200300seconds after the request →
Cloudflare drops a request that sends nothing for 100 seconds; gw now sends something every 15 seconds until the model answers.Source: gw repository (docs/gorevler/G3.2-uzun-istek.md, README) and the live checks through the tunnel on 26–27 November 2026. The ticks are drawn at the configured 15-second interval.
Data
Long prompts through the Cloudflare tunnel
RequestPrompt tokensFirst byte to the client (s)While the model readsEvery (s)Answer (s)Result
Before the fixabout 256knonenothingnone524 from Cloudflare at 100 s
Streamed388,745at once (200 and SSE headers)a ": prefill" comment line15286first token at 286 s; correct answer
Not streamed245,20631one space (9 in all)15156.6parseable JSON; correct answer

Source: gw repository (docs/gorevler/G3.2-uzun-istek.md, README) and the live checks through the tunnel on 26–27 November 2026

In plain words

The AI machine on my desk runs a language model, image models and a speech model. The aim is to be my own provider for them: my agents and computers call them like a hosted model API. That needs one address, a key for each client, a record of who used what, and a way in from anywhere. gw is that front door. It serves the language model on the machine and, through Cloudflare, a service that relays traffic to the machine, to my other computers. The image and speech models sit behind the same door but are not served yet. Claude agents wrote gw at my request; other Claude agents attacked it after every step.

My view: as LLM costs rise, local AI will keep developing this way, and with developments like this we will in fact become our own providers. The main goal of gw is exactly that: to be the provider of my own language model and my other models, on one NVIDIA DGX Spark.

A model that runs is not yet a provider. Clients need one endpoint, a key each, a record of use and a way in from anywhere, and when the models do not all fit in memory, something has to decide which one runs.

Here they do not fit. The machine has 121.7 GiB of unified memory. Inkling 2 Mini on vLLM takes about 88 GiB and four to five minutes to start; the ComfyUI image, video and music models about 63 GB; a speech server about 2 GiB.

Every client had a port written into it, only Tailscale devices could reach the models from outside, and nobody owned the decision of what to stop. On 25 November Inkling was stopped to make room for ComfyUI, and a web app that depended on it silently lost its AI features. One agent had also stopped its own model server three times.

On 26 November I asked for one gateway that agents can manage and that I can reach from anywhere, “even from a session on another PC”, in Rust. From the first design commit to a panel with sessions, cache hits and speeds took 23 commits and about 20.5 hours; gw was live behind Cloudflare by about 19:40 on the first day.

What a provider needs, and what gw does

A provider needsWhat gw does
One endpointOne OpenAI-compatible address for every model, plus /m/<id>/… for a model’s own API
A key per clientA gw key per machine or agent: 32 random characters, stored only as a digest, revocable
A record of useEvery request tied to an agent and a conversation, with tokens and speeds; no prompts stored
A way in from anywhereA Cloudflare tunnel, with Cloudflare Access in front and gw’s own keys behind it
A decision on what runsA manager that plans which model to stop and asks the owner first; switched off for now

Who built it

I did not write a line of it. A Claude session in Claude Code, which the repository calls the architect, wrote the design and one brief per phase, checked each result, committed and deployed. Claude subagents (Opus) wrote all of the code. After each round a separate Claude subagent attacked the build; its findings became the next brief, and every fix came with its own test.

My part was the request, the decisions, and a yes or no on every step that touched the live machine or the internet. One decision matters below: write the gateway, but do not start models yet. The switch for that, lifecycle.enabled, is still off.

Build or buy

The closest existing project is llama-swap: Go, MIT licence, 6,326 stars, with model swapping, capacity routing, ComfyUI and audio endpoints and a UI. The design kept it as the reference and the fallback. Four things ruled out running it as it is:

  1. It runs each model server as its own child process. Here the servers are systemd units that must outlive a gateway restart.
  2. Its memory numbers are written by hand. Here nvidia-smi cannot even report GPU memory (it prints [N/A]); only /proc/meminfo shows it.
  3. Its MCP endpoint only serves documentation. Agents cannot load or unload a model.
  4. It has no owner approval and no flow that adds a model and measures it.

So gw borrowed ideas instead: the capacity planner and roles from llama-swap, the systemd lifecycle and queue from model-swap-proxy, and wake, sleep and alive hooks from llmux.

How it fits together

gw is one Rust binary with 268 tests, all run against a fake backend and no GPU; the repository has the details. The switch above the diagram lights up one caller’s path at a time.

Cloudflare Access checks a service token first; gw then wants a valid Access JWT on every request, its own key as well on /v1, /m and /mcp, and refuses admin keys on this listener.

gw between its callers and the model servers on one DGX SparkRemote clients pass Cloudflare Access and the tunnel to gw's remote listener on port 4001; local agents use the local listener on port 4000; the owner's CLI uses admin.sock. Inside gw, the data plane, the MCP tools and the panel ask the manager, which consults the planner and keeps an SQLite record. The manager may start or stop systemd user units only when the lifecycle switch is on, and it is off. Requests go from the data plane straight to the model servers: /v1 to vLLM with Inkling 2 Mini on port 8020, /m/<id> with WebSockets to ComfyUI, and /v1/audio to the speech server.gw: one Rust binaryRemote clienta laptop or a cloud session: Claude Code, theOpenAI SDKLocal agentson the machine itselfOwner's terminalthe gw CLICloudflare Accessservice token for machines, one-time PIN for theownerCloudflare Tunnelcloudflared, outbound onlyRemote listener :4001gw key and Access JWT; admin keys refusedLocal listener :4000a keyless client gets inferadmin.sockmode 0600, the ownerData plane/v1 OpenAI API, /m/<id> passthrough, SSEunbufferedMCP /mcptools for agents; they cannot approvePanel and API/ui, /api, live eventsManageradmission, single-flight starts, drain, memoryguard, approvalsPlannerthe cheapest set of models to stopSQLite recordevery request, no bodiessystemd user units, over D-Busgw starts or stops one only when lifecycle.enabled is on; on the live install it is offvLLM: Inkling 2 Mini127.0.0.1:8020, ~88 GiB; /v1ComfyUIimage, video, music, ~63 GB; /m/<id>, WebSockettooSpeech serverWhisper and Kokoro, ~2 GiB; /v1/audio/*
gw between its callers and the model servers on one DGX SparkRemote clients pass Cloudflare Access and the tunnel to gw's remote listener on port 4001; local agents use the local listener on port 4000; the owner's CLI uses admin.sock. Inside gw, the data plane, the MCP tools and the panel ask the manager, which consults the planner and keeps an SQLite record. The manager may start or stop systemd user units only when the lifecycle switch is on, and it is off. Requests go from the data plane straight to the model servers: /v1 to vLLM with Inkling 2 Mini on port 8020, /m/<id> with WebSockets to ComfyUI, and /v1/audio to the speech server.gw: one Rust binaryRemote clienta laptop or a cloud session:Claude Code, the OpenAI SDKLocal agentson the machine itselfOwner's terminalthe gw CLICloudflare Accessservice token for machines,one-time PIN for the ownerCloudflare Tunnelcloudflared, outbound onlyRemote listener :4001gw key and Access JWT; adminkeys refusedLocal listener :4000a keyless client gets inferadmin.sockmode 0600, the ownerData plane/v1 OpenAI API, /m/<id>passthrough, SSE unbufferedMCP /mcptools for agents; they cannotapprovePanel and API/ui, /api, live eventsManageradmission, single-flight starts,drain, memory guard, approvalsPlannerthe cheapest set of models tostopSQLite recordevery request, no bodiessystemd user units, over D-Busgw starts or stops one only when lifecycle.enabled is on; on the live install it is offvLLM: Inkling 2 Mini127.0.0.1:8020, ~88 GiB; /v1ComfyUIimage, video, music, ~63 GB;/m/<id>, WebSocket tooSpeech serverWhisper and Kokoro, ~2 GiB;/v1/audio/*
gw between its callers and the model servers on one DGX SparkRemote clients pass Cloudflare Access and the tunnel to gw's remote listener on port 4001; local agents use the local listener on port 4000; the owner's CLI uses admin.sock. Inside gw, the data plane, the MCP tools and the panel ask the manager, which consults the planner and keeps an SQLite record. The manager may start or stop systemd user units only when the lifecycle switch is on, and it is off. Requests go from the data plane straight to the model servers: /v1 to vLLM with Inkling 2 Mini on port 8020, /m/<id> with WebSockets to ComfyUI, and /v1/audio to the speech server.gw: one Rust binaryRemote clienta laptop or a cloudsession: ClaudeCode, the OpenAISDKLocal agentson the machineitselfOwner's terminalthe gw CLICloudflareAccessservice token formachines, one-timePIN for the ownerCloudflare Tunnelcloudflared,outbound onlyRemote listener:4001gw key and AccessJWT; admin keysrefusedLocal listener:4000a keyless clientgets inferadmin.sockmode 0600, theownerData plane/v1 OpenAI API,/m/<id>passthrough, SSEunbufferedMCP /mcptools for agents;they cannotapprovePanel and API/ui, /api, live eventsManageradmission,single-flight starts,drain, memoryguard, approvalsPlannerthe cheapest set ofmodels to stopSQLite recordevery request, nobodiessystemd user units, over D-Busgw starts or stops one only when lifecycle.enabled is on; on the liveinstall it is offvLLM: Inkling 2Mini127.0.0.1:8020, ~88GiB; /v1ComfyUIimage, video,music, ~63 GB;/m/<id>,WebSocket tooSpeech serverWhisper andKokoro, ~2 GiB;/v1/audio/*
One binary in front of every model: three ways in, one manager, and no start or stop until the owner turns the lifecycle on. On 28 November only the language model was being served; ComfyUI and the speech server sit in the same catalogue.Source: gw repository: docs/TASARIM.md §9 and §4, README, deploy/README.md; memory figures from the fact sheet.
Data
The parts in the diagram
PartWhereWhat it does
gwone Rust binary, run as a systemd user unitthe gateway: three listeners, the data plane, MCP, the panel, the manager and the record
Cloudflare AccessCloudflare edgeservice token for machines, one-time PIN for the owner
Cloudflare Tunnelcloudflared, a systemd user unitcarries requests for the hostname to 127.0.0.1:4001 only
Remote listener127.0.0.1:4001a gw key and a valid Access JWT on every request; admin keys refused
Local listener127.0.0.1:4000agents on the machine; a keyless client gets infer
Admin socketadmin.sock, mode 0600the owner's gw CLI
Data plane/v1, /m/<id>OpenAI-compatible API and raw passthrough; SSE unbuffered
MCP/mcptools for agents; they cannot approve
Panel and API/ui, /apiread-only for local clients; owner actions after `gw panel` or the Access login
Managerinside gwadmission, single-flight starts, drain, memory guard, approvals
Plannerinside gwthe cheapest set of models to stop
SQLite recordinside gwevery request, with no bodies
systemd user unitsD-Busstarted or stopped only when lifecycle.enabled is on; off on the live install
Model servers127.0.0.1vLLM with Inkling 2 Mini on :8020 (/v1), ComfyUI (/m/<id>, WebSocket included), a Whisper and Kokoro speech server (/v1/audio/*)

Source: gw repository: docs/TASARIM.md §9 and §4, README, deploy/README.md

Three ways in. Agents on the machine use port 4000; a client without a key there may run models but not manage them. Remote traffic arrives on port 4001, where every request needs a key and admin keys are refused. The owner’s command line talks over a private Unix socket.

gw tells remote traffic apart by the port it arrived on, not by a header: the Cloudflare connector also connects from 127.0.0.1.

One API. Clients send OpenAI-style requests and name a model or a role such as chat. Streams pass through unbuffered; in a test gw added 2.1 ms to the first byte.

A manager that asks. When a model is wanted that is not running, a planner picks the cheapest set of models to stop, preferring ones that start fast and have not been used lately. Stopping a model marked evict: approve needs the owner’s yes. Agents can ask; they cannot approve.

The manager also keeps order: many requests for one stopped model start it once, running requests finish before a stop, and a model whose state gw cannot tell gets no traffic.

Off for now. While lifecycle.enabled is false, gw starts and stops nothing: it routes to what is already running and records what it would have stopped. The switch is checked twice, in the manager and in the systemd call. I have not turned it on.

For agents. An MCP server at /mcp lets agents list models, ask for one, release it and read usage; the README lists every tool.

Reaching it from anywhere

A Cloudflare tunnel carries one hostname to gw’s port 4001. The model API has two locks: Cloudflare Access at the edge, gw’s own key at the origin. The owner’s Access login opens the panel without a gw key, but not the model API.

The setup on 26 November, in order:

  1. A named tunnel, gw, whose single rule sends models.gurkanozkan.ai to 127.0.0.1:4001; everything else gets a 404.
  2. An Access application on that hostname. The owner signs in by email with a one-time PIN; machines send a service token.
  3. A check before exposure: an unauthenticated request had to be redirected (302) to the Access login. Only then was the connector started, as a systemd user unit.
  4. gw’s side: every remote request must carry a valid Access token, which gw verifies itself, and requests to /v1, /m and /mcp a gw key as well.

The live test that day:

Request through the tunnel, 26 NovemberResult
No Access token302 to the login
Access token, no gw key401
gw key, no Access token302
Both200
MCPthe 8 tools of that day
/metrics with an infer key403
A revoked key401

Three details needed a second look:

  • The first DNS route went to another, older tunnel, because the default config file named it. An explicit --config gw.yml with --overwrite-dns fixed it.
  • cloudflared keeps idle connections for 90 s, and gw closed them after 15 s: an occasional 502. gw now keeps them 120 s, the tunnel 60.
  • Cloudflare blocks Python’s default User-Agent, Python-urllib/3.12, with a 403 (error 1010). gw’s examples send their own.

The 100-second limit

Cloudflare cuts a request with a 524 when the origin sends nothing for 100 seconds. On the Free, Pro and Business plans that cannot be changed.

A long prompt hits it with nothing broken: when I sent a 256k-token prompt to Inkling from my laptop, the model was still reading it at 100 seconds, and the laptop got a 524. The figure at the top of this page shows both fixes.

Streaming. gw sends the headers at once, then a comment line every 15 seconds until the first token. Streaming clients skip comment lines. The live check on 27 November: a 388,745-token prompt through the tunnel got its first token at 286 s, and the answer was correct.

Not streaming. gw waits 30 seconds for the model. If nothing comes, it sends a 200 and then one space every 15 seconds, which JSON allows before the body. The live check: a 245,206-token prompt got its headers at 31 s, then 9 spaces, then a correct answer at 156.6 s.

The price: once gw has sent 200, an error also arrives with status 200, so a client must check the body for error. Streaming is still the better choice.

What the reviewers found

Nothing went out through the tunnel until the findings of the last review before exposure were fixed. The first review counted 0 critical, 1 high and 9 medium findings. Each entry below is the attack, what it did, and the fix.

A path with .. in it. The attack was /m/x/ws/../v1/chat/completions. ComfyUI’s /ws paths are exempt, so that a browser tab watching ComfyUI cannot start a model, and gw saw /ws. The HTTP library then collapsed the .., and a chat request reached the model with no slot, counter or record. The fix checks the path the model will receive and refuses ...

A web page on the same machine. Through DNS rebinding or a cross-site request, a page open in a browser could drive port 4000. The fix: the Host must be local, a foreign Origin is refused, and JSON endpoints require a JSON content type.

A huge body. A 100 MB JSON body made gw use about three times that in memory, which the models need. Bodies are now capped, charged to a budget and not copied; the same body costs about its own size.

A nosy key. A restricted remote key could list every model and the names of other keys. Now a key sees only its own models, and a model it may not use answers 404, like one that does not exist.

Bodies promised, never sent. Sixteen connections each declared a 32 MiB body and sent nothing; 80 of 80 normal requests then got a 503. Memory is now charged as bytes arrive and a silent body times out. Afterwards 79 of 80 got 200, and the stalled connections got 408 at 30 s.

A slow drip. One key sending a byte every 25 seconds held all of a model’s slots, so a single leaked key could lock everyone out of Inkling. Now one key holds at most half of a model’s slots, and an upload must keep a minimum speed. Two colluding keys could still fill every slot, so uploads together may hold only half.

A session flood. 5,000 MCP initialize calls from one key raised gw’s memory by about 300 MB. Sessions are now capped, time out and are rate-limited; the same flood adds 5 MB.

A browser login on the model API. The owner’s browser login could start a model with a plain GET to /m/…. Browser logins now open the panel and /api only.

An expensive comparison. After gw went live, a benchmark comparison with 4,000 repeated ids used 96 s of CPU; the ids are now counted before anything is read. A remote key could also read the expected answers of custom benchmarks; they are now hidden.

What none of them found. A way to start or stop a model while the lifecycle is off. An open route on the remote port: 22 of 22 probed routes answered 401. A forged admin: all 31 forged Access tokens, unsigned and expired ones among them, were refused.

What the panel shows

The panel at /ui is built into the binary, with no build step and no CDN. The sessions view and the performance strip exist because I asked to see sessions, the cache hit rate, and prefill and decode speeds directly.

The gw panel's Overview tab: performance tiles, a memory bar with Inkling as the largest slice, a table of three models and a table of agents

The live panel's Overview on 28 November 2026, opened read-only without an owner session: the performance strip over the last 24 hours, the memory bar, the three models and the agents seen in a day. The badge at the top says the lifecycle is disabled. The events are restarts made outside gw during a serving test; gw only recorded them. One model's internal name is covered.

Every request is tied to an agent, by its key, a header, its MCP name or the local process behind the connection, and grouped into conversations. No prompt or answer is stored.

The gw panel's Sessions tab: a table of conversations, and below it one session's turns as bars of prompt size with a time-to-first-token line that falls after the first turn

The Sessions tab on 28 November 2026 with one conversation opened: prompt size per turn with its cached part, and time to first token. The conversation comes from the serving test's experimental drafter variant, an A/B configuration rather than the production setup, and has 8 turns: 6 plus 2 measurement turns. The list is cropped after seven rows.

Per-request cache numbers needed one vLLM flag, --enable-prompt-tokens-details, turned on on 27 November. A repeated 11k prompt then showed 11,072 of 11,129 tokens cached, and its first token came after 1.5 s instead of 14.8 s.

One hour of mostly serving experiments on 27 November, as gw perf --window 1h summed it:

MeasureValue
Requests127, with 1 error
Prompt tokens7.0M
Cache hits86.4%, or 88.5% where requests report it
Decode speed42.2 tok/s on average
Time to first token1.9 s at the median, 10.8 s at p95
KV cachepeak 48%, 0 preemptions

The gw panel's Connect tab: the tunnel address, the four values a client needs, the model names and roles, notes on long prompts, and copyable examples

The Connect tab's through-the-tunnel view on 28 November 2026. The examples carry placeholders where the gw key and the Access token go; gw stores only digests of keys, so it has none to show. One model's internal name is covered.

What Inkling behind it handled

A serving test on 27 and 28 November ran four coding agents against Inkling, each with about 240k tokens of context from real source repositories, growing to about 270k over six turns. They answered 40 of 40 retrieval questions and finished 4 of 4 coding tasks. Warm turns found 97.7% of their prompt in the prefix cache, with 0 preemptions.

The cold start is the weak part: prefills run one at a time, so the fourth agent waits about 10 minutes, and warm turns still take 10.5 to 14 s to the first token at the median. A client rule came out of it: keep reasoning_effort constant within a conversation, because changing it drops the cache.

If you build one

  • Keep the model servers as systemd units the gateway does not own, so they outlive a gateway restart.
  • Tell remote traffic apart by the port it arrives on, not by a header; the tunnel’s connector also comes from 127.0.0.1.
  • Behind Cloudflare, a long prompt needs bytes before 100 seconds: stream, or send a heartbeat.
  • Only append to an agent’s context; an edit in the middle means the whole prompt is read again.
  • Have a separate agent attack every round, and fix what it finds before anything is exposed.

Where it stands

So far gw makes this machine a provider for one model. Inkling is served live through gw, on the machine and through the tunnel, with a key per machine.

The image and speech models sit in the same catalogue, behind the same door, but on 28 November they were not being served: ComfyUI listened on another address, and the speech unit had failed. The OpenAI-style image endpoint is not built. Neither is the easy model adding I asked for (gw add, pull, calibrate), and with the lifecycle switch off, the choice of which model to stop is still made outside gw.

The code, with the design and every brief in Turkish, is public at github.com/ozkangrk/gw under Apache-2.0: a cleaned copy in one commit, without the names of other private projects or machine-specific paths.

Sources

I asked for gw and made the decisions named here. Claude, working in Claude Code, wrote the design and the task briefs, Claude subagents wrote all of the code, and separate Claude subagents did the reviews. Claude also drafted this essay from the repository’s records and took the screenshots from the live panel; a separate Claude session checked its facts against those records, and the essay was published at my request on 28 November 2026 and revised for readability, also at my request, the same day.