-
Notifications
You must be signed in to change notification settings - Fork 132
javascript fetch for local llm decisions
From JavaScript you call a local decision server with a plain fetch POST of JSON to
/v1/systemone and read answers.<name>.noul, the probability of yes. In Node.js, where the
global fetch is stable since v21.0.0, that is all there is to it, plus a timeout and a check of
response.ok, because fetch does not reject on HTTP errors. In the browser it works only
from a page served by the same origin as the server: the server sends no CORS headers,
so a page on another origin is blocked by the browser.
The CORS point is the one that costs people an afternoon. The same request that works from curl or Node fails in the browser, and the error JavaScript sees says nothing useful about why.
This page is the Node.js version, error handling, the browser rule and the two ways around it, and where the API key must never go. The snippets are minimal sketches to adapt, using only the documented request and response fields.
The Node.js documentation lists the global fetch as added in v17.5.0 and v16.15.0 and no
longer experimental since v21.0.0. AbortSignal.timeout is there too, from v17.3.0 and v16.14.0.
const JEV_URL = process.env.JEV_URL ?? "http://127.0.0.1:8017";
async function decide(state, questions, ms = 10000) {
const body = {
model: "jev-latest",
state,
questions: Object.fromEntries(
Object.entries(questions).map(([k, q]) => [k, { type: "noul", instructions: q }])
),
};
const headers = { "Content-Type": "application/json" };
if (process.env.JEV_API_KEY) headers.Authorization = `Bearer ${process.env.JEV_API_KEY}`;
const res = await fetch(`${JEV_URL}/v1/systemone`, {
method: "POST", headers, body: JSON.stringify(body), signal: AbortSignal.timeout(ms),
});
if (res.status === 422) throw new Error(JSON.stringify((await res.json()).detail));
if (!res.ok) throw new Error(`jev: HTTP ${res.status}`);
const { answers } = await res.json();
return Object.fromEntries(Object.entries(answers).map(([k, a]) => [k, a.noul]));
}decide("I was charged twice for the same order.", { billing: "Is this a billing problem?" })
is the README's Quickstart request (the README shows 0.94 for it). Put every question about one
text in the same call: the state is read once, so on the reference laptop three questions take
about 66 ms together against 49 ms for one alone. The request shape is described in full on
ask a local LLM a yes/no question and get P(yes).
MDN is explicit: "A fetch() promise only rejects when the request fails, for example, because
of a badly-formed request URL or a network error", and it does not reject on 404, 504 and
the like. So the code checks the status itself. What each case means here:
-
Network error or connection refused. The server is not up, or is still loading the model:
jev serveopens its port only once the model is loaded. PollGET /healthuntil it returns{"status": "ready", ...}. -
TimeoutError. The signal fromAbortSignal.timeoutaborts with aTimeoutErrorDOMException, which MDN distinguishes from theAbortErrorof a user abort. Requests share one model (small ones arriving together are read in one model call), so a timeout under load usually means the queue is long, not that the model hangs. -
422. The body is{"detail": [{"loc": [...], "msg": "...", "type": "..."}]}. Common causes: a model name that is notjev-*or the served model, a misspelt field (unknown fields are rejected), achoicewith one option or ascorewith one level, or a question whose prompt, the state plus that question, is over the context limit. -
401. The server was started withJEV_API_KEYand the header is missing or wrong.
Do not retry a 422 or a 401; the same request fails the same way.
A POST with Content-Type: application/json is not a "simple" request in the CORS sense, and a
request with an Authorization header is not either. MDN's guide lists both as triggers for a
preflight OPTIONS request, after which the server must answer with
Access-Control-Allow-Origin and the allowed headers. The jev server adds no CORS
headers, so none of those headers are sent, and per MDN the browser then blocks access to the
response and reports a CORS error that "for security reasons" JavaScript cannot inspect.
For your own front end, two designs work:
-
Call jevos from your backend. The browser talks to your server, your server calls
127.0.0.1:8017. This is the normal design and keeps the decision server off the network. -
Put both behind one origin. A reverse proxy serves your page and forwards
/v1/to the decision server, so the browser sees one origin. This also gives you a place for TLS; see securing a local LLM server with an API key.
Adding CORS headers to the server is possible in your own fork, but a permissive setting would
let any page a user opens read answers from a server on their machine, which is what the browser
rule exists to prevent. If you do it, allow one named origin, not *.
Never ship JEV_API_KEY to a browser. Anything in front-end code is readable by whoever loads
the page, and the server has one key for all callers. Keep the key in the backend's environment,
as the Node sketch does, and let the browser authenticate to your backend in whatever way it
already does.
Nothing about the model changes with the language. The same limits apply: English only, yes/no, choice and (early) score questions, and a probability whose reliability depends on the kind of question. On our 999-question set a small model leaned toward yes on arithmetic and dates, which is why small LLMs and arithmetic in yes/no questions says to compute numbers in code, and in JavaScript that is one line.
Which Node.js version do I need? One with a global fetch: added in v17.5.0 and v16.15.0,
stable since v21.0.0, according to the Node.js docs.
Why does my browser say CORS error when curl works? curl does not enforce CORS; browsers do. The server sends no CORS headers, so only same-origin pages can read its answers.
Can I use axios instead of fetch? Any HTTP client works: it is one JSON POST. The error handling rules are the same.
How do I set a timeout on fetch? Pass signal: AbortSignal.timeout(ms) and catch
TimeoutError.
Can a web page call the server with the API key? It can, but it should not: the key would be visible to every visitor.
See also: a Python client for local LLM decisions, curl examples for a local LLM decision API and a Slack bot that uses local LLM decisions.
- Request and response fields, 422 and 401 behaviour, the absence of CORS headers, the shared model call for small requests: read from the source of jev.
- Latencies and the billing example: the jev README.
- Node.js globals: fetch and AbortSignal.timeout, fetched 2026-09-29.
- MDN: Window.fetch(), MDN: AbortSignal.timeout() and MDN: CORS guide, fetched 2026-09-29.
From the notes of jev, a yes/no decision model that runs on a laptop CPU. The server sends no CORS headers, so the browser question is decided by where the page is served from, not by the code.
- Ask a local LLM a yes/no question and get P(yes)
- Zero-shot text classification with yes/no questions
- LLM policy decisions: put the rule in the question
- LLM as a judge on a CPU
- Why a small LLM says yes when the answer is no
- Small LLMs and arithmetic in yes/no questions
- Our held-out benchmark said 0.855, new questions said 0.757
- jevos vs Jev vs Laya for yes/no decisions
- An open-source alternative to Jev for yes/no decisions
- jevos vs the OpenAI API for yes/no classification
- jevos vs Ollama for yes/no decisions
- jevos vs bart-large-mnli for zero-shot classification
- A yes/no LLM vs a fine-tuned BERT classifier
- jevos vs SetFit: zero-shot vs few-shot classification
- jevos vs Llama Guard for content safety checks
- jev serve vs llama.cpp server for classification
- jevos vs LM Studio: a decision server, not a chat app
- Local vs hosted LLM decisions: latency, cost, privacy
- A yes/no LLM vs a business rules engine
- LLM decisions vs keyword rules and regex
- The fastest AI model for yes/no decisions
- What makes a local LLM fast on a CPU
- Why one forward pass beats generating an answer
- Prefill vs decode: where LLM latency comes from
- Why LLM latency grows with the length of the text
- Why a hosted LLM API cannot answer in 50 ms
- Many questions about one text: why the extra ones are cheap
- CPU or GPU for a small LLM
- Latency budgets: where a 200 ms model fits
- Measuring LLM latency: median, p90 and warm-up
- Q4_K_M vs Q8_0: speed and size for a small model
- Throughput vs latency for a decision server
- What P(yes) means, and what it does not
- LLM calibration explained with yes/no answers
- Expected calibration error (ECE), explained
- Temperature scaling for LLM probabilities
- Platt scaling for a yes/no model
- Reading a reliability diagram
- How to choose a threshold for P(yes)
- Thresholds when a wrong yes costs more than a wrong no
- Human in the loop AI with a review band
- Precision and recall at a P(yes) threshold
- Base rates: why a 0.9 yes can still be wrong often
- Combining yes/no answers with AND, OR and NOT
- Logits, log-odds and P(yes)
- LLM confidence scores: probabilities vs self-reports
- How to write yes/no questions an LLM answers well
- Negation in yes/no questions for an LLM
- One condition per question: splitting compound questions
- Ask whether the text says it at all
- Scores as yes/no thresholds: is it at least high?
- Sending JSON as the text: designing the state
- Why wording changes an LLM's answer, and how to test it
- Mainly about: questions for messages with several topics
- Yes/no questions about tone and emotion
- Asking about intent: what does the writer want?
- Yes/no questions about long documents
- Using an English-only LLM with other languages
- Content moderation with a local LLM
- A Discord moderation bot with a local LLM
- Spam detection with yes/no questions
- Review moderation with a local LLM
- Email triage with a local LLM
- Support ticket routing with yes/no questions
- Urgency detection in customer messages
- Sentiment analysis with yes/no questions
- Intent detection with a local LLM
- Lead qualification with yes/no questions
- Fraud case triage with a local LLM
- Phishing email screening with a local LLM
- Log and alert triage with a local LLM
- Checking text for personal data with yes/no questions
- Prompt injection screening with a small model
- Document classification with a local LLM
- Product categorization with yes/no questions
- Contract clause detection with a local LLM
- Refund request triage with a local LLM
- Detecting cancellation intent in customer messages
- RAG evaluation with yes/no questions
- RAG faithfulness check with a local LLM
- Hallucination detection with a local LLM
- LLM regression tests in CI with yes/no checks
- Rubric design for an LLM judge
- Pairwise comparison with a yes/no judge
- LLM judge bias and how to control it
- Evaluation metrics for yes/no classifiers
- Building a yes/no test set for your own data
- Accuracy by kind of question: why one number hides failures
- Generating test questions with answers computed by code
- Benchmark contamination and truly held-out tests
- An LLM router with yes/no questions
- A model cascade: small model first, large model on doubt
- Semantic routing vs yes/no questions
- Gating AI agent tool calls with yes/no checks
- AI agent guardrails with yes/no questions
- Stop conditions for AI agents
- Logging LLM decisions for audit
- Reducing LLM cost with local yes/no decisions
- Replacing chat LLM calls with yes/no questions
- Structured output vs a probability
- A Python client for local LLM decisions
- Calling a local LLM decision server from JavaScript
- Local LLM yes/no decisions in n8n
- A Slack bot that uses local LLM decisions
- Home Assistant automations with local LLM decisions
- A LangChain tool for local yes/no decisions
- Batch decisions from files with jev decide
- Running LLM yes/no checks in GitHub Actions
- Securing a local LLM server with an API key
- curl examples for a local LLM decision API
- Self-hosted AI for decisions
- A private LLM for text classification
- On-premise LLM for business decisions
- GDPR and automated decision-making with an LLM
- Offline AI for decisions: no network needed
- Edge AI decisions on a CPU
- Run an LLM locally without a GPU
- Small language models explained
- When a small model is enough, and when it is not
- An LLM on a laptop: what it can do in real time
- What is GGUF, for someone deploying a classifier
- GGUF quantization types explained: Q4_K_M, Q8_0 and others
- GGUF vs safetensors
- llama.cpp vs Ollama for a classification service
- llama-cpp-python vs calling llama.cpp through ctypes
- llama.cpp on Windows without compiling
- Running llama.cpp CPU only
- Using llama.cpp prebuilt binaries instead of building