Metadata-Version: 2.4
Name: local-lattice
Version: 0.3.2
Summary: Local Lattice — MLX-native OpenAI-compatible gateway with capability routing, admission queue, and a hybrid local+cloud swarm.
Project-URL: Homepage, https://github.com/chrisswimlee/local-lattice
Project-URL: Repository, https://github.com/chrisswimlee/local-lattice
Project-URL: Issues, https://github.com/chrisswimlee/local-lattice/issues
Project-URL: Changelog, https://github.com/chrisswimlee/local-lattice/blob/main/CHANGELOG.md
Author: The Local Lattice Authors
License:                                  Apache License
                                   Version 2.0, January 2004
                                http://www.apache.org/licenses/
        
           TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
        
           1. Definitions.
        
              "License" shall mean the terms and conditions for use, reproduction,
              and distribution as defined by Sections 1 through 9 of this document.
        
              "Licensor" shall mean the copyright owner or entity authorized by
              the copyright owner that is granting the License.
        
              "Legal Entity" shall mean the union of the acting entity and all
              other entities that control, are controlled by, or are under common
              control with that entity. For the purposes of this definition,
              "control" means (i) the power, direct or indirect, to cause the
              direction or management of such entity, whether by contract or
              otherwise, or (ii) ownership of fifty percent (50%) or more of the
              outstanding shares, or (iii) beneficial ownership of such entity.
        
              "You" (or "Your") shall mean an individual or Legal Entity
              exercising permissions granted by this License.
        
              "Source" form shall mean the preferred form for making modifications,
              including but not limited to software source code, documentation
              source, and configuration files.
        
              "Object" form shall mean any form resulting from mechanical
              transformation or translation of a Source form, including but
              not limited to compiled object code, generated documentation,
              and conversions to other media types.
        
              "Work" shall mean the work of authorship, whether in Source or
              Object form, made available under the License, as indicated by a
              copyright notice that is included in or attached to the work
              (an example is provided in the Appendix below).
        
              "Derivative Works" shall mean any work, whether in Source or Object
              form, that is based on (or derived from) the Work and for which the
              editorial revisions, annotations, elaborations, or other modifications
              represent, as a whole, an original work of authorship. For the purposes
              of this License, Derivative Works shall not include works that remain
              separable from, or merely link (or bind by name) to the interfaces of,
              the Work and Derivative Works thereof.
        
              "Contribution" shall mean any work of authorship, including
              the original version of the Work and any modifications or additions
              to that Work or Derivative Works thereof, that is intentionally
              submitted to Licensor for inclusion in the Work by the copyright owner
              or by an individual or Legal Entity authorized to submit on behalf of
              the copyright owner. For the purposes of this definition, "submitted"
              means any form of electronic, verbal, or written communication sent
              to the Licensor or its representatives, including but not limited to
              communication on electronic mailing lists, source code control systems,
              and issue tracking systems that are managed by, or on behalf of, the
              Licensor for the purpose of discussing and improving the Work, but
              excluding communication that is conspicuously marked or otherwise
              designated in writing by the copyright owner as "Not a Contribution."
        
              "Contributor" shall mean Licensor and any individual or Legal Entity
              on behalf of whom a Contribution has been received by Licensor and
              subsequently incorporated within the Work.
        
           2. Grant of Copyright License. Subject to the terms and conditions of
              this License, each Contributor hereby grants to You a perpetual,
              worldwide, non-exclusive, no-charge, royalty-free, irrevocable
              copyright license to reproduce, prepare Derivative Works of,
              publicly display, publicly perform, sublicense, and distribute the
              Work and such Derivative Works in Source or Object form.
        
           3. Grant of Patent License. Subject to the terms and conditions of
              this License, each Contributor hereby grants to You a perpetual,
              worldwide, non-exclusive, no-charge, royalty-free, irrevocable
              (except as stated in this section) patent license to make, have made,
              use, offer to sell, sell, import, and otherwise transfer the Work,
              where such license applies only to those patent claims licensable
              by such Contributor that are necessarily infringed by their
              Contribution(s) alone or by combination of their Contribution(s)
              with the Work to which such Contribution(s) was submitted. If You
              institute patent litigation against any entity (including a
              cross-claim or counterclaim in a lawsuit) alleging that the Work
              or a Contribution incorporated within the Work constitutes direct
              or contributory patent infringement, then any patent licenses
              granted to You under this License for that Work shall terminate
              as of the date such litigation is filed.
        
           4. Redistribution. You may reproduce and distribute copies of the
              Work or Derivative Works thereof in any medium, with or without
              modifications, and in Source or Object form, provided that You
              meet the following conditions:
        
              (a) You must give any other recipients of the Work or
                  Derivative Works a copy of this License; and
        
              (b) You must cause any modified files to carry prominent notices
                  stating that You changed the files; and
        
              (c) You must retain, in the Source form of any Derivative Works
                  that You distribute, all copyright, patent, trademark, and
                  attribution notices from the Source form of the Work,
                  excluding those notices that do not pertain to any part of
                  the Derivative Works; and
        
              (d) If the Work includes a "NOTICE" text file as part of its
                  distribution, then any Derivative Works that You distribute must
                  include a readable copy of the attribution notices contained
                  within such NOTICE file, excluding those notices that do not
                  pertain to any part of the Derivative Works, in at least one
                  of the following places: within a NOTICE text file distributed
                  as part of the Derivative Works; within the Source form or
                  documentation, if provided along with the Derivative Works; or,
                  within a display generated by the Derivative Works, if and
                  wherever such third-party notices normally appear. The contents
                  of the NOTICE file are for informational purposes only and
                  do not modify the License. You may add Your own attribution
                  notices within Derivative Works that You distribute, alongside
                  or as an addendum to the NOTICE text from the Work, provided
                  that such additional attribution notices cannot be construed
                  as modifying the License.
        
              You may add Your own copyright statement to Your modifications and
              may provide additional or different license terms and conditions
              for use, reproduction, or distribution of Your modifications, or
              for any such Derivative Works as a whole, provided Your use,
              reproduction, and distribution of the Work otherwise complies with
              the conditions stated in this License.
        
           5. Submission of Contributions. Unless You explicitly state otherwise,
              any Contribution intentionally submitted for inclusion in the Work
              by You to the Licensor shall be under the terms and conditions of
              this License, without any additional terms or conditions.
              Notwithstanding the above, nothing herein shall supersede or modify
              the terms of any separate license agreement you may have executed
              with Licensor regarding such Contributions.
        
           6. Trademarks. This License does not grant permission to use the trade
              names, trademarks, service marks, or product names of the Licensor,
              except as required for reasonable and customary use in describing the
              origin of the Work and reproducing the content of the NOTICE file.
        
           7. Disclaimer of Warranty. Unless required by applicable law or
              agreed to in writing, Licensor provides the Work (and each
              Contributor provides its Contributions) on an "AS IS" BASIS,
              WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
              implied, including, without limitation, any warranties or conditions
              of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
              PARTICULAR PURPOSE. You are solely responsible for determining the
              appropriateness of using or redistributing the Work and assume any
              risks associated with Your exercise of permissions under this License.
        
           8. Limitation of Liability. In no event and under no legal theory,
              whether in tort (including negligence), contract, or otherwise,
              unless required by applicable law (such as deliberate and grossly
              negligent acts) or agreed to in writing, shall any Contributor be
              liable to You for damages, including any direct, indirect, special,
              incidental, or consequential damages of any character arising as a
              result of this License or out of the use or inability to use the
              Work (including but not limited to damages for loss of goodwill,
              work stoppage, computer failure or malfunction, or any and all
              other commercial damages or losses), even if such Contributor
              has been advised of the possibility of such damages.
        
           9. Accepting Warranty or Additional Liability. While redistributing
              the Work or Derivative Works thereof, You may choose to offer,
              and charge a fee for, acceptance of support, warranty, indemnity,
              or other liability obligations and/or rights consistent with this
              License. However, in accepting such obligations, You may act only
              on Your own behalf and on Your sole responsibility, not on behalf
              of any other Contributor, and only if You agree to indemnify,
              defend, and hold each Contributor harmless for any liability
              incurred by, or claims asserted against, such Contributor by reason
              of your accepting any such warranty or additional liability.
        
           END OF TERMS AND CONDITIONS
        
           APPENDIX: How to apply the Apache License to your work.
        
              To apply the Apache License to your work, attach the following
              boilerplate notice, with the fields enclosed by brackets "[]"
              replaced with your own identifying information. (Don't include
              the brackets!)  The text should be enclosed in the appropriate
              comment syntax for the file format. We also recommend that a
              file or class name and description of purpose be included on the
              same "printed page" as the copyright notice for easier
              identification within third-party archives.
        
           Copyright 2026 The MiddleLayer Authors
        
           Licensed under the Apache License, Version 2.0 (the "License");
           you may not use this file except in compliance with the License.
           You may obtain a copy of the License at
        
               http://www.apache.org/licenses/LICENSE-2.0
        
           Unless required by applicable law or agreed to in writing, software
           distributed under the License is distributed on an "AS IS" BASIS,
           WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
           See the License for the specific language governing permissions and
           limitations under the License.
License-File: LICENSE
License-File: NOTICE
Keywords: anthropic,apple-silicon,inference-gateway,llm,lm-studio,mlx,openai-compatible,self-hosted,swarm
Classifier: Development Status :: 3 - Alpha
Classifier: Environment :: Web Environment
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: System Administrators
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Operating System :: MacOS :: MacOS X
Classifier: Operating System :: POSIX :: Linux
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Typing :: Typed
Requires-Python: >=3.11
Requires-Dist: flask>=3.0
Requires-Dist: requests>=2.28
Provides-Extra: all
Requires-Dist: build>=1.2; extra == 'all'
Requires-Dist: flask-cors>=5.0; extra == 'all'
Requires-Dist: gunicorn>=22.0; extra == 'all'
Requires-Dist: hypothesis>=6.100; extra == 'all'
Requires-Dist: litellm>=1.40; extra == 'all'
Requires-Dist: mypy>=1.10; extra == 'all'
Requires-Dist: pre-commit>=3.7; extra == 'all'
Requires-Dist: pytest-cov>=5.0; extra == 'all'
Requires-Dist: pytest-timeout>=2.3; extra == 'all'
Requires-Dist: pytest>=8.0; extra == 'all'
Requires-Dist: ruff>=0.6; extra == 'all'
Provides-Extra: anthropic
Requires-Dist: litellm>=1.40; extra == 'anthropic'
Provides-Extra: dashboard
Provides-Extra: dev
Requires-Dist: build>=1.2; extra == 'dev'
Requires-Dist: hypothesis>=6.100; extra == 'dev'
Requires-Dist: mypy>=1.10; extra == 'dev'
Requires-Dist: pre-commit>=3.7; extra == 'dev'
Requires-Dist: pytest-cov>=5.0; extra == 'dev'
Requires-Dist: pytest-timeout>=2.3; extra == 'dev'
Requires-Dist: pytest>=8.0; extra == 'dev'
Requires-Dist: ruff>=0.6; extra == 'dev'
Provides-Extra: lmstudio
Requires-Dist: flask-cors>=5.0; extra == 'lmstudio'
Requires-Dist: gunicorn>=22.0; extra == 'lmstudio'
Provides-Extra: mlx
Requires-Dist: flask-cors>=5.0; extra == 'mlx'
Requires-Dist: huggingface-hub>=0.20; extra == 'mlx'
Requires-Dist: mlx-lm>=0.19; extra == 'mlx'
Description-Content-Type: text/markdown

<!-- README -->

# Local Lattice

**The capability layer between your agents and your LLM compute. Agents
describe what they need; Lattice picks the right model, routes, swarms, and
falls back. One OpenAI-compatible API, local-first.**

[![CI](https://github.com/chrisswimlee/local-lattice/actions/workflows/ci.yml/badge.svg)](https://github.com/chrisswimlee/local-lattice/actions/workflows/ci.yml)
[![License: Apache-2.0](https://img.shields.io/badge/License-Apache_2.0-blue.svg)](LICENSE)
[![Python 3.11+](https://img.shields.io/badge/Python-3.11%2B-blue.svg)](https://www.python.org/)
[![Project status: alpha](https://img.shields.io/badge/status-alpha-orange.svg)](#project-status-and-roadmap)

**Canonical repository:** [github.com/chrisswimlee/local-lattice](https://github.com/chrisswimlee/local-lattice).
The PyPI distribution name is **`local-lattice`**; the importable Python package remains **`middle_layer`** until Pass 3.

## The problem

Every agent framework hardcodes models. You write `model="gpt-4o"` or
`model="qwen2.5-coder-32b-instruct"` and ship it. The agent breaks when:

- the user has different models on disk,
- the operator wants to swap providers without redeploying,
- a small local model could have answered, but the agent went straight to
  the cloud anyway,
- one model wasn't enough and you needed a second opinion.

Agents shouldn't know model identifiers. They should declare **capabilities**
(`role:coder`, `role:reasoner`, vision, long context, low latency), and the
infrastructure should pick the best available local model — or fall back to
cloud — without the agent code changing.

## What Local Lattice is

A small Flask server that speaks the **OpenAI HTTP API** and adds a capability
layer on top of it:

- **Capability-based resolution.** `model="role:coder"`, priority lists
  (`"model-a,model-b,fallback"`), wildcards (`"*coder*"`), and automatic
  routing on vision content, prompt length, and a `X-MLX-Latency-Tier`
  header. Backed by [`mlx_roles.json`](./mlx_roles.json) and
  [`model_profiles.json`](./model_profiles.json) — see
  [`docs/capabilities.md`](./docs/capabilities.md) for the full grammar.
- **Swarm primitives, exposed as HTTP routes.** `/swarm/fanout`,
  `/swarm/vote`, `/swarm/pipeline`, `/swarm/debate` — let an agent ask for
  N opinions, a moderated vote, a sequential pipeline, or a multi-round
  debate without writing the orchestration itself.
- **Direct MLX execution on Apple Silicon** via
  [`mlx_lm`](https://github.com/ml-explore/mlx), with an LM Studio proxy
  backend for Linux / x86. Adding more backends (vLLM, llama.cpp) is on
  the roadmap.
- **Hybrid local-plus-cloud.** Optional escalation to Anthropic Claude
  when a request exceeds local capacity or requests it explicitly.
- **Production-shaped ops.** Multi-model LRU, per-model concurrency caps,
  bounded admission queue with priority and retry-after, and an in-process
  metrics dashboard at `/dashboard/`.

The HTTP shape is just OpenAI. Any agent framework that can point at a custom
OpenAI base URL works — see [`docs/integrations/`](./docs/integrations/) for
LangGraph and OpenAI Agents SDK examples.

> **Status (0.3.2): alpha.** The HTTP surface is stable in practice (every
> route is pinned by `docs/_internal/baseline/` regression captures) but the
> Python API and internal module layout will change before 1.0. Pin the
> version if you embed this. See [`docs/why-lattice.md`](./docs/why-lattice.md)
> for the longer "why this exists" story.

## 30-second quickstart

```bash
git clone https://github.com/chrisswimlee/local-lattice.git
cd local-lattice

python3 -m venv .venv && source .venv/bin/activate
pip install -e ".[mlx]"     # Apple Silicon only; use `[lmstudio]` elsewhere

# Probe LM Studio or MLX on this machine and write role:* mappings
local-lattice-init
# or: local-lattice-init --backend lmstudio --dry-run

# point at a folder with MLX weights (LM Studio's default is fine)
export MLX_MODEL_ROOT="$HOME/.lmstudio/models"
export MIDDLE_LAYER_API_KEY="$(uuidgen)"   # enable auth; deny-by-default

local-lattice-mlx serve --host 127.0.0.1 --port 5001
# back-compat: middle-layer-mlx is the same entry point
```

In another shell:

```bash
curl -sS -H "X-API-Key: $MIDDLE_LAYER_API_KEY" \
     http://127.0.0.1:5001/v1/models | jq .

curl -sS http://127.0.0.1:5001/v1/chat/completions \
     -H "X-API-Key: $MIDDLE_LAYER_API_KEY" \
     -H "Content-Type: application/json" \
     -d '{"model":"role:fast","messages":[{"role":"user","content":"ping"}]}'
```

The dashboard is at `http://127.0.0.1:5001/dashboard/` (set the same API
key in its sessionStorage prompt). Disable it with
`MLX_DASHBOARD_ENABLED=0`.

### 60-second demo

With either gateway running, [`scripts/demo.sh`](./scripts/demo.sh) walks
the whole pitch against your live model set:

```bash
./scripts/demo.sh                            # LM Studio gateway on :5000
BASE_URL=http://127.0.0.1:5001 ./scripts/demo.sh   # MLX gateway
```

It lists models, sends the same agent code at `role:fast` and
`role:coder` (watch them resolve to *different* loaded models), then asks
`/swarm/vote` for a judged second opinion. Swap what's loaded and run it
again — the calls don't change.

See [Swarm in 60 seconds](#swarm-in-60-seconds) below for sequence diagrams
and copy-paste examples.

## Swarm in 60 seconds

Swarm routes are the fastest way to get **multiple local models working
together** without writing orchestration. You keep sending capabilities
(`role:coder`, `"auto"`, …); Lattice resolves them against whatever is
loaded and runs fanout, judge, or sequential steps for you.

> **Tip:** Local reasoning models often need `max_tokens >= 2000` or they
> spend the whole budget on hidden chain-of-thought and return empty
> `content`. The snippets below default to 2000.

### How `/swarm/vote` works

One HTTP call → parallel fanout → judge picks a winner → you get a normal
OpenAI-shaped `chat.completion` plus a `swarm` object with candidates.

```mermaid
sequenceDiagram
    participant Client
    participant Lattice
    participant A as role:fast
    participant B as role:coder
    participant C as role:reasoner
    participant Judge as judge (role:reasoner)

    Client->>Lattice: POST /swarm/vote
    par Fanout (parallel)
        Lattice->>A: chat completion
        Lattice->>B: chat completion
        Lattice->>C: chat completion
    end
    A-->>Lattice: answer A
    B-->>Lattice: answer B
    C-->>Lattice: answer C
    Lattice->>Judge: rank anonymized candidates
    Judge-->>Lattice: winner + rationale
    Lattice-->>Client: chat.completion + swarm.winner
```

Use `"models": "auto"` to fan out to every loaded chat-capable model
(subject to the gateway's auto cap — see config table below).

### How `/swarm/pipeline` works

Sequential steps. Later steps reference earlier output via `{{plan}}`,
`{{code}}`, or `{{previous}}` in each step's `system` prompt.

```mermaid
sequenceDiagram
    participant Client
    participant Lattice
    participant Plan as plan (role:reasoner)
    participant Code as code (role:coder)
    participant Review as review (role:reasoner)

    Client->>Lattice: POST /swarm/pipeline
    Lattice->>Plan: step 1 — outline approach
    Plan-->>Lattice: plan text
    Lattice->>Code: step 2 — system includes {{plan}}
    Code-->>Lattice: code text
    Lattice->>Review: step 3 — system includes {{code}}
    Review-->>Lattice: final answer
    Lattice-->>Client: chat.completion (+ swarm.history)
```

### Copy-paste examples

**1. Single capability route** — drop-in for any OpenAI client:

```python
import os
from openai import OpenAI

client = OpenAI(
    base_url="http://127.0.0.1:5000/v1",  # or :5001 for MLX
    api_key=os.environ.get("MIDDLE_LAYER_API_KEY", "local"),
)
resp = client.chat.completions.create(
    model="role:coder",
    messages=[{"role": "user", "content": "Reverse a string in Python."}],
    max_tokens=2000,
)
print(resp.model)   # concrete model id that answered — log this
print(resp.choices[0].message.content)
```

**2. Judged second opinion** — fanout + judge in one call:

```python
import os, requests

BASE = "http://127.0.0.1:5000"
headers = {
    "Content-Type": "application/json",
    "X-API-Key": os.environ.get("MIDDLE_LAYER_API_KEY", ""),
}

resp = requests.post(
    f"{BASE}/swarm/vote",
    headers=headers,
    json={
        "models": "auto",
        "strategy": "best-of-n",
        "judge": "role:reasoner",
        "messages": [
            {"role": "user", "content": "Name a coffee-shop WiFi network."}
        ],
        "max_tokens": 2000,
    },
    timeout=300,
)
data = resp.json()
print("winner:", data.get("swarm", {}).get("winner"))
print(data["choices"][0]["message"]["content"])
```

**3. Plan → code → review pipeline:**

```python
resp = requests.post(
    f"{BASE}/swarm/pipeline",
    headers=headers,
    json={
        "messages": [
            {"role": "user", "content": "Build a CLI that counts words in a file."}
        ],
        "steps": [
            {
                "name": "plan",
                "model": "role:reasoner",
                "system": "Outline the approach in bullet points.",
                "max_tokens": 512,
            },
            {
                "name": "code",
                "model": "role:coder",
                "system": "Implement this plan:\n\n{{plan}}",
            },
            {
                "name": "review",
                "model": "role:reasoner",
                "system": "Critique and suggest fixes:\n\n{{code}}",
            },
        ],
        "max_tokens": 2000,
    },
    timeout=300,
)
```

**4. OpenAI client shortcut** — same vote, no new endpoint:

```python
resp = client.chat.completions.create(
    model="swarmCouncil",
    messages=[{"role": "user", "content": "Pros and cons of SQLite for a side project?"}],
    max_tokens=2000,
    extra_body={
        "swarm": {
            "models": "auto",
            "strategy": "best-of-n",
            "judge": "role:reasoner",
        },
    },
)
```

### Swarm route cheat sheet

| Route | What it does | Returns |
|-------|----------------|---------|
| `POST /swarm/fanout` | Same prompt → N models in parallel | All answers (`object: swarm.fanout`) |
| `POST /swarm/vote` | Fanout + judge | OpenAI `chat.completion` + `swarm.winner` |
| `POST /swarm/pipeline` | Sequential steps with `{{name}}` templates | Final step as `chat.completion` |
| `POST /swarm/debate` | Multi-round argument + judge synthesis | **MLX gateway only** (`:5001`) |
| `POST /v1/chat/completions` with `model: swarmCouncil` | Vote via plain chat API | Supports `stream: true` |

Full request/response contracts: [`docs/capabilities.md`](./docs/capabilities.md).
Agent-oriented reference: [`llms.txt`](./llms.txt).

Try it live: [`scripts/demo.sh`](./scripts/demo.sh) (steps 2–4 exercise
capability routing and `/swarm/vote`).

### Performance / routing overhead

Every timed response includes standard headers:

| Header | Meaning |
|--------|---------|
| `X-Lattice-Resolve-Ms` | Capability → model id (and swarm model expansion) |
| `X-Lattice-Queue-Ms` | Admission / queue wait before inference starts |
| `X-Lattice-Upstream-Ms` | LM Studio HTTP hop or MLX generation time |
| `X-Lattice-Total-Ms` | End-to-end handler wall time |

Legacy MLX headers `X-MLX-Latency-Ms` and `X-MLX-Queue-Wait-Ms` are still
set on the MLX gateway for one minor.

Structured logs (enabled by default, disable with `LATTICE_LOG_TIMING=0`):

```text
lattice.request resolve_ms=2 queue_ms=0 upstream_ms=840 total_ms=845 path=/v1/chat/completions status=200
```

Typical routing overhead on the **MLX direct path** is a few milliseconds.
The **LM Studio proxy** adds roughly **3–10ms** per request for the
localhost HTTP hop on top of resolve time. Streaming responses expose
queue/resolve timing in headers at stream start; upstream time reflects
generation and is also visible per chunk in the dashboard.

## Which gateway should I run?

Local Lattice ships **two interchangeable gateways** that speak the same
OpenAI-compatible HTTP surface. Pick one based on what you already have
running on the box:

| You have… | Run | Launcher | Port |
|---|---|---|---|
| **LM Studio installed and loading your models** | **`lmstudio` proxy** (recommended for most operators) | `./start_middle_layer.sh` or `./scripts/start.sh --profile lmstudio` | 5000 |
| MLX-converted weights and no LM Studio | `mlx` direct gateway | `./start_middle_layerMLX.sh` or `./scripts/start.sh --profile mlx` | 5001 |
| Memory-tight Mac running MoE / 70B+ models | `mlx` direct gateway in stable mode | `./scripts/start.sh --profile stable=safe` | 5001 |

### Pick `lmstudio` when…

- You already use LM Studio's UI as your model browser and download tool.
- You want a separate OS process serving inference (crash isolation: a bad
  load takes down LM Studio, not your gateway).
- You're running mixed model formats (GGUF, MLX, EXL2) — LM Studio's
  loader handles all of them; the MLX gateway only loads MLX weights.
- You don't care about the ~3–10ms HTTP roundtrip overhead per request.

This is the **primary path most operators want.** All the dynamic-by-
default behavior (strict loaded-model policy, curated swarm fanout)
lands here automatically when you use the launcher.

### Pick `mlx` when…

- You're running pure Apple-Silicon MLX models and want the lowest
  per-request latency (no HTTP hop, direct `mlx_lm.generate`).
- You want to ship MiddleLayer as a self-contained unit without
  requiring operators to install LM Studio separately.
- You need fine-grained in-process control over model lifecycle
  (programmatic load/unload, per-alias admission caps, real-time
  Metal-allocator hints).
- You're benchmarking — MLX shaves first-token latency on streaming
  endpoints.

The MLX gateway can run side-by-side with the LM Studio gateway on a
different port if you want both options available without switching.

### Pick `stable` when…

- You're on a memory-tight Mac (16 GB) and a single inference job can
  consume most of RAM. The stable profile tunes
  `MAX_CONCURRENT_MODELS=1`, `MAX_PARALLEL_MODEL_CALLS=1`,
  `MLX_PER_MODEL_INFLIGHT_CAP=1` and trims queue and token caps so
  the runtime never tries to coexist a second model with the first.
- Use `--profile stable=safe` (most conservative),
  `--profile stable=balanced`, or `--profile stable=faster` for the
  three pre-tuned tiers.

## How it compares

| Capability                                       | Local Lattice | `mlx_lm.server` | Ollama | LM Studio | LiteLLM |
|--------------------------------------------------|:-------------:|:---------------:|:------:|:---------:|:-------:|
| OpenAI `/v1/chat/completions` + `/v1/models`     |     ✅         |        ✅          |   ✅     |    ✅       |    ✅      |
| Streaming SSE (`data: ... [DONE]`)               |     ✅         |        ✅          |   ✅     |    ✅       |    ✅      |
| Capability routing (`role:coder`, vision, tier)  |     ✅         |        —           |   —      |     —      |     ~      |
| Auto-routing on prompt content (vision/long ctx) |     ✅         |        —           |   —      |     —      |     —      |
| Swarm: fanout / vote / pipeline / debate         |     ✅         |        —           |   —      |     —      |     —      |
| Hybrid local + cloud (Anthropic escalation)      |     ✅         |        —           |   —      |     —      |     ✅      |
| Direct MLX execution on Apple Silicon            |     ✅         |        ✅          |   —      |     —      |     —      |
| Multi-model LRU + per-model concurrency cap      |     ✅         |        —           |   ✅     |    ✅       |     —      |
| Admission queue with priority + retry-after      |     ✅         |        —           |   —      |     —      |     —      |
| In-process observability dashboard               |     ✅         |        —           |   —      |     —      |     —      |
| `pip install`, OpenAI-compatible API key auth    |     ✅         |        —           |   —      |     —      |     ✅      |

Local Lattice is **not** a replacement for `mlx_lm.server` or Ollama — it sits
*in front of* them and adds the capability layer that lets agent code stop
caring which model is running. If you only need a single model with raw
throughput, prefer the underlying runtime directly.

## Installing

### Apple Silicon (the main path)

```bash
pip install -e ".[mlx]"
```

Pulls `mlx-lm`, `huggingface_hub`, and `flask-cors`. The MiddleLayer CLI
auto-discovers `~/.lmstudio/models`, `~/.cache/lm-studio/models`,
`~/.cache/mlx-models` (in that order). Override with `MLX_MODEL_ROOT` or
`--model-root`.

### Linux / x86 (LM Studio proxy)

```bash
pip install -e ".[lmstudio,anthropic]"
```

This installs the cross-platform pieces only (no `mlx_lm`). The
`local-lattice-lmstudio` console script (alias: `middle-layer-lmstudio`) runs the legacy proxy that talks
to a separate LM Studio instance at `LM_STUDIO_URL=http://127.0.0.1:1234`.

### Everything cross-platform

```bash
pip install -e ".[all]"   # equivalent to [lmstudio,anthropic,dashboard,dev]
```

### Compatibility shims

For one minor version we still honour the previous workflow:

```bash
pip install -r requirements-mlx.txt           # == pip install -e .[mlx] (local-lattice)
pip install -r requirements-mlx-gateway.txt   # == pip install -e .[mlx,anthropic]
```

Both files print a deprecation note in their comments. They will be
removed in 0.4.0.

## Configuration

Configuration today is all environment variables, read at process start.
**Pass 2 is currently consolidating these into a typed
[`middle_layer.config.Settings`](./middle_layer/) object**; this README
will then auto-generate a complete table from the schema. Until that
lands, the canonical inventory of every variable, its default, and its
file location is the Pass-0 ground-truth document at
[`docs/_internal/CURRENT_STATE.md`](./docs/_internal/CURRENT_STATE.md)
(checked in on the `pass/0-discovery` branch).

Quick reference of the most common knobs:

| Env var                     | Default     | What it does                                            |
|-----------------------------|-------------|---------------------------------------------------------|
| `HOST`                      | `127.0.0.1` | Bind address. Refuses to start on a public interface without `MIDDLE_LAYER_API_KEY`. |
| `PORT`                      | `5001`      | TCP port for the gateway.                               |
| `MIDDLE_LAYER_API_KEY`      | _(unset)_   | If set, every request needs `X-API-Key` or `Bearer`. Compared constant-time. |
| `MIDDLE_LAYER_ALLOW_PUBLIC_NO_AUTH` | _(unset)_ | Override the public-bind safety check. Use only behind a trusted auth-enforcing proxy. |
| `MIDDLE_LAYER_MAX_REQUEST_BYTES` | `10485760` | Max HTTP request body in bytes (default 10 MiB).      |
| `MLX_MODEL_ROOT`            | auto        | Where to look for MLX model directories.                |
| `DEFAULT_MODEL`             | _(empty)_   | Alias returned for `model: ""`/`auto`/`default`.        |
| `MAX_CONCURRENT_MODELS`     | `2`         | LRU bound on resident MLX models.                       |
| `MAX_PARALLEL_MODEL_CALLS`  | `2`         | Global concurrent-generation cap.                       |
| `MLX_PER_MODEL_INFLIGHT_CAP`| `1`         | Per-alias generation cap (MLX gateway). `0` disables admission (legacy; emits `DeprecationWarning` when unset before 0.4.0). |
| `MLX_FORCE_GC_ON_EVICT`     | `0`         | When `1`, run `gc.collect()` after every MLX eviction in addition to the Metal-cache release. Tighter peak RSS on memory-tight Macs at the cost of small extra wall-clock latency per swap. |
| `EXTRA_PLACEHOLDER_MODELS`  | _(unset → legacy OpenClaw set + `DeprecationWarning`)_ | Comma-separated extra "you pick" aliases; set to empty to exclude legacy ids. |
| `PREFER_LOADED_MODELS`      | `strict`    | LM Studio gateway loaded-id policy. `strict` never JIT-loads installed-but-not-loaded ids; `1` falls back to the installed set on a miss; `0` ignores loaded vs installed. Unset emits a `DeprecationWarning` (legacy default was `1`). |
| `SWARM_CHAT_DEFAULT_MODELS` | `auto`      | Default `swarm.models` list when a swarm chat request omits it. `auto`/`loaded`/`*` expand to the currently-loaded chat-capable set (filtered to exclude embedding models, capped at `SWARM_CHAT_AUTO_MAX`); or a comma-separated list of ids/`role:*` lookups. Unset emits a `DeprecationWarning` (legacy default was `role:reasoner,role:coder,role:fast`). |
| `SWARM_CHAT_AUTO_MAX`       | `3`         | Cap on how many loaded ids the `auto` sentinel contributes to a default-shaped swarm. Keeps fanout-vs-latency reasonable on boxes with many loaded models. Set to `0` to disable the cap. Dedicated `/swarm/fanout` HTTP endpoint ignores this. |
| `SWARM_CHAT_DEFAULT_STRATEGY` | `best-of-n` | Default swarm winner-pick when the request omits `swarm.strategy`. `best-of-n` (judge picks from candidates), `first-success` (returns on first temporally successful agent, cancels pending peers), `longest`, `fanout`. |
| `ANTHROPIC_API_KEY`         | _(unset)_   | Enables optional Claude escalation for long tasks.      |
| `ANTHROPIC_AUTO_ROUTE`      | `1`         | Auto-escalate big tasks. Will default off in 0.4.0.     |
| `MLX_DASHBOARD_ENABLED`     | `1`         | Mount the in-process dashboard at `/dashboard/`.        |
| `MLX_DASHBOARD_CAPTURE_PROMPTS` | `0`     | Keep prompts in the dashboard ring. Off by default.     |

## Security defaults (deny-by-default)

- **Constant-time API key check.** Both gateway backends and the
  dashboard compare keys with `hmac.compare_digest`. Send the key as
  either `X-API-Key: <key>` or `Authorization: Bearer <key>`.
- **Refuse to bind public without auth.** Starting on a non-loopback
  interface without `MIDDLE_LAYER_API_KEY` set exits with a clear error.
  Override with `MIDDLE_LAYER_ALLOW_PUBLIC_NO_AUTH=1` only when an
  upstream proxy is enforcing authentication.
- **Request body size cap.** Default 10 MiB; tune with
  `MIDDLE_LAYER_MAX_REQUEST_BYTES`. Oversize requests get a Flask-native
  413.
- **Standard hardening headers** on every response:
  `X-Content-Type-Options: nosniff`, `X-Frame-Options: DENY`,
  `Referrer-Policy: no-referrer`, `Cross-Origin-Resource-Policy: same-origin`.
  Dashboard responses additionally carry a strict
  `Content-Security-Policy` that disallows inline scripts and remote
  sources.
- **Dashboard model-load allowlist.** `/dashboard/api/models/load` only
  accepts aliases that pass a syntactic filter *and* appear in the live
  on-disk model set discovered by the MLX manager.
- **CORS off.** Set `CORS_ORIGINS=https://your.app` to allowlist a
  specific origin. `*` is accepted today but is rejected when combined
  with credentials.
- **Prompt logging off.** `MLX_DASHBOARD_CAPTURE_PROMPTS=0`. Turning it
  on stores recent user prompts in process memory only (never to disk
  in this release); a regex redactor is on the Pass-5+ roadmap.

Still open and tracked for future passes: per-IP rate limiting, HSTS
guidance behind TLS, and CSP nonce-mode for the dashboard. See the
[hardening roadmap](./SECURITY.md#hardening-roadmap) for the full list.

A full threat model and the responsible-disclosure address live in
[SECURITY.md](./SECURITY.md).

## Docs

- [`llms.txt`](./llms.txt) — **self-contained integration guide for AI
  agents**: feed this one file to a coding agent and it has every
  endpoint shape, the `model` grammar, and the error contract needed to
  integrate without human help.
- [`docs/why-lattice.md`](./docs/why-lattice.md) — the longer "why this
  exists" story: capability routing as an agent-infra primitive.
- [`docs/capabilities.md`](./docs/capabilities.md) — formal spec of the
  capability protocol: resolver grammar, role registry, auto-routing,
  swarm endpoint contracts.
- [`docs/integrations/`](./docs/integrations/) — drop-in examples for
  LangGraph, OpenAI Agents SDK, and other agent frameworks.
- [CONTRIBUTING.md](./CONTRIBUTING.md) — dev loop, commits, tests.
- [SECURITY.md](./SECURITY.md) — vulnerability reporting.
- [CHANGELOG.md](./CHANGELOG.md) — semver-shaped release notes.
- [CODE_OF_CONDUCT.md](./CODE_OF_CONDUCT.md) — Contributor Covenant 2.1.
- `docs/configuration.md` (Pass 2) — every setting, auto-generated.
- `docs/openapi.yaml` (Pass 8) — hand-curated OpenAPI spec.

## Project status and roadmap

This repository is mid-migration from "useful internal code" to a
polished open-source release. The migration is broken into named
passes, each landed as its own PR. Pass-by-pass progress lives in
[CHANGELOG.md](./CHANGELOG.md). A high-level summary:

- **Pass 0** (done) — read-only discovery, baseline regression captures.
- **Pass 1** (this release) — legal foundation, build system,
  documentation, launcher consolidation, branding scrub.
- **Pass 2** — configuration consolidation (`pydantic-settings`).
- **Pass 3** — restructure the two monoliths into a typed package.
- **Pass 4** — security hardening (auth, CORS, rate limits, CSP).
- **Pass 5** — tests, types, linting, CI.
- **Pass 6** — observability (`structlog`, `/metrics`, optional OTEL).
- **Pass 7** — distribution (PyPI, Dockerfile, devcontainer).
- **Pass 8** — docs, OpenAPI, dashboard UX overhaul.
- **Pass 9** — polish and 1.0.

## License

Apache-2.0. See [LICENSE](./LICENSE) and [NOTICE](./NOTICE).
