# Set up System Zero in a coding agent, and build the user's own > System Zero (https://system-zero.semantic-router.org) is a decision model with no learned weights: rules counted from data plus hand-written modules, in pure Python (3.9 or newer, no dependencies) on one CPU core. It answers the typed questions of the System One decision API, a choice, a yes/no (`noul`) or a score, with a probability for every option. Every answer names its rule, the rule's record in training and its file and line, or says that no rule applied. It is on PyPI as `sr-zero`. This guide is for a coding agent: follow it to install System Zero for the user, answer with it, serve it over HTTP, build and maintain the user's own Zero from their labelled data, and put it in front of the user's own model. The user asked you to set up System Zero, to decide something with it, or to build their own Zero. Do the steps in order: each ends with a check, and the next step assumes it passed. Say what you will install or change before you do it. System Zero runs on the user's machine and sends nothing anywhere, unless the user gives it their own model's endpoint with `--defer-to` (step 6); keep it that way. The user's data, packs and answers stay local: never paste them into a web search, an issue, a chat or another service. The commands are for bash and zsh (Linux, macOS, WSL). ## 0. Check that System Zero fits - Use it for routine decisions whose evidence is in the input text: routing a ticket, flagging spam, an intent, a policy check, a triage step. It answers in under a millisecond on one CPU core, and every answer can be checked against its rule. - Do not use it for open-domain language understanding or world knowledge: facts, commonsense, what a text implies. Words alone do not carry those answers; System Zero is near chance there. On questions outside what its rules cover it falls back to the prior or chance, and its confidence on new domains can be wrong, so keep open-domain and new-domain decisions with a neural model, and tell the user why. - Not for decisions about people, or other high-stakes uses, without review by a person. ## 1. Check Python and uv ```sh uv --version python3 --version ``` System Zero needs Python 3.9 or newer. With uv the system Python does not matter: uv fetches a suitable one when it finds none. Without uv, ask the user before installing it (`curl -LsSf https://astral.sh/uv/install.sh | sh`, then a new shell), or use the virtual environment in step 2. ## 2. Install, and ask one question ```sh uvx sr-zero 'Check out my channel and subscribe' -q 'Is this comment spam?' --yes-no ``` `uvx` runs it without installing anything permanently; the download is about 7 MB, once. uv may print a line about it, then the answer comes in well under a second: ```text > yes 0.90 ###################### no 0.10 ## why: if the state contains "my channel", answer true (37 of 37 right in training) system_zero/rules/options.py:2324 · 379 µs · 0 parameters ``` To keep the command, install it as a tool and check the version: ```sh uv tool install sr-zero sr-zero --version ``` Check: `sr-zero 0.11.0` or newer; the pack commands of step 5 need 0.9.2, and `--defer-to` (step 6) needs 0.11.0. Without uv, install into a virtual environment, since many systems refuse `pip install` outside one: ```sh python3 -m venv ~/.venvs/sr-zero ~/.venvs/sr-zero/bin/python -m pip install sr-zero ~/.venvs/sr-zero/bin/sr-zero --version ``` On Debian and Ubuntu, `python3 -m venv` needs the `python3-venv` package and otherwise stops with `ensurepip is not available`: ask the user to install it, or use uv. ## 3. Read the answer - `> yes 0.90`: the pick and its probability. The probability is the lower end of the 95% interval of the rule's record, so a rule seen a few times stays unsure, and it reaches 0.9 only on a record that three or more families of training questions back; this record's lower end is 0.906, held just under 0.9. - `if the state contains "my channel", answer true`: the rule that decided. `true` is the yes of a yes/no question. - `(37 of 37 right in training)`: the rule's record: it fired on 37 training questions and was right on all 37. - `system_zero/rules/options.py:2324`: the rule's file and line in the installed package. `0 parameters`: there are no weights. - `no rule applies, so every option is equally likely` (chance) or `no rule fired, so the answer most often right in training` (the prior): nothing in the text decided. Treat the answer as unknown and defer it. A choice takes `-o` and its options, a score `--levels` from lowest to highest: ```sh sr-zero 'Oh great, my flight got cancelled again. Truly the best way to start a holiday.' -q 'Is this text intended to be sarcastic?' -o yes no sr-zero 'Our team offsite is in the city of the Sagrada Familia.' -q 'Which country do we fly to?' -o Spain Italy Portugal sr-zero 'The service was fine, nothing special.' -q 'How positive is this review?' --levels negative neutral positive ``` The sarcasm answer is `yes 0.90`, from a bag of words: `the answer the request's words count for most, against "no" (18 features; most of all "truly the", "oh great", "great my") (1,898 of 1,997 right in training)`. The country needs world knowledge: every option gets 0.33 and the reason is `no rule applies`, so that question belongs to a neural model. - `--raw` prints the answer as JSON with its `explanation`: `by` (the kind of rule: `module`, `subframe`, `frame`, `options`, `evidence`, `bag`, `table`, `cue`, `prior` or `chance`, and `deferred` for an answer from the user's model, step 6), `rule`, `right`, `fired`, `file`, `line` and `code`, the rule's source line. - Many questions: write one `{"state", "questions"}` request per line (the form of step 4) and run `sr-zero -j requests.jsonl -e`. It prints one JSON answer per line; `-e` adds the explanations. ## 4. Serve it ```sh sr-zero serve --port 8000 ``` It loads the rules once, in about 3 seconds and at a peak of 410 to 460 MB, then prints `System Zero is listening on http://127.0.0.1:8000 (POST /v1/systemone)`. Run it in a second terminal, or in the background with its output in a log file (`nohup sr-zero serve --port 8000 > sr-zero-serve.log 2>&1 &`). It listens on 127.0.0.1, the default; keep it there unless the user asks otherwise. Check from another shell: ```sh curl -s -w '\n' http://localhost:8000/health curl -s -w '\n' http://localhost:8000/v1/systemone \ -H 'Content-Type: application/json' \ -d '{"state": "Check out my channel and subscribe", "questions": {"spam": {"type": "noul", "instructions": "Is this comment spam?"}}}' ``` ```json {"api_version":"5.0.0","status":"ready","reason":null,"model":"system-zero"} {"model":"system-zero","answers":{"spam":{"type":"noul","noul":0.8999059390425449}},"usage":{"input_tokens":10,"output_tokens":0}} ``` A request is a `state`, the text to decide about, and named `questions`: | `type` | You write | The answer | |---|---|---| | `choice` | `instructions`; `criteria`: 2 to 255 options, each a key and a description | `choice`, every option's `probabilities`, and `confidence`, the lead over the next option | | `noul` | `instructions`; optionally `criteria.true` and `criteria.false` | `noul`: the probability of yes | | `score` | `instructions`; `criteria`: 2 to 10 levels, from lowest to highest | `score` (the expected level), `confidence`, `legend` and each level's `probabilities` | - `POST /api/decide` takes the same request and adds each answer's `explanation`, with the lines of the rule file around the rule. Use it to report why. - `POST /v1/systemone/batch` answers named requests in one call. `POST /v1/decisions` is the native form, where a question that fails carries its own error. `GET /v1/models` describes the model. - With `SYSTEM_ZERO_API_KEYS=KEY1,KEY2` in its environment, the server asks for `Authorization: Bearer KEY` on the `/v1/` paths. `/api/decide` takes no key, so do not open the port to other machines. - Stop the server with Ctrl+C, or with `kill` and its process id when it runs in the background. ## 5. Build the user's own Zero A Zero pack is a directory of data, never code: rules counted from the user's labelled examples, plus override rules the user writes, served in front of the official rules for the questions it claims. Every other question is answered by the official rules exactly as without the pack. The examples below build a pack `support` from 600 labelled tickets; use the user's names and files. ### 5.1 Look at the data - CSV: a header with `text` and `label`. Optional columns: `source` (where a row came from, such as a channel, a customer or a dataset; it is the unit you can remove later), `group` (rows that are near-copies, such as one thread, template or customer; they stay together when records are counted on held-out rows), `split` (rows marked `dev` or `test` are left out of the compile), `licence` and `id`. Each row is asked as the pack's question, with one option per label. - System One JSON lines: `{"state": "...", "questions": {"q": {...}}, "expected": {"q": "OPTION_KEY"}}`, with `true` or `false` for a `noul`; optionally `source`, `meta.licence` and `meta.group`. A file can ask several questions; `choice` and `noul` questions are learned. - Check: at least 30 labelled rows per question (a few hundred are better), each label spelled one way, and no rows from a benchmark or test set the user will report results on. Hold out a dev set for step 5.4. If the user has none, split one row in five off a copy of their data, by `group` when there is one. Here the data is `data/all.csv`, which stays as it is: ```sh python3 - <<'EOF' import csv, hashlib with open('data/all.csv', newline='', encoding='utf-8-sig') as f: reader = csv.DictReader(f) rows, columns = list(reader), reader.fieldnames names = {c.strip().lower(): c for c in columns} key = names.get('group') or names['text'] for path, dev in (('data/train.csv', False), ('data/dev.csv', True)): with open(path, 'w', newline='', encoding='utf-8') as f: out = csv.DictWriter(f, fieldnames=columns) out.writeheader() out.writerows(r for r in rows if (int(hashlib.sha256(r[key].encode()).hexdigest(), 16) % 5 == 0) == dev) EOF wc -l data/train.csv data/dev.csv ``` ### 5.2 Create the pack and compile it ```sh sr-zero pack init support --question 'Which team should handle this ticket?' sr-zero pack compile data/train.csv -o support ``` ```text created support/: manifest.json, overrides.jsonl (rules you write), tests.jsonl (their examples and your decision tests) next: sr-zero pack compile DATA -o support read 600 rows from 1 file(s); skipped 0 without a usable label account, billing, returns, sales, shipping, technical (under any question template): 600 training questions, evidence sums and a decision list: 45 rules, 1134 evidence cues sources (questions, licence class): email 300 (U), web 300 (U) wrote support/rules.jsonl and manifest.json in 0.14 s ``` - Check: a line with the user's labels and a rule count above 0. `not learned: ... (fewer than --min-count)` means too few rows for that question; `skipped N without a usable label` points at rows to look at. - `(U)` is the licence class of rows that state no licence; `--licence 'LICENCE'` records one. - The same data and settings compile to the same bytes. Never edit `rules.jsonl` by hand: the manifest holds its hash, and a changed file refuses the pack. ### 5.3 Ask it ```sh sr-zero pack explain support 'I was charged twice for my plan this month' ``` ```text > billing 0.63 ############### sales 0.11 ### account 0.11 ### returns 0.06 # shipping 0.05 # technical 0.05 # why: the answer the request's words count for most (12 of its cues: "i was", "month", "was charged") (132 of 190 right in training) support/rules.jsonl:454 · 313 µs · 0 parameters by source: email 64 of 98, web 68 of 92 pack support 0.1.0 ``` The rule is a line of the pack's own `rules.jsonl`, and its record is split by source. `--compare` adds what the official rules alone would answer. A pack compiled from JSON lines has no default question: add `-q 'QUESTION' --yes-no`, or `-q 'QUESTION' -o A B C`. ### 5.4 Run the dev gate ```sh sr-zero pack test support --dev data/dev.csv ``` ```text loading the official rules ... pack support 0.1.0 · identity d98174bb052c · 1 scope(s), 45 rules, 1,134 evidence cues, 0 override(s) decision tests: none yet (lines with "expect" in tests.jsonl) dev: 180 questions accuracy 51.7% (the official rules alone: 20.6%) who answered pack:evidence 180 at 51.7% coverage threshold: answered at accuracy; a prior's answer never counts as answered 0.00: 100.0% at 51.7%; 0.50: 33.9% at 88.5%; 0.70: 0.0% at n/a; 0.80: 0.0% at n/a; 0.90: 0.0% at n/a; 0.95: 0.0% at n/a calibration expected calibration error 14.7% threshold none: no threshold's answers reach the target on dev with enough answered; defer everything, or improve the pack latency 0.380 ms median, 0.697 ms p95 per decision gate: passed (180 questions the pack answers: pack 51.7%, official alone 20.6%; 56 won, 0 lost, sign test p < 0.0001) ``` - `accuracy`: the pack on the dev questions, against the official rules alone on the same questions. - `who answered`: each layer's count and accuracy. `pack:evidence`, `pack:options` and `pack:frame` are counted rules, `pack:prior` means that no rule fired, `override:override` is the user's overrides. - `coverage`: for each threshold, the share of dev questions answered at or above it and their accuracy. - `threshold`: the lowest threshold whose answers reach the target, a 90% lower bound (`--target` sets it), with at least 30 answered, printed rounded down so it is safe to copy; `none` when no threshold does. - `gate`: on the dev questions the pack claims, the pack against the official rules alone. It fails when the official rules are right more often where the two disagree (more lost than won). - `test` exits 1 when the gate or a decision test fails, and 2 when the pack cannot be loaded. What to do with the result: - `gate: FAILED`: do not serve, seal or hand over the pack. Tell the user that the official rules alone do better on these questions, then fix the labels, add data, or drop the pack. - `gate: passed (0 questions the pack answers ...)`: the dev set does not ask the pack's question. A yes/no or two-option pack answers its own question wording alone; make the dev rows ask it and run the gate again. - `gate: passed` with a threshold: serve it. Seal the threshold into the pack (5.6) and serve it in front of the user's model with `--defer-to` (step 6), or let the caller accept an answer when its top probability (`probabilities[choice]`, or the larger of `noul` and `1 - noul`) is at least the threshold and its explanation from `/api/decide` does not say `"by": "prior"` or `"by": "chance"`; the rest goes to a person or a neural model. - `gate: passed` and threshold `none`: the pack beats the official rules but is not sure enough to decide alone at the target. Serve it as a suggestion at most, or defer every decision, and improve it with more and more varied examples, and with overrides for what the user knows. A lower `--target` finds a threshold only if the user accepts that accuracy: the example pack reaches a 75% lower bound at 0.625 (step 6). - A pass on few dev questions (the test warns under 100), or with the sign test's p at 0.05 or above, is weak evidence. Say so. ### 5.5 Fix a decision with an override When the pack gets a case wrong and the user knows the policy, add one line to `support/overrides.jsonl`: ```json {"id": "chargebacks", "when": {"any": ["chargeback", "disputed the charge"]}, "answer": "billing", "note": "Payment disputes go to billing, whatever else the ticket says."} ``` and its examples, with a decision test, to `support/tests.jsonl`: ```json {"rule": "chargebacks", "state": "My bank shows a chargeback from you.", "fires": true} {"rule": "chargebacks", "state": "I disputed the charge with my card company.", "fires": true} {"rule": "chargebacks", "state": "Can I charge my phone with the dock?", "fires": false} {"state": "My bank shows a chargeback from you, and the parcel never arrived", "expect": "billing"} ``` - A phrase matches whole words in order, anywhere in the text, in any letter case: `charge` does not match `charged`. `when` takes `any`, `all` and `none` lists of phrases; `answer` is one of the pack's options. There are no regular expressions. - Every override needs an example it fires on; `"fires": false` lines are texts it must leave alone. When an example does not hold, the pack refuses to load, and the message names the file and line. - An override decides before the counted rules. Its probability is the lower bound of its record on its examples: 2 examples give 0.34, 10 give 0.72 and 20 give 0.84. Check: ```sh sr-zero pack explain support 'My bank shows a chargeback from you' sr-zero pack test support --dev data/dev.csv ``` The explanation reads `why: if the request says "chargeback" or "disputed the charge", answer billing`, at `support/overrides.jsonl:1`, and the test reports `overrides: 1 rule(s), 3 example(s), all as they should be` and `decision tests: 1 of 1 pass`, with the gate. ### 5.6 Seal it, and serve it ```sh sr-zero pack test support --dev data/dev.csv --seal ``` Check: `sealed: manifest.json records the hashes of the files that passed`. With `--dev`, sealing needs the gate to pass as well, otherwise the test prints `not sealed` under what failed, and it records the threshold the test recommends, which `serve --defer-to` uses (step 6). ```sh sr-zero serve --pack support --port 8000 ``` It prints `packs: support 0.1.0`, runs every decision test of every pack, and refuses to start when one fails. Ask the pack's question as `questions.decision` in `support/manifest.json` writes it: ```sh curl -s -w '\n' http://localhost:8000/v1/systemone \ -H 'Content-Type: application/json' \ -d '{"state": "My bank shows a chargeback from you", "questions": {"team": {"type": "choice", "instructions": "Which team should handle this ticket?", "criteria": {"account": "account", "billing": "billing", "returns": "returns", "sales": "sales", "shipping": "shipping", "technical": "technical"}}}}' ``` - Check: the answer has `"choice":"billing"`. - Over HTTP, `/api/decide` cites a pack rule's file, line and record but leaves out its words, notes and sources; `--explain-packs full` shows them, for a server the user alone can reach. - Several packs: repeat `--pack`; the earlier pack takes precedence, and two packs that claim one question are refused unless `--overlap` lets the earlier one win. - From Python: `from system_zero.packs import Stack, load`, then `Stack([load('support')]).decide(state, questions, explain=True)`. ### 5.7 Remove a source Every count in a pack is kept by source, so a source can be removed exactly, for example when the user may no longer use one channel's data: ```sh sr-zero pack unlearn support --source email --dry-run sr-zero pack unlearn support --source email data/train.csv -o support-no-email sr-zero pack test support-no-email --dev data/dev.csv ``` ```text 45 of 45 rules count questions from email (292 firings); `unlearn` without --dry-run rebuilds the pack without them removed 300 rows of email; rebuilt support-no-email from 300 rows: 1121 rule lines gone, 839 new or changed its rules.jsonl is the one a compile of the remaining rows gives, byte for byte; manifest.json records the removal ``` `unlearn` needs the data the pack was compiled from and checks that the other sources' rows are unchanged; the overrides and tests come along. Run the gate on the new pack before it replaces the old one. The pack holds no copy of the rows, but the user's data files do: removing the rows there is the user's call. ### 5.8 Keep it up to date - New labelled rows: compile again from all the files (`sr-zero pack compile data/train.csv data/new.csv -o support`), then run steps 5.4 and 5.6. Overrides and tests stay. - A wrong answer reported: add a decision test that expects the right answer, then an override or more data, then the gate. - After upgrading sr-zero, run `pack test --dev` again. A pack that is refused because this System Zero reads text differently needs a compile from its data. - Keep packs in the user's private version control: one rule per line, so `git diff` and `git blame` work rule by rule. A pack holds words and word pairs of its training text, so it is not for publishing without the user's OK; `pack test` warns about cues that look like identifiers. ## 6. Put it in front of the user's model With `--defer-to URL`, `sr-zero serve` keeps the questions it is sure of and sends the rest, with the request's state, to any System One endpoint: the user's own neural model. A question stays when a pack's override decided it, or a rule answered it at or above the threshold; an answer from a prior or by chance always goes. Nothing is sent anywhere unless `--defer-to` is given: ask the user for their model's endpoint, and use no other. Seal a threshold into the pack, at a target the user accepts (here a 75% lower bound): ```sh sr-zero pack test support --dev data/dev.csv --target 0.75 --seal ``` ```text threshold 0.625: answers 33.9% (61) at 88.5%, Wilson lower bound 78.2% >= target sealed: manifest.json records the hashes of the files that passed and the threshold 0.625, which serve --defer-to uses unless --threshold is given ``` The examples below send to `http://127.0.0.1:8001`; use the user's endpoint. To check the wiring before their model is connected, a second System Zero can stand in for it, serving decision lists compiled from the same rows: ```sh sr-zero pack compile data/train.csv -o model --question 'Which team should handle this ticket?' --no-evidence nohup sr-zero serve --pack model --port 8001 > sr-zero-model.log 2>&1 & ``` Serve the pack in front of the model: ```sh sr-zero serve --pack support --defer-to http://127.0.0.1:8001 --port 8000 ``` ```text packs: support 0.1.0 defer: questions not kept here (threshold 0.625, from `pack test --dev --seal` of pack support) go to http://127.0.0.1:8001/v1/systemone System Zero is listening on http://127.0.0.1:8000 (POST /v1/systemone) ``` `--threshold T` sets the threshold by hand, and `--threshold yes=0.95,no=0.99` one per answer; `--defer-timeout SECONDS` (10 by default) bounds the wait; `SYSTEM_ZERO_DEFER_KEY`, if set, is sent to the endpoint as its key, and nowhere else. Check from another shell, with a ticket the pack is unsure of: ```sh curl -s -w '\n' http://localhost:8000/api/decide \ -H 'Content-Type: application/json' \ -d '{"state": "The login page keeps spinning and never loads", "questions": {"team": {"type": "choice", "instructions": "Which team should handle this ticket?", "criteria": {"account": "account", "billing": "billing", "returns": "returns", "sales": "sales", "shipping": "shipping", "technical": "technical"}}}}' ``` - Check: the team answer's `explanation` has `"by":"deferred"` and `"rule":"deferred: the top probability here, 0.447, is below the threshold 0.625"`, and under `local` System Zero's own answer with its file and line. A ticket the pack is sure of, such as `I was charged twice for my plan this month`, is answered here and sends nothing. - If the endpoint fails or does not answer in time, the question keeps System Zero's own answer with a `deferral` error, the request still succeeds, and the server logs one line: `system-zero: --defer-to http://127.0.0.1:8001/v1/systemone: cannot reach it ([Errno 111] Connection refused); System Zero answered 1 question(s) itself`. - The one-shot command and `-j` take `--defer-to` and `--threshold` as well. - Without `--defer-to`, the caller can do the same, as in the threshold check of 5.4. - Stop the stand-in when done: `kill` its process id, or Ctrl+C in its terminal. ## 7. Troubleshooting - Python older than 3.9: pip finds no matching distribution for sr-zero. Use uv, or `uv tool install --python 3.12 sr-zero`, which fetches that Python. - `sr-zero: command not found` after `uv tool install`: uv's tool directory is not on PATH. Run `uv tool update-shell` and open a new shell. - `externally-managed-environment` from pip: use uv, or the virtual environment of step 2. `ensurepip is not available` from `python3 -m venv`: install `python3-venv` with the user's OK, or use uv. - The download: `uvx` and `uv tool install` fetch the package, and a Python when none fits; behind a proxy, set `HTTPS_PROXY`. After that System Zero needs no network. `uvx sr-zero@latest` picks up a new release. - Quoting: put text and questions in single quotes. Inside single quotes an apostrophe is written `'\''`, as in `'I can'\''t log in'`. Double quotes expand `$` and backticks, and in an interactive bash or zsh also the exclamation mark; use them for text with none of these. An unquoted `?` or `*` makes zsh stop with `no matches found`. For anything longer, put the request in a JSON file and use `sr-zero -j FILE` or `curl -d @FILE`. - `the port is in use (is another server running?); choose another port`: stop the other server, or pass another `--port`. - A pack refuses to load: the message names the file and line to fix. `... is changed since the manifest sealed it` is a note: seal again once the checks pass. - `has no question decision`: the pack was compiled from JSON lines; give `-q` with `--yes-no` or `-o`. - `--defer-to ...: cannot reach it`: the endpoint is down or its URL is wrong; System Zero answered those questions itself and marked them with `deferral`. ## Limits - Language understanding and world knowledge stay with neural models (step 0). - Wording matters: the rules are keyed to how a question is asked; ask it the way the rules were built for, or build a Zero pack from your own questions and wording. - Small packs are weak on paraphrased text. In the 600-ticket example the pack is right on 51.7% of dev tickets, against 20.6% for the official rules alone, and no threshold reaches the 90% target. `pack test` shows this before anything is served; defer what the pack is not sure of. - Probabilities are lower bounds of records counted in training. Choose the threshold on dev data, not by the probabilities' face value. - Under distribution shift some families of decisions are confidently wrong. Keep a dev set that looks like current traffic, and run the gate again when traffic changes. - Text alone: no images, no `box` questions, no span answers. The modules are written for English. - The code is Apache-2.0. The rule tables in `system_zero/rules/` are counted from the datasets and data pools listed in `DATA_SOURCES.md`, whose licences are mixed, including non-commercial, share-alike, research-only and unknown ones; their licence, `LicenseRef-System-Zero-Rule-Tables`, is described in `NOTICE`. Check `DATA_SOURCES.md` before commercial use; both files are installed with the package, in its `.dist-info/licenses/` directory. - With `--defer-to`, the questions System Zero is not sure of leave the machine, with their state, for the endpoint given and no other. ## Links - Site: https://system-zero.semantic-router.org - This guide: https://system-zero.semantic-router.org/llms.txt - Agent skill: https://system-zero.semantic-router.org/skill/SKILL.md - Package: https://pypi.org/project/sr-zero/