bots-garden/razorbillpublic Fork
⑂ v0.2.0
Commits
⬇ Clone ▾
git clone https://git.rickub.com/bots-garden/razorbill.git
git clone ssh://git@rickub.com/bots-garden/razorbill.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

Download ZIP
architecture.md · 55 lines · 5.6 KBmarkdown
Blame HistoryOpen raw

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

  1. Hummingbird receives POST /v1/chat/completions. Middlewares log it, apply CORS, check the API key, and turn any error into OpenAI's error body.
  2. The body is decoded and validated into a ChatPlan; JSON Schemas are re-read to keep their key order. With response_format, an instruction carrying the schema is added to the system message.
  3. 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).
  4. 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.
  5. The events become OpenAI pieces: stop sequences are applied, reasoning is separated or hidden, tool calls are numbered, and finish_reason and usage are computed.
  6. 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): rzb first 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

 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
# 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](../../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](../how-to/develop-from-a-sandbox.md)). 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](openai-compatibility.md)).

## The path of a chat request

1. Hummingbird receives `POST /v1/chat/completions`. Middlewares log it, apply CORS, check the API key, and turn any error into OpenAI's error body.
2. The body is decoded and validated into a `ChatPlan`; JSON Schemas are re-read to keep their key order. With `response_format`, an instruction carrying the schema is added to the system message.
3. 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](model-lifecycle.md)).
4. 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](model-lifecycle.md#reusing-the-previous-prompt)), and streams events.
5. The events become OpenAI pieces: stop sequences are applied, reasoning is separated or hidden, tool calls are numbered, and `finish_reason` and `usage` are computed.
6. 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): `rzb` first 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](model-lifecycle.md).
- How JSON and tool calls are guaranteed: [constrained decoding](constrained-decoding.md).
- How reasoning is separated from answers: [reasoning models](thinking.md).