Middleware implementing the Box Maze process-control architecture for LLM reasoning safety. Decomposes LLM reasoning into three validated layers—memory grounding, structured inference, and boundary enforcement—to reduce adversarial boundary failures from ~40% to under 1%.
Based on arXiv:2603.19182.
User Input
│
▼
┌──────────────────────┐
│ Memory Grounding │ Sanitizes input, anchors system context
│ Layer │ Detects injection attempts
└──────────┬───────────┘
│ LayerResult
▼
┌──────────────────────┐
│ Structured │ Adds chain-of-thought scaffolding
│ Inference Layer │ Calls the LLM within constraints
└──────────┬───────────┘
│ LayerResult
▼
┌──────────────────────┐
│ Boundary │ Validates output against BoundarySpec
│ Enforcement Layer │ Blocks critical violations
└──────────┬───────────┘
│
▼
GuardResult
Each layer validates the output of the previous one. If any layer detects a critical violation (severity >= 0.8), the pipeline short-circuits and returns a refusal.
from process_guard import ProcessGuard
from boundary_checker import BoundarySpec
def my_llm(prompt: str) -> str:
# Your LLM call here
return call_api(prompt)
guard = ProcessGuard(
llm=my_llm,
spec=BoundarySpec(allowed_topics=["math", "science"]),
system_context="You are a helpful math tutor.",
)
result = guard.run("What is the derivative of x^2?")
print(result.text) # LLM response
print(result.accepted) # True if all boundaries held
print(result.violations) # List of any detected violationsBoundarySpec controls what the guard checks:
| Field | Default | Description |
|---|---|---|
allowed_topics |
[] (any) |
Restrict output to these topics |
blocked_patterns |
injection regexes | Regex patterns that trigger violations |
max_output_tokens |
4096 |
Maximum output length in whitespace-delimited tokens |
role_identity |
"helpful assistant" |
Expected role for the model |
pip install pytest
python -m pytest test_guard.py -v