# Show HN: AirMind – Zig and Metal Qwen4-35B engine and local agent for 16GB Macs

> Source: <https://github.com/fxsjy/AirMind>
> Published: 2026-12-06 06:42:00+00:00

**Key takeaway:** AirMind runs Qwen4-35B-A3B on a 16 GiB Mac by keeping a 10.6 GiB UD-IQ2_XXS model fully resident in unified memory (25–36 tok/s decode) or streaming 4-bit weights through a 2-slab GPU hot-expert LRU cache (14–18 tok/s decode), with a disk-persisted KV+GDN prefix cache restoring 1,100+ tokens in ~54

**Zero-dependency Zig + Apple Metal GPU inference engine and built-in local coding agent for Qwen4-35B-A3B — engineered to run fast on a 16 GiB MacBook.**

AirMind compiles to a single native binary with **zero external runtime dependencies** (no Python runtime required for inference, no llama.cpp, no PyTorch, no HTTP hop between the agent and the model). Just run `./zig-out/bin/airmind` to launch a resident in-process coding & web agent powered by custom Metal shaders.

- **From-Scratch Zig 0.17.1 + Native Apple Metal GPU**  - Custom `simdgroup_matrix` hardware GEMM prefill, 0-barrier warp QSA attention, 128-TG in-register GDN recurrence, and adaptive expert-grouped MoE.
  - Full 13-format GGML quantization support (`F32` ,`F16` ,`BF16` ,`Q8_0` ,`Q4_K` ,`Q5_K` ,`Q6_K` ,`IQ4_NL` ,`IQ4_XS` ,`IQ3_XXS` ,`IQ2_S` ,`IQ2_XS` ,`IQ2_XXS` ), bit-exact against`libggml` .
- Custom 
- **Built-In In-Process Coding & Web Agent (Default Mode)**  - Bare `./zig-out/bin/airmind` launches directly into an interactive coding agent with**zero HTTP overhead** between the agent loop and the GPU engine.
  - **9 built-in tools** :
    - Auto-run read-only: `list_files` ,`read_file` ,`grep_files` ,`web_search` (no API key),`web_fetch` ,`plot_chart` (Unicode sparkline + bar chart).
    - Interactive approval (`[y]es / [n]o / [a]lways` or`--yolo` ):`write_file` ,`edit_file` ,`run_shell` .
  - Auto-run read-only: 
  - **UTF-8 / CJK-aware terminal line editor** : full Left/Right/Home/End cursor navigation, mid-line insert/delete across wrapped Chinese + English text, Up/Down history, live prefill/tool-generation progress bars, and automatic two-stage context compaction.
- Bare 
- **Instant Startup & Speculative Acceleration**  - **Disk-persisted KV+GDN prefix cache** (`/tmp/airmind_prefix_*.zqs` ): restores 1,100+ system + tool tokens in**~54 ms** on repeat launches (~1s cold boot to ready).
  - **Zero-RAM Stochastic N-gram Speculative Decoding** : exact Leviathan rejection sampling compatible with`temperature` ,`min_p` , and`repeat_penalty` — accepts up to**~40% free tokens** when generating code & unit tests with**0 MiB** extra weights.
- **Fits 16 GiB Apple Silicon Without Swap**  - **IQ2 Resident Mode (default on ≤18 GiB Macs)** : 10.6 GiB`UD-IQ2_XXS` fully resident in unified memory (`250–300 tok/s` prefill,`25–36 tok/s` decode).
  - **4-Bit Streaming Mode (`--iq4`)** : explicit 2-slab GPU Hot-Expert LRU Cache + 8-thread` F_NOCACHE` NVMe streaming (`14–18 tok/s` decode,`70–120 tok/s` prefill on 16 GiB RAM, zero desktop lag).
  - **Optional Loop4-HC v3.0 Recurrent Depth (`--smart` / `--deep`)** : zero-LoRA 34.8 MB BF16 Depth-AttnRes sidecar looping` L12..27` (`T=2/3` , Prompt-Split keeps full prefill speed) + bounded`--think-budget` .
- **OpenAI-Compatible HTTP Server (`airmind serve`)**  - Full SSE streaming, OpenAI `tools` /`delta.tool_calls` /`role: "tool"` , and multi-conversation KV+GDN state snapshotting.
- Full SSE streaming, OpenAI 

- **macOS on Apple Silicon** (M1/M2/M3/M4, 16 GiB+ Unified Memory recommended; pure-Zig CPU fallback included)
- **Zig `0.17.1`** (` brew install zig` )

```
# One command: builds ReleaseFast binary + auto-downloads optimal model for your Mac's RAM
# (<=18 GiB RAM -> IQ2_XXS + Loop4-HC sidecar; >=24 GiB RAM -> 4-bit IQ4_XS + Loop4-HC sidecar)
zig build setup

# Launch AirMind Local Coding Agent (zero flags needed!)
./zig-out/bin/airmind
```

Other build & setup options:

```
zig build setup -Dmodel=iq2      # Force 2-bit full-resident model + Loop4-HC sidecar
zig build setup -Dmodel=iq4      # Force 4-bit UD-IQ4_XS (hardlinks Shard 2 if IQ2 exists)
zig build setup -Dmodel=all      # Download both IQ2 and IQ4 + Loop4-HC sidecar
zig build                        # Offline build only (defaults to ReleaseFast)
zig build test                   # Run full unit test suite (Debug safety checks)
# Default: Built-in Local Coding Agent (auto-detects Apple Silicon RAM/cores & local models)
./zig-out/bin/airmind

# Extra reasoning presets
./zig-out/bin/airmind --smart            # Loop4-HC v3.0 T=2 Prompt-Split (higher IQ, full prefill speed)
./zig-out/bin/airmind --deep             # Loop4-HC T=2 + 320-token bounded <think> chain
./zig-out/bin/airmind --iq4              # True 4-bit UD-IQ4_XS with GPU Hot-Expert LRU Cache + NVMe streaming
./zig-out/bin/airmind --yolo             # Auto-approve write_file / edit_file / run_shell
./zig-out/bin/airmind --no-agent         # Plain interactive chat REPL without tools

# OpenAI-compatible HTTP Server (SSE + tool calling + multi-slot KV/GDN cache)
./zig-out/bin/airmind serve --port 8080 -c 16384

# Single-turn CLI chat / raw completion
./zig-out/bin/airmind chat -p "Explain Fermat's Little Theorem briefly"
./zig-out/bin/airmind --help
```

| Command | Action | 
|---|---|
| `/agent` | Toggle built-in agent tools ON / OFF | 
| `/yolo` | Toggle auto-approval for `write_file` ,`edit_file` ,`run_shell` | 
| `/think` | Toggle Qwen4 `<think>` reasoning mode | 
| `/compact` | Compact older tool outputs / conversation history | 
| `/clear` | Clear conversation & instantly restore disk-cached system prompt | 
| `/stats` | Show context usage and last-turn speed | 
| `/quit` | Exit | 

| Mode | Memory Strategy | Prefill | Decode | Notes | 
|---|---|---|---|---|
| `UD-IQ2_XXS` (default) | 10.6 GiB RAM resident | 210–300 tok/s | 28–36 tok/s | Zero SSD I/O; ~40% free tokens on code via 0-MiB N-gram spec | 
| `UD-IQ2_XXS --smart` | 10.7 GiB RAM resident | 200–290 tok/s | 15–22 tok/s | Loop4-HC `T=2` Prompt-Split (+33% on 10-problem math/logic bench) | 
| `UD-IQ4_XS --iq4` | 1.94 GiB dense + 8.7 GiB GPU LRU | 74–120 tok/s | 15–18 tok/s | Soft-threshold 4-bit streaming, zero swap on 16 GiB | 
| Repeat agent startup | Disk KV+GDN snapshot (85 MiB) | **54 ms** restore | — | Cold boot to interactive `Agent>` in ~1s | 

```
src/
├── main.zig           # CLI entry, Apple Silicon hardware probe, auto-config presets
├── root.zig           # Library module exports & test root
├── gguf.zig           # Zero-dependency multi-shard GGUF v3 parser + mmap
├── quant.zig          # Pure-Zig CPU dequant & SIMD dot products (13 GGML formats)
├── quant_tables.zig   # IQ2 / IQ3 / IQ4_NL / IQ4_XS lookup tables
├── tokenizer.zig      # Native BPE tokenizer + Qwen4 special tokens
├── sampler.zig        # 8-wide SIMD sampler (min_p, top_p, repeat_penalty, Leviathan spec verify)
├── qwen4.zig          # Qwen4-35B-A3B architecture (GDN, QSA, Engram PLE, HC, MoE) CPU reference
├── metal.zig          # Apple Metal GPU engine, batched prefill, state snapshots, MTP/N-gram spec
├── qwen4.metal        # Custom Metal compute shaders (simdgroup_matrix, fused GDN/QSA/MoE/Loop)
├── metal_bridge.h/.m  # Minimal Objective-C ARC Metal C-ABI bridge
├── expert_cache.zig   # O(1) 2-slab GPU Hot-Expert LRU Cache + 8-thread F_NOCACHE NVMe streamer
├── loop4hc.zig        # Zero-dependency safetensors parser & Loop4-HC v3.0 Depth-AttnRes loader
├── agent.zig          # Built-in 9-tool local coding, web search/fetch & terminal chart agent
└── server.zig         # In-process Agent REPL (UTF-8/CJK line editor, auto-compact) + OpenAI HTTP server
```


