Token Reduction Engine
Deterministic AI validation middleware. Catches hallucinations before they reach users. Zero LLM calls required for fact-based queries. Typical results: 20% token savings, 95%+ accuracy on facts.
Quick Reference
# Validate a single response
python3 scripts/hguard_client.py validate "What is 2+2?" "4"
# Batch validate from file
python3 scripts/hguard_client.py batch input.json output.json
# Check Brain API status
python3 scripts/hguard_client.py status
Installation
clawhub install token-reduction-engine
Python API
from scripts.hguard_client import HGuardClient
# Uses localhost:8000 by default. Override with CERTAINLOGIC_API env var.
client = HGuardClient()
# Validate any AI-generated text
result = client.validate("What is Docker?", "Docker is a containerization platform.")
print(result["valid"]) # True
print(result["confidence"]) # 1.0
# Check for hallucinations
bad = client.validate("What is Python recursion depth?", "Depth is 500.")
print(bad["valid"]) # False
print(bad["flags"]) # ["Factual mismatch: ..."]
# Batch process
results = client.batch_validate([
{"query": "Q1", "response": "A1"},
{"query": "Q2", "response": "A2"},
])
# Check API status
status = client.status()
Configuration
Set the Brain API URL via environment variable:
export CERTAINLOGIC_API="http://localhost:8000"
Or pass it directly to the client:
client = HGuardClient(api_url="http://your-brain-api:8000")
Troubleshooting
| Issue | Fix |
|---|
| "Brain API not found" | Start the brain service at localhost:8000 |
| "No facts loaded" | Load facts via Brain API endpoint |
| Validation too strict | Adjust threshold in code: client.set_threshold(0.5) |
Uninstall
clawhub uninstall token-reduction-engine
License
MIT-0 (Free, no attribution required)
Note
Requires a running Brain API instance. This package provides the client only. The server must be configured separately.