Skip to content

Deterministic AI Agent Reliability & Prefix-Affinity LLM Routing

Repositories: github.com/cuhawk/AgentReliabilityLab · github.com/cuhawk/Harness3 · github.com/cuhawk/p2InferRoute


Three Failure Modes That Break Production AI Agents

Shipping a tool-using LLM agent from demo to production usually breaks in three places simultaneously:

  1. Unreproducible evaluations ("vibes & LLM-as-a-judge"): A judge LLM marks a run "passed" even when the agent skipped human approval, leaked a secret into tool arguments, or succeeded by accident on retry #4.
  2. Non-idempotent crashes mid-workflow: A worker restarts after calling a write tool but before recording progress—causing duplicate side effects or orphaned approvals on resume.
  3. Blind round-robin inference routing: Spraying multi-turn agent prompts evenly across model replicas destroys KV-cache locality, spiking p99 Time-to-First-Token (TTFT) and Time-per-Output-Token (TPOT).

Here is how three modular systems solve those three layers—with exact, reproducible measurements.


Layer 1: Deterministic Trace Evaluation & Judge Calibration (AgentReliabilityLab)

flowchart LR
    YAML[92 Declarative Scenarios<br/>17 Categories] --> Adapter[Agent Adapter<br/>HTTP / OpenAI / MCP]
    Adapter --> Gateway[Managed Tool Gateway<br/>Policy Check -> Ordinal Faults -> Sandbox]
    Gateway --> Trace[(SQLite Trace + OTel Spans)]
    Trace --> Eval[13 Pure-Function Evaluators<br/>RLVR Step Verifier]
    Eval --> Stats[Wilson 95% CIs · pass@k / pass^k<br/>Rogan-Gladen Calibration · CI Gate]

AgentReliabilityLab (ARL) replaces random seeds and opaque judge prompts with ordinal-keyed fault injection (21 fault types across Tool, Model, Retrieval, State, and Run domains—e.g. "2nd call to customer_api returns HTTP 500") and 13 pure-function evaluators (completion, output_format, tools, tool_args, retry, state, evidence, authorization, approval, secrets, injection, cost, latency).

Measured Wave 5 Results (suites/full.yaml, 3 Trials, Seed 42)

  • Guarded reference agent: 92 / 92 scenarios and 276 / 276 trials passed (Wilson 95% CI [98.63%, 100%]), achieving 100% pass@k and pass^k for k = 1, 2, 3, including all 8 reward-hacking & verifier-integrity scenarios.
  • Runtime speed: 0.644 ms p50 / 34.763 ms p95 per trial (100% offline, zero API keys).
  • Why naive LLM judges fail (empirical calibration result): When calibrating a scripted final-status judge against deterministic ground-truth labels on the weak agent, ARL measured Cohen's $\kappa = 0.1027$, TPR = 100%, and TNR = 9.52%—passing 41 / 46 (89.13%) held-out trials despite widespread safety/policy violations. Rogan–Gladen prevalence correction immediately exposed the illusion (0% clipped estimate, 95% CI [0%, 100%]).
  • Interoperability: arl export-inspect suites/full.yaml --output inspect-tasks.jsonl exports all 92 scenarios directly into UK AISI Inspect AI format.

Layer 2: Crash-Safe Durable Execution & Signed HITL Gates (Harness3)

Evaluation tells you whether an agent behaves safely; Harness3 enforces durable execution and authorization at runtime:

  • Transactional SQLite Checkpoints: Every step commits alongside local side effects (or pairs with a provider idempotency key for remote calls). If the process dies mid-step, resume(principal, run_id) replays cleanly from the first uncommitted step.
  • Cryptographically Bound Human-in-the-Loop (HITL): Privileged steps halt with awaiting_approval, surfacing requester, operation, tool, and exact argument hash. An authenticated same-tenant reviewer signs the exact ToolCall (TenantPolicy.sign) before execution resumes.
  • Measured Wave 5 Verification (300 pytest + 13 subtests):
  • Crash recovery: 6 / 6 injected process exits recovered with 0 duplicate side effects.
  • Checkpoint overhead: 1.4042 ms p50 / 2.2433 ms p95 (200 samples).
  • Signed HITL authorization: 0.0233 ms p50 / 0.0509 ms p95.

Layer 3: Prefix-Affinity & Schema-Cascade LLM Routing (p2InferRoute)

p2InferRoute is an OpenAI-compatible FastAPI gateway with deterministic strategy scoring (lowest_latency, lowest_cost, private, task_aware, prefix_affinity), circuit breakers, streaming pre-first-token fallback, strict JSON Schema Draft 2020-12 cascading, and Prometheus SSE TTFT/TPOT/SLO Goodput telemetry.

Live llama.cpp Replica A/B Benchmark (Wave 5)

Measured across two live llama.cpp CPU replicas (SmolLM2-135M Q4_K_M, 64 streaming requests per arm, concurrency 4, SLOs 400 ms TTFT / 150 ms TPOT):

Routing Policy KV Cache Hit Rate TTFT p50 TTFT p95 TTFT p99 Mean TPOT SLO Goodput
round_robin 46.74% 1,262.55 ms 2,330.90 ms 2,992.43 ms 50.30 ms 0.42 req/s
prefix_affinity 55.76% (+9.02 pp) 1,170.71 ms 2,272.40 ms 2,281.05 ms (-23.8%) 29.33 ms (-41.7%) 0.41 req/s

Using rendezvous hashing for cold prefixes and longest-known-prefix preference (with per-worker in-flight spill thresholds) improved cache reuse and slashed p99 TTFT and mean decode latency without sacrificing availability.


Reproduce Offline in 60 Seconds

# 1. AgentReliabilityLab — full offline 92-scenario eval & Inspect AI export
git clone https://github.com/cuhawk/AgentReliabilityLab.git && cd AgentReliabilityLab
uv sync
uv run arl eval suites/full.yaml --agent reference:guarded --trials 3 --k 1 --k 2 --k 3 --seed 42
uv run arl export-inspect suites/full.yaml --output inspect-tasks.jsonl

# 2. p2InferRoute — gateway & routing A/B benchmark
git clone https://github.com/cuhawk/p2InferRoute.git && cd p2InferRoute
uv sync && uv run pytest -q

Launch Interactive Browser Demos View AgentReliabilityLab on GitHub