Composable runtime safety for every NIM API call. Input filtering, PII scrubbing, cost limits, tool auditing. Zero external services. One import.
Test the exact same guardrail logic that runs in Python — no API key, no backend.
Guardrails run before the NIM call, around tool execution, and on the final output. Short-circuit on the first failure.
TurnRecord + full audit log (JSON).
from nim_guardrails import (
NimClient, GuardedAgent, AgentConfig,
GuardrailChain, InputLengthGuardrail,
BlockedTopicGuardrail, PiiDetectionGuardrail, CostLimitGuardrail
)
client = NimClient() # reads NVIDIA_API_KEY
input_guards = GuardrailChain([
InputLengthGuardrail(max_chars=2000),
BlockedTopicGuardrail(),
CostLimitGuardrail(max_total_tokens=50_000),
])
agent = GuardedAgent(
client=client,
config=AgentConfig(system_prompt="You are a helpful travel assistant."),
input_guardrails=input_guards,
output_guardrails=GuardrailChain([PiiDetectionGuardrail()]),
)
response, record = agent.run("Plan a trip to Tokyo")
print(response)
print("Blocked?", record.blocked)
print("Latency:", record.latency_ms)