Architecture — explanation
What is this about?
razorbill is one Swift package that produces one executable, rzb, a model server: it loads MLX models on the Mac's GPU and exposes them through an OpenAI-compatible HTTP API. Inside, the code is split into five modules with a strict direction of dependencies. This page explains that split, the path a request takes through it, and a few decisions that shape how the project is built.
The diagram docs/diagrams/packages.drawio shows the modules, their dependencies and the third-party packages.
The modules
| Module | Role | Depends on |
|---|---|---|
RazorbillCore |
The engine protocol and its events, generation settings, output constraints, the server configuration file (loading, validation), the model catalog and cache, the fake engine. | Yams |
RazorbillMLX |
The MLX engines: chat sessions, raw completions, thinking detection and budget, constrained decoding (XGrammar), embeddings. | Core, mlx-swift-lm, mlx-swift, swift-huggingface, swift-transformers (macOS only) |
RazorbillServer |
The HTTP server: routes, OpenAI request and response types, streaming, errors, authentication, the model manager. | Core, Hummingbird, swift-service-lifecycle |
RazorbillCLI |
The commands (serve, config validate, models, version). |
Core, Server, swift-argument-parser, swift-log |
rzb |
The executable: registers the engines and starts the command line. | CLI, MLX |
Everything points towards RazorbillCore, and RazorbillCore knows nothing about MLX, HTTP or command-line parsing. RazorbillServer does not know MLX either: it talks to engines only through the protocol.
Why it is split this way
Everything but MLX must build and be tested without a GPU. MLX needs Metal, which exists only on Apple platforms. Keeping it out of the core and the server means the configuration, the HTTP layer, the OpenAI types, streaming, the model manager and the command line build and are tested on Linux, where most of the development happens (see develop from a Linux sandbox). On Linux, RazorbillMLX is empty and rzb has only the fake engine, which answers echo: … — enough to test the whole server, and to run the OpenAI compatibility suite against its protocol.
Engines are replaceable. The server asks an InferenceEngine to load a model, open a session, send messages and stream events (text, reasoning, tool calls, statistics), to continue a raw prompt, or to embed texts. MLX is one implementation; another runtime would be one more module behind the same protocol.
The OpenAI layer is thin and strict. Request types decode the OpenAI JSON, check every field, and produce a plan (messages, settings, output constraint). Fields razorbill cannot honour are refused with a 400 naming them rather than silently ignored (see OpenAI compatibility).
The path of a chat request
- Hummingbird receives
POST /v1/chat/completions. Middlewares log it, apply CORS, check the API key, and turn any error into OpenAI's error body. - The body is decoded and validated into a
ChatPlan; JSON Schemas are re-read to keep their key order. Withresponse_format, an instruction carrying the schema is added to the system message. - The model manager resolves the name (declared id, or Hugging Face id), loads the model if needed, and gives the request a lease: one generation per model at a time, the others wait in a queue (see model lifecycle).
- The engine opens a fresh session with the request's settings and constraint, renders the conversation with the model's chat template, reuses the part of the KV cache the previous request left that matches it (why), and streams events.
- The events become OpenAI pieces: stop sequences are applied, reasoning is separated or hidden, tool calls are numbered, and
finish_reasonandusageare computed. - The pieces are gathered into one
chat.completion, or sent as Server-Sent Events as they come. When the answer ends — or the client disconnects — the lease is released.
The server is stateless: each request carries the whole conversation, and nothing is kept between requests but the loaded models.
Why the Mac build uses xcodebuild
MLX ships Metal shaders that must be compiled into a default.metallib. SwiftPM's swift build cannot compile Metal, so a binary built that way fails with Failed to load the default metallib as soon as a model loads. xcodebuild compiles them into mlx-swift_Cmlx.bundle, which must stay next to rzb. On Linux there is no MLX, so plain swift build / swift test work there.
Rejected alternatives
- A local agent (version 0.1):
rzbfirst ran one agent from a YAML file in the terminal, with its own tools. It was replaced by a server, because an OpenAI-compatible API lets every existing client, framework and agent use the Mac's GPU, instead of one built-in front-end. - Ollama's API (
/api/chat…) next to OpenAI's: two APIs to keep compatible for one benefit; OpenAI's is what frameworks speak. - Vapor instead of Hummingbird: Hummingbird 2 is smaller, built on structured concurrency, and enough for six endpoints.
- Python (mlx-lm's server): razorbill is a single native binary, with no Python environment to install.
How it relates to the rest
- How models are loaded, shared and unloaded: model lifecycle.
- How JSON and tool calls are guaranteed: constrained decoding.
- How reasoning is separated from answers: reasoning models.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
|