Deterministic JSON repair, confidence scoring, progressive tool disclosure, and session memory — built for 2B–4B on-device models that hallucinate tool calls.
2B–4B parameter models are fast and cheap, but they output broken JSON, hallucinate tool names, repeat the same failed call, and eat your context window. This harness fixes all of it — deterministically, without re-prompting.
# What the model actually outputs:
{'tool': 'speak', 'args': {'text': 'hello', 'speed': '1.5'}}
# What the harness produces:
{"tool": "21labs.speak", "arguments":
{"text": "hello", "engine": "kokoro", "speed": 1.5}}
Five modules, one import, zero config.
Fixes malformed JSON without re-prompting. Trailing commas, single quotes, unquoted keys, markdown fences, type drift, camelCase/snake_case mismatch. Pipeline: rename → coerce → inject defaults.
Multi-signal scoring: schema match (35%), history (25%), completeness (25%), repair cost (15%). Decides whether to trust the local model or escalate to cloud.
Small models fail on tool selection with too many options. Ranks tools by query relevance, shows only the top N. Keyword overlap, historical success, category matching.
Tracks success/failure per tool, detects same-tool loops, injects steering hints into the system prompt, monitors context pressure, compacts responses to save tokens.
25 test cases across 4 categories. 100% pass rate. 9.4ms total.
$ python benchmarks/benchmark.py
--- Benchmark 1: JSON Repair ---
[PASS] trailing comma 0.4ms
[PASS] single quotes 0.1ms
[PASS] missing quotes on keys 0.4ms
[PASS] markdown fences 0.1ms
[PASS] leading garbage 0.1ms
[PASS] string number arg 0.1ms
[PASS] boolean as string 0.1ms
[PASS] camelCase keys 0.3ms
[PASS] comments in JSON 0.1ms
[PASS] empty arguments 0.1ms
--- Benchmark 2: Confidence Scoring ---
[PASS] clean call 0.0ms
[PASS] missing required 0.0ms
[PASS] unknown tool 0.0ms
[PASS] heavy repairs 0.0ms
[PASS] avoided tool 0.0ms
BENCHMARK RESULTS: 25/25 passed (100%)
Total time: 9.4ms
Three lines to repair, score, and rank. The harness handles the rest.
from small_model_harness import (
create_harness_session,
compact_tool_response,
)
from small_model_harness.tool_repair import repair_tool_call
from small_model_harness.confidence import score_tool_call
from small_model_harness.tool_disclosure import rank_tools
# 1. Session with auto-budget from context window
harness = create_harness_session(n_ctx=4096)
# 2. Repair malformed model output
raw = \'{"tool": "speak", "args": {"text": "hi"}}\'
args, tool, fixes = repair_tool_call(raw, schemas)
# 3. Score confidence
score = score_tool_call(tool, args, schema, harness)
if score.should_escalate:
escalate_to_cloud()
# 4. Rank tools for next turn
top = rank_tools("generate speech", schemas, harness)
One dependency. Works everywhere Python runs.
$ pip install small-model-harness