# Route between local and cloud models with hanzo-router

URL: https://docs.hanzo.ai/docs/guides/local-first-routing

> Run hanzo-router, the open Rust routing core, on your own machine: give it a model registry and a memory snapshot, and watch it pick a loaded model, a local model that fits, or the cloud.

**After this page you have run a routing decision on your own machine, you know
the order the router prefers, and you know which parts of Hanzo's routing you
can read and which you call over the API.**

Routing is the choice of which model answers a request. `hanzo-router` makes
that choice with pure logic over values you pass in. You give it a registry of
models, a snapshot of free memory, and the set of models already loaded. It
touches no hardware, so every decision is deterministic and easy to test.

## What the router prefers

For a classified request, `Policy::select` returns the first of these that works:

1. **`Reuse`** — a model already loaded in a running engine. No load cost.
2. **`LoadLocal`** — a local model whose footprint fits in free memory.
3. **`Cloud`** — the cheapest cloud model that can do the task.
4. **`NoFit`** — nothing can serve it; the caller decides what to do.

A model can also be ruled out before it is ranked: if it cannot read images
the request carries, if its context window is too short, or if a cloud
model costs more than the request's cost ceiling.

## Run it

You need a Rust toolchain (`rustup`, stable). Make a new crate and add the
router from GitHub:

```bash
cargo new route-demo && cd route-demo
cargo add hanzo-router --git https://github.com/hanzoai/router
```

Replace `src/main.rs` with:

````rust title="src/main.rs"
use hanzo_router::{route, Backend, Heuristic, MemSnapshot, ModelCard, Policy, Registry, Request, Task};
use std::collections::BTreeSet;

const GB: u64 = 1 << 30;

fn main() {
    let registry = Registry::new(vec![
        ModelCard { id: "zen6-flash".into(), backend: Backend::Local { est_bytes: 6 * GB },
            tasks: vec![Task::Code, Task::Reasoning, Task::General], max_context: 262_144, vision: true, cost_per_1k: 0.0 },
        ModelCard { id: "enso-pro".into(), backend: Backend::Cloud { provider: "hanzo".into() },
            tasks: vec![Task::Code, Task::Reasoning, Task::General], max_context: 1_000_000, vision: true, cost_per_1k: 3.3 },
    ]);
    let policy = Policy::default();
    let req = Request { text: "fix this ```rust``` borrow-checker error".into(), ..Default::default() };

    let cases = [
        ("laptop, 20 GB free, nothing loaded", MemSnapshot { available_bytes: 20 * GB, total_bytes: 32 * GB, unified: true }, vec![]),
        ("laptop, zen6-flash already loaded ", MemSnapshot { available_bytes: 14 * GB, total_bytes: 32 * GB, unified: true }, vec!["zen6-flash"]),
        ("laptop, 3 GB free, nothing loaded ", MemSnapshot { available_bytes: 3 * GB, total_bytes: 16 * GB, unified: true }, vec![]),
    ];
    for (label, mem, loaded) in cases {
        let running: BTreeSet<String> = loaded.into_iter().map(String::from).collect();
        println!("{label} -> {:?}", route(&req, &Heuristic, &policy, &registry, mem, &running));
    }
}
````

Run it:

```bash
cargo run -q
```

```text title="Output"
laptop, 20 GB free, nothing loaded -> LoadLocal { model: "zen6-flash", est_bytes: 6442450944 }
laptop, zen6-flash already loaded  -> Reuse { model: "zen6-flash" }
laptop, 3 GB free, nothing loaded  -> Cloud { provider: "hanzo", model: "enso-pro" }
```

Same request, three answers. The request did not change; the machine did.
The registry entries are values you write. `est_bytes` is your estimate of
the model's resident size, and `cost_per_1k` is the price you want the cost
gate to see. The router does not download or serve models. It only decides.

> This output was produced on 2026-11-29 against `hanzoai/router` at commit
> `2791b4f`, crate version `0.1.1`.

## Check it yourself

The repository carries its own tests and a timing loop for the rules path:

```bash
git clone https://github.com/hanzoai/router && cd router
cargo test --release
cargo test --release --test bench_overhead -- --nocapture
```

The benchmark times `classify` plus `select` over a 20-model registry with a
prompt of about 1,000 characters and prints mean, p50 and p99 per decision.
It measures the decision only: no network, no model load, no learned policy.
Numbers depend on your CPU, so run it on your own hardware and read the result
there.

## Rules first, learned later

The router separates **mechanism** from **policy**. The mechanism (registry,
memory fit, cost gate, dispatch) is what you ran above. The policy is the part
that ranks candidates, and it can be swapped:

- **Rule-based `Policy`**, the default. You can load it from YAML with
  `hanzo_router::load_policy`. A new deployment can route sensibly before it
  has any evaluation data.
- **Learned heads**, a ridge-fit policy that an engine loads from a bundle
  with `ROUTER_HEADS=<path>`. With the variable unset, routing falls back to
  the rules.

## Where Enso fits

[Enso](https://docs.hanzo.ai/docs/models/enso) is Hanzo's hosted router and model family on
`api.hanzo.ai`. `enso-auto` picks a tier per request and bills at the tier
that answers. Enso's policy is closed and served by API only. `hanzo-router`
is the open placement core you can read, run and test. Use the router to keep
local-or-cloud decisions on your own machine. Use `enso-auto` when you want
Hanzo to pick among hosted tiers for you.

## Next steps

- [Enso](https://docs.hanzo.ai/docs/models/enso): the hosted tiers, their prices and the `auto` router.
- [What is open in Hanzo](https://docs.hanzo.ai/docs/open-source): which parts you can read, run and fork.
- [Self-hosting quickstart](https://docs.hanzo.ai/docs/self-hosting/quickstart): run Hanzo Cloud and Engine on your own machine.
- Source: [github.com/hanzoai/router](https://github.com/hanzoai/router).
