result = raxe.scan("test prompt")# Boolean evaluation - True when SAFEif result: # True when no threats print("Safe to proceed")else: print("Threat detected")# Propertiesresult.has_threats # bool: True if any threat detectedresult.severity # str | None: "critical", "high", "medium", "low", "info"result.total_detections # int: Number of detections (L1 + L2)result.detections # list[Detection]: All L1 detectionsresult.duration_ms # float: Total scan time in millisecondsresult.should_block # bool: True if policy says to blockresult.l1_detections # int: Count of L1 detectionsresult.l2_detections # int: Count of L2 predictionsresult.text_hash # str: SHA256 hash of scanned text (for logging)
for detection in result.detections: for match in detection.matches: match.matched_text # str: The matched content (for debugging only) match.start # int: Start position in original text match.end # int: End position in original text
For latency-sensitive applications (FastAPI, real-time APIs), use background scan mode. The scan runs asynchronously on a worker thread — your code continues immediately with ~0ms overhead.
background_mode.py
from raxe import Raxefrom raxe.sdk.agent_scanner import AgentScannerConfig, create_agent_scannerraxe = Raxe()scanner = create_agent_scanner( raxe, AgentScannerConfig( on_threat="log", execution_mode="background", on_threat_callback=lambda r: print( f"Threat: severity={r.severity}, hash={r.prompt_hash}" ), ),)# Returns in <1ms — scan runs in backgroundscanner.scan_prompt("user input")# Your code continues immediatelyresult = call_llm(prompt)
from raxe import Raxe# Automatic cleanup - recommended for scripts and short-lived processeswith Raxe() as raxe: result = raxe.scan("test prompt") # Process result...# Telemetry automatically flushed on exit
from raxe import Raxe, RaxeBlockedError, RaxeExceptionraxe = Raxe()try: result = raxe.scan(user_input, block_on_threat=True)except RaxeBlockedError as e: # Threat was detected and blocked by policy print(f"Blocked: {e.result.severity}") print(f"Detections: {e.result.total_detections}") print(f"Rule: {e.rule_id}")except RaxeException as e: # Other RAXE errors (config, initialization, etc.) print(f"Error: {e}") # Decide: fail open (allow) or fail closed (block)
from raxe import Severityresult = raxe.scan(user_input)# Filter by category (threat family)pi_threats = [d for d in result.detections if d.category == "PI"] # Prompt Injectionde_threats = [d for d in result.detections if d.category == "DE"] # Data Exfiltration# Filter by severity (using enum for type safety)critical = [d for d in result.detections if d.severity == Severity.CRITICAL]high_and_above = [d for d in result.detections if d.severity >= Severity.HIGH]# Filter by confidence thresholdhigh_confidence = [d for d in result.detections if d.confidence >= 0.9]
# Override the tenant/app default policy for specific requestsresult = raxe.scan( prompt, tenant_id="acme", app_id="chatbot", policy_id="strict" # Force strict mode for this request)
from concurrent.futures import ThreadPoolExecutorfrom raxe import Raxeraxe = Raxe() # Single instance - reuse across threadsdef scan_prompt(prompt: str): return raxe.scan(prompt)# Safe to use from multiple threadswith ThreadPoolExecutor(max_workers=10) as executor: results = list(executor.map(scan_prompt, prompts))