A coding agent with the model inside it.
Saetta is a C port of the pi coding agent that runs Qwen3.8, Inkling 2 Mini and Ornith-1.5 in its own process, on its own hand-written Metal inference engine. No Python, no Node, no server: the agent and the model share one process, and the engine is built for agent work: long shared prompts, short answers, tool calls.
# install (into ~/.local/bin), then start in a project
curl -fsSL https://saetta.dev/install.sh | sh
cd ~/src/my-project && saetta start code
saetta start code edits a 3D game: it adds six crystals along the lake shore, probes the terrain for the shoreline and checks the script. Cut for length; Qwen3.8-Flash-Next Coder on an M5 Max.
This Space is a page about Saetta. It runs on your Mac, not on Hugging Face hardware: the engine is Metal only.
The idea: one process, not an agent talking to a server
The usual setup sends the whole conversation as text over HTTP to an inference server on every turn, and the server guesses from token prefixes what it can keep. Saetta runs the agent and the engine together, so the agent tells the engine what it knows: which conversation this is, where it forks, which tool is still running, which files it just read.
State that survives restarts
Each turn's conversation state is saved to the SSD and restored on resume: a resumed session reads only its new message.
15-turn Coder session with restarts, a fork and a retry.
Checkpoints at message starts
A fork, an edit or a retry restarts at the last message it shares with the conversation; subagents start at their parent's fork point.
The same session and agent through saetta serve over HTTP; the gap is mostly the fork, the retry and the restarts.
Tool output read while the tool runs
While a command streams, the engine reads what the next request will contain, so the answer starts as the command ends.
Median first token after a tool result, streaming workload.
Effort where it matters
The agent reasons in full on the turn you start, after a failed tool call and after a repeated one, and skips it after routine tool results.
30 coding tasks, both 30 of 30, same engine and model.
The engine
C23 and hand-written Metal, one binary for every model
No framework, no graph and no Python: the host is C23 (Apple clang, strict floating point, no fast-math), the kernels are hand-written Metal, and the whole thing, engine, server and agent, is one 3 MB binary. Each model is its own file with its shapes as compile-time constants generated from the model's config.json; there is no runtime shape logic.
- Weights read in place. The Hugging Face MLX packs (4-bit, mixed 4/5/6/8-bit, NVFP4/MXFP8 experts) are memory-mapped from the downloaded files and handed to the GPU without a conversion step; GGUF fine-tunes are imported into a pack once (
saetta import), their MTP layer included. - M5 neural accelerators. Prompt reading runs its matmuls on Metal tensor ops: quantized weights unpacked in registers, half-precision activations, f32 sums. Routed experts, attention and the recurrent scan all have prompt-chunk kernels of their own.
- M1 to M4. The same model runs on simdgroup kernels on the GPU cores (
SAETTA_NO_ACCEL=1forces them): same output, slower prompt reading.
- Fits the Mac it runs on. At start the engine measures memory, keeps the weights and an 8 GB reserve, and sizes the context and the checkpoints to what is left; a pack that does not fit is refused, or (experimental) streams its cold experts from the SSD with the hot ones resident.
- Decode at batch 1 is bandwidth bound. The per-token path allocates nothing, takes no lock and makes no syscall; the router's softmax and top-k run on the GPU, so experts are picked without a round trip to the CPU.
- Built for agent turns. Long shared prompts, short answers and tool calls: the engine keeps conversations, reads prompts in large chunks and speculates on the text an answer is likely to copy.
The models it is built for
Hybrid attention: most layers carry a fixed-size state
The target models mix a few attention layers with many recurrent or windowed ones. Only the attention layers grow a KV cache with the context; the rest hold a state of fixed size. That is what makes a conversation cheap to snapshot, copy and save, and the engine is designed around it.
Qwen3.8-Flash-Next: 48 layers
512 routed experts with 10 active plus a shared one per token; every fourth layer is attention whose indexer picks, per query, the 2,048 most relevant past tokens in blocks of 4; four hyper-connection residual streams; a 20M-row n-gram embedding table (32 to 38 GB in the packs) that the CPU reads row by row from the mapped file and adds at layer 1; an MTP head that drafts the next tokens. The Coder pack keeps the 256 experts that matter for code.
Qwen3.8-27B: 64 layers, dense
The same three-to-one pattern of Gated DeltaNet and full attention, dense FFNs; LoRA adapters at run time; GGUF fine-tunes with their MTP draft heads.
Inkling 2 Mini: 24 layers
128 routed experts, 8 active; the sliding layers keep ring buffers of the last 1,024 positions, so only 4 layers grow with the context: up to 1M tokens where memory allows.
KV cache and conversation state
The engine keeps conversations, not just a prefix cache
A conversation is its KV cache plus the recurrent state at a position. Saetta keeps several per model and knows where each one forks, so a new request almost never reads its history again.
- KV cache in f16 (f32 on request), allocated for the context in use and grown in steps: about 35 KB per token on Flash-Next (12 attention layers) and 64 KB on the 27B. Contexts up to 262K tokens, 1M on Inkling.
- Restore points. Each conversation keeps the state at the end of its last prompt and at the end of its last reply: the next request that extends either continues there in place.
- Checkpoints at message starts. While a prompt is read, the engine ends a chunk at message starts at least 1,024 tokens apart and copies the recurrent state there (118 MB on Flash-Next, 157 MB on the 27B, 172 MB on Inkling). Up to 16 per conversation, recent ones dense and older ones spaced out, 3 kept while a conversation is idle. A fork, an edit or a retry restarts at the last one it shares.
- Shared starts. Up to 4 conversations stay in memory. A request that leaves another conversation's tokens gets its own conversation, which starts from a copy of the other's rows and state at the checkpoint: subagents start at their parent's fork point, a second session at the first one's system prompt and tools.
- State on the SSD. With the model in the agent, each turn's state is written to
~/.saetta/statein the background and restored on--continue,/resumeand/fork: a resumed session reads only its new message. - Exact restarts. A restored or copied state is the state the engine computed at that position, bit for bit, and prompt chunks are cut so that where they end does not change what they compute: on the Qwen models any chunk of 10 tokens or more computes the same bits wherever it ends (measured); on Inkling a chunk does when it ends at a multiple of 256, so its checkpoints sit there.
Prompt reading and decoding
Read ahead, then speculate on what the answer copies
- Large prompt chunks. Flash-Next reads prompts in chunks of up to 4,096 tokens (Inkling 2,048), each one command buffer, with the next chunk's inputs (embeddings, n-gram rows) prepared on 8 CPU threads while the GPU runs.
- Tool output read while the tool runs. While a command streams, the engine reads the output so far into the conversation in chunks of up to 2,048 tokens, yielding to any request that arrives; when the command ends, the next request finds most of it read.
- Per-turn effort. The agent asks for full reasoning on the turn you start, after a failed tool call and after a repeated one, and for none after routine tool results, through a thinking budget the engine enforces.
- Two draft sources. The model's MTP head drafts the next tokens; prompt lookup drafts from earlier text that matches what is being written. The agent feeds the lookup the files it read, edited or wrote, so an edit that copies a block of a file is written several tokens per step.
- Priced speculation. A controller measures the time of a verification round per draft length and how often drafts are accepted per match length, and runs a lookup window only when it beats an MTP round. Verification chunks carry up to 8 tokens.
- Same output. Greedy decoding with speculation gives the target model's tokens; sampled requests use speculative sampling, which keeps the model's distribution. Tool calls can be decoded under a grammar of the request's tools (opt-in).
The agent, pi and LoRA
A port of pi in C, and adapters trained to drive it
- Saetta Code is pi 1.5.2 in C. pi's tools (
read,bash,edit,write, andgrep/find/ls), system prompt, skills,AGENTS.md, session files and terminal UI. Its requests and session files match pi's byte for byte on 63 recorded scenarios; it shares pi's configuration folder, so either agent continues the other's sessions. - Integration in process. The agent tells the engine its session, where its subagents fork, which command is still streaming and which files it touched. MCP servers over stdio and HTTP, subagents that share the loaded model, print, JSON and rpc modes.
- Other agents too. The same engine serves Claude Code, pi and opencode through OpenAI- and Anthropic-compatible APIs:
saetta start claude,saetta start pi,saetta start opencode.
- LoRA at run time. PEFT adapters on Qwen3.8-27B, applied per request with vLLM's flags: attention and MLP projections, rank a multiple of 8 up to 128, rsLoRA. Each adapter is checked against the base model before it loads (base, rank, target modules, DoRA, bias).
- The pi adapter. harness-hub/Qwen3.8-27B-pi-adapted is trained to drive pi (its card: SWE-bench Pro hard 39.2% to 64.7% over the base model). Saetta Code runs it in process:
saetta download 27b-8bit && saetta download 27b-pi saetta start code 27b-8bit --lora-modules qwen3.8-pi=27b-pi # the agent, the model and the adapter in one process saetta start pi --pack 27b-8bit --lora-modules qwen3.8-pi=27b-pi # or pi itself on the same engine
Correctness first
Every model is checked against a reference, every change against it again
- A numpy oracle per model. Golden prompts of up to 32K tokens are run through an independent numpy reference written from the model's reference code; the engine must match it layer by layer (relative error 5e-5) and on the logits, and its half-precision prompt path is gated on the KL of the output distribution (at most 1e-3). One golden per model family runs on every engine commit, every golden on both the accelerator and the simdgroup kernels before each release.
- Kernels against scalar references. Each Metal kernel has a scalar C reference and a test on real tensors from the packs.
- Exactness rules for every cache. A restored or copied state must equal the state the engine computed at that position, bit for bit; tests compare restarts, copies across conversations and speculative rounds with plain reads.
Measured on an M5 Max, 128 GB
Every number is a measured run, greedy, on the Qwen3.8-Flash-Next Coder pack unless the chart says otherwise. Charts mark the best result; the notes say where Saetta is not ahead.
Where Saetta is not ahead: Strata is faster on a single code edit with the full Flash-Next pack (68.5 s against 86.4 s; it decodes at 103.6 tokens/s against 93.4). The other engines run different weights where no conversion of the same weights exists (Strata and llama.cpp on GGUF). M1 to M4 run the same kernels on their GPU cores but are not measured on real hardware yet.
Models and Macs
saetta start code picks the pack that fits the Mac's memory; /model in the agent downloads and switches. Weights are read in place from their Hugging Face repos.
| pack | weights | Mac memory | notes |
|---|---|---|---|
coder | Qwen3.8-Flash-Next Coder, 256 of 512 experts | 96 GB | the default from 96 GB; draft head |
full | Qwen3.8-Flash-Next, 4-bit | 128 GB | draft head |
27b-4bit, 27b-8bit | Qwen3.8-27B | 32 GB, 48 GB | LoRA adapters on 8-bit; coding fine-tunes with draft heads |
inkling | Inkling 2 Mini, 26B, 3.4B active | 32 GB | context up to 1M tokens where memory allows |
ornith-35b, ornith-122b | Ornith-1.5 (ornith-ai) | 32 GB, 96 GB | first version |
The same engine serves other agents too: saetta start claude, saetta start pi and saetta start opencode run them against a local OpenAI- and Anthropic-compatible server.
/monitor in Saetta Code: speed, prompt progress, memory and conversations, live.