Core Controls
Deterministic seed, cooperative cancel, tool-arg validation, and PII redaction β four production switches with one-line opt-in.
Four small switches that make a PraisonAI agent safer, more deterministic, and easier to interrupt in production.
The user starts a run; core controls enforce safety policies during execution.
from praisonaiagents import Agent
agent = Agent(
name="assistant",
instructions="Run with guardrails, interrupts, and validation enabled",
)
agent.start("Execute this task safely")graph TD
Caller["π Your Code"] -->|"agent.chat(..., seed=42, cancel_token=tok)"| Agent["π€ Agent"]
Agent -->|BEFORE_LLM hook| Scrub["π§Ή PII Redactor"]
Scrub --> LLM["π§ LLM"]
LLM -->|tool call| Validator["π‘οΈ Tool Validator"]
Validator -->|valid| Tool["π§ Tool"]
Validator -->|invalid| Reject["β Rejected"]
classDef agent fill:#8B0000,stroke:#7C90A0,color:#fff
classDef tool fill:#189AB4,stroke:#7C90A0,color:#fff
classDef gate fill:#F59E0B,stroke:#7C90A0,color:#fff
class Caller,Agent,LLM agent
class Tool,Scrub,Validator tool
class Reject gateQuick Start
- PII Redaction
python from praisonaiagents import Agent from praisonaiagents.trace import enable_pii_redaction enable_pii_redaction() # idempotent; safe to call multiple times agent = Agent( name="helper", instructions="You are a concise assistant.", model={"model": "gpt-4o-mini"}, ) print(agent.chat("My api_key=sk-XYZ β what is 2+2?")) - Deterministic Seed & Cancellation
python from praisonaiagents import Agent from praisonaiagents.agent.interrupt import InterruptController agent = Agent(name="helper", instructions="You are a concise assistant.", model={"model": "gpt-4o-mini"}) tok = InterruptController() reply = agent.chat("Pick a number 1-100.", seed=42, cancel_token=tok) - Tool Validation
python from praisonaiagents import Agent from praisonaiagents.tools.validators import ValidationResult class RangeValidator: def validate_args(self, tool_name, args, context=None): if tool_name == "set_temperature" and not 0 <= args.get("value", 0) <= 100: return ValidationResult(valid=False, errors=["temperature 0-100"]) return ValidationResult(valid=True) def validate_result(self, tool_name, result, context=None): return ValidationResult(valid=True) def set_temperature(value: float) -> str: return f"set to {value}" agent = Agent(name="thermostat", tools=[set_temperature], model={"model": "gpt-4o-mini"}) agent._tool_validator = RangeValidator() print(agent.chat("Set the temperature to 500"))
How It Works
sequenceDiagram
participant Caller
participant Agent
participant Scrubber as PII Hook
participant LLM
participant Validator
participant Tool
Caller->>Agent: chat(prompt, seed, cancel_token)
Agent->>Agent: cancel_token.is_set()? (per tool iteration, all providers)
Agent->>Agent: cancel_token.is_set()? (immediately before tool dispatch, all providers)
Agent->>Scrubber: BEFORE_LLM (scrub messages)
Scrubber-->>Agent: redacted messages
Agent->>Agent: cancel_token.is_set()? (last-chance)
Agent->>LLM: completion(seed, messages, tools)
LLM-->>Agent: tool_call(name, args)
Agent->>Validator: validate_args(name, args)
alt valid
Validator-->>Agent: ok
Agent->>Tool: run(args)
Tool-->>Agent: result
else rejected
Validator-->>Agent: ValidationResult(valid=False)
Agent-->>LLM: "Tool arguments rejected: ..."
end
Agent-->>Caller: replyConfiguration Options
| Option | Type | Default | Description | |
|---|---|---|---|---|
seed | `int \ | None` | None | Per-call random seed forwarded to the LLM. Overrides any seed set on the LLM instance for this call only. |
cancel_token | `InterruptController \ | None` | None | Cooperative cancellation token. When is_set() returns true at a checkpoint, raises InterruptedError(reason). Falls back to agent.interrupt_controller if not provided. Threaded through both the LiteLLM and OpenAI-native paths for sync chat(); async achat() currently only honours it on the LiteLLM path. Also forwarded end-to-end on the streaming path (start(stream=True) / iter_stream()) since PR #5081. |
Deterministic seed
Per-call determinism for reproducible outputs.
graph LR
Caller[π Caller] --> Agent["π€ Agent.chat(seed=42)"]
Agent --> Kwargs["π§ llm_kwargs['seed']=42"]
Kwargs --> LLM["π§ LLM"]
LLM --> Output["π Deterministic Output"]
classDef input fill:#6366F1,stroke:#7C90A0,color:#fff
classDef process fill:#F59E0B,stroke:#7C90A0,color:#fff
classDef output fill:#10B981,stroke:#7C90A0,color:#fff
class Caller input
class Agent,Kwargs process
class LLM,Output outputfrom praisonaiagents import Agent
agent = Agent(
name="helper",
instructions="You are a concise assistant.",
model={"model": "gpt-4o-mini"},
)
# Same seed β same model output (provider-permitting)
print(agent.chat("Pick a number 1-100", seed=42))
print(agent.chat("Pick a number 1-100", seed=42))Cooperative cancellation
Thread-safe cancellation without hard kills.
graph LR
Thread["π§΅ Other Thread"] --> Request["β οΈ tok.request(reason)"]
Request --> Flag["π© Flag Set"]
Chat["π¬ Agent.chat()"] --> Checkpoint["π Checkpoint"]
Flag --> Checkpoint
Checkpoint --> Error["β InterruptedError"]
classDef thread fill:#6366F1,stroke:#7C90A0,color:#fff
classDef signal fill:#F59E0B,stroke:#7C90A0,color:#fff
classDef result fill:#8B0000,stroke:#7C90A0,color:#fff
class Thread thread
class Request,Flag,Checkpoint signal
class Chat,Error resultimport threading
from praisonaiagents import Agent
from praisonaiagents.agent.interrupt import InterruptController
agent = Agent(name="worker", instructions="You are a helpful assistant.", model={"model": "gpt-4o-mini"})
tok = InterruptController()
def do_work():
try:
print(agent.chat("Write a long essay about Rome.", cancel_token=tok))
except InterruptedError as e:
print(f"Aborted cleanly: {e}")
threading.Thread(target=do_work).start()
# β¦ later, from any thread:
tok.request("user pressed stop")Tool argument validation
Validate tool arguments before execution.
graph LR
LLM["π§ LLM tool_call"] --> Hook["πͺ BEFORE_TOOL hook"]
Hook --> Validator["π‘οΈ validator.validate_args"]
Validator --> Valid{"β
valid?"}
Valid -->|yes| Tool["π§ Tool Function"]
Valid -->|no| Rejection["β rejection string β LLM"]
Tool --> Result["π Result"]
classDef llm fill:#8B0000,stroke:#7C90A0,color:#fff
classDef process fill:#189AB4,stroke:#7C90A0,color:#fff
classDef result fill:#10B981,stroke:#7C90A0,color:#fff
classDef error fill:#F59E0B,stroke:#7C90A0,color:#fff
class LLM llm
class Hook,Validator,Valid process
class Tool,Result result
class Rejection errorfrom praisonaiagents import Agent
from praisonaiagents.tools.validators import ValidationResult
class RangeValidator:
def validate_args(self, tool_name, args, context=None):
if tool_name == "set_temperature" and not 0 <= args.get("value", 0) <= 100:
return ValidationResult(
valid=False,
errors=["temperature must be between 0 and 100"],
remediation="Use a value in the 0-100 range.",
)
return ValidationResult(valid=True)
def validate_result(self, tool_name, result, context=None):
return ValidationResult(valid=True)
def set_temperature(value: float) -> str:
return f"set to {value}"
agent = Agent(name="thermostat", tools=[set_temperature], model={"model": "gpt-4o-mini"})
agent._tool_validator = RangeValidator()
print(agent.chat("Set the temperature to 500")) # validator rejects, tool never runsValidationResult fields:
| Field | Type | Description |
|---|---|---|
valid | bool | Whether validation passed |
errors | List[str] | List of error messages |
warnings | List[str] | List of warning messages |
remediation | Optional[str] | Suggested fix for validation errors |
PII redaction
Scrub sensitive data from LLM requests.
graph LR
Messages["π messages"] --> Hook["πͺ BEFORE_LLM hook"]
Hook --> Scrubber["π§Ή scrub_pii_text"]
Scrubber --> Redacted["π redacted messages"]
Redacted --> LLM["π§ LLM"]
classDef input fill:#6366F1,stroke:#7C90A0,color:#fff
classDef process fill:#189AB4,stroke:#7C90A0,color:#fff
classDef secure fill:#10B981,stroke:#7C90A0,color:#fff
classDef llm fill:#8B0000,stroke:#7C90A0,color:#fff
class Messages input
class Hook,Scrubber process
class Redacted secure
class LLM llm- Enable once at startup
python from praisonaiagents.trace import enable_pii_redaction enable_pii_redaction() # idempotent β safe to call many times - Use agents normally
Every
BEFORE_LLMevent now passes messages through the scrubber. - Disable in tests if needed
python from praisonaiagents.trace import disable_pii_redaction disable_pii_redaction()
What gets scrubbed:
- Key=value pairs:
api_key=sk-β¦,password: hunter2,token=β¦β[REDACTED] - Naked tokens: OpenAI-style
sk-ABCDEFβ¦keys β[REDACTED] - Identifiers: US SSNs
123-45-6789β[REDACTED-SSN], credit cards β[REDACTED-CC], emails β[REDACTED-EMAIL]
Which one do I use?
graph TB
Start["π€ What do you need?"] --> Repro{"π― Reproducible outputs\nfor evals/snapshots?"}
Start --> Stop{"π User pressed stop/\ntime limit hit?"}
Start --> BadArgs{"π« LLM might call tools\nwith bad arguments?"}
Start --> Secrets{"π Prompts may contain\nuser secrets/PII?"}
Repro -->|yes| SeedSol["β
Use seed parameter"]
Stop -->|yes| CancelSol["β
Use cancel_token +\nInterruptController"]
BadArgs -->|yes| ValidatorSol["β
Use _tool_validator"]
Secrets -->|yes| PIISol["β
Use enable_pii_redaction()"]
classDef question fill:#6366F1,stroke:#7C90A0,color:#fff
classDef solution fill:#10B981,stroke:#7C90A0,color:#fff
class Start,Repro,Stop,BadArgs,Secrets question
class SeedSol,CancelSol,ValidatorSol,PIISol solutionCommon Patterns
Reproducible eval
from praisonaiagents import Agent
agent = Agent(name="eval", instructions="Pick a random number 1-100", model={"model": "gpt-4o-mini"})
# Same seed should give same output
response1 = agent.chat("Pick a number", seed=42)
response2 = agent.chat("Pick a number", seed=42)
assert response1 == response2 # Should pass with most providersCancel from Ctrl+C handler
import signal
from praisonaiagents import Agent
from praisonaiagents.agent.interrupt import InterruptController
agent = Agent(name="worker", instructions="You are helpful", model={"model": "gpt-4o-mini"})
tok = InterruptController()
def signal_handler(signum, frame):
tok.request("sigint")
signal.signal(signal.SIGINT, signal_handler)
try:
result = agent.chat("Write a very long essay", cancel_token=tok)
print(result)
except InterruptedError as e:
print(f"Cancelled: {e}")Disable redaction in tests
import pytest
from praisonaiagents.trace import enable_pii_redaction, disable_pii_redaction
@pytest.fixture(autouse=True)
def test_redaction():
# Enable for production-like behavior
enable_pii_redaction()
yield
# Clean up after test
disable_pii_redaction()Best Practices
Seed reliability
Some providers ignore seed or treat it as best-effort. Don’t rely on it for production routing decisions.
Thread-safe cancellation
InterruptController.request(...) is thread-safe. Trip it from a signal handler, an HTTP cancel endpoint, or a parent supervisor.
Fast validators
Validators run on every tool invocation. Avoid network calls or heavy work β return fast ValidationResult objects.
Local audit trail
enable_pii_redaction() only scrubs messages going to the LLM. Your local chat history still has the raw prompt β keep it secure or scrub it separately.
Related
Human-in-the-loop approvals for sensitive operations
GuardrailsInput and output validation with custom rules
Security Environment VariablesSecure handling of API keys and secrets
HooksEvent system for BEFORE_LLM and BEFORE_TOOL hooks
BEFORE_LLM on asyncBEFORE_LLM / AFTER_LLM fire on both chat() and achat()