Skip to main content

Safentic SDK

Safentic is a runtime guardrail SDK for agentic AI systems.

It intercepts and evaluates tool calls between agent intent and execution, enforcing custom safety policies and generating structured audit logs for compliance.

Key Features

  • Runtime Protection: Intercepts tool calls at the action boundary
  • Policy-Driven: Define safety rules in simple YAML configuration
  • Audit Logging: Structured JSON logs for compliance and debugging
  • Framework Agnostic: Works with LangChain, AutoGen, MCP, and custom agents
  • Easy Integration: Minimal code changes to existing agents

Installation

pip install safentic

Quick Start (5 minutes)

1. Set Up Your Environment

Before using Safentic, configure the required API key:

export OPENAI_API_KEY="your-openai-api-key"

2. Create a Policy File

Create config/policy.yaml to define your safety rules:

tools:
  sample_tool:
    rules:
      - type: llm_verifier
        description: "Block outputs that contain disallowed terms"
        instruction: "Does this text contain disallowed terms or references?"
        model: gpt-4
        fields: [body]
        response_format: boolean
        response_trigger: yes
        match_mode: exact
        level: block
        severity: high
        tags: [denylist]

  another_tool:
    rules: []  

logging:
  level: INFO
  destination: "safentic/logs/safentic_audit.log"
  jsonl: "safentic/logs/safentic_audit.jsonl"

3. Wrap Your Agent with SafetyLayer

Import and initialize Safentic in your application:

from safentic.layer import SafetyLayer
from your_agent_module import YourAgentClass

# Initialize your existing agent
agent = YourAgentClass()

# Wrap it with Safentic
safety_layer = SafetyLayer(
    agent=agent,
    api_key="your-api-key",  
    agent_id="demo-agent"
)

4. Call Tools Through the Safety Layer

Use the wrapped agent to execute tool calls safely:

try:
    result = safety_layer.call_tool("some_tool", {"body": "example input"})
    print("Allowed:", result)
except Exception as e:
    print("Blocked:", str(e))

Example Output:

Blocked: Blocked by policy

Complete Example

Here's a complete integration example:

from safentic.layer import SafetyLayer

# Step 1: Create or import your agent
class MyAgent:
    def execute_tool(self, tool_name, params):
        # Your tool logic here
        return f"Executed {tool_name}"

agent = MyAgent()

# Step 2: Initialize Safentic
safety_layer = SafetyLayer(
    agent=agent,
    api_key="your-api-key",
    agent_id="my-agent"
)

# Step 3: Execute tools through Safentic
try:
    result = safety_layer.call_tool("delete_file", {"path": "/sensitive/data"})
    print(f"Success: {result}")
except Exception as e:
    print(f"Action blocked: {e}")
    # Log to your monitoring system

Configuring Your Policy File

  • Safentic enforces rules defined in a YAML configuration file (e.g. policy.yaml).
  • By default, it looks for config/policy.yaml, or you can set the path with:
export SAFENTIC_POLICY_PATH=/path/to/policy.yaml

Policy Schema

Safentic supports the llm_verifier rule type and now also allows deterministic filtering via prechecks.

tools:
  <tool_name>:
    rules:
      - type: llm_verifier
        description: "<short description of what this rule enforces>"
        instruction: "<prompt instruction given to the verifier LLM>"
        model: "<llm model name, e.g. gpt-4>"
        fields: [<list of input fields to check>]
        reference_file: "<path to reference text file, optional>"
        response_format: boolean
        response_trigger: yes
        match_mode: exact
        level: block         # enforcement level: block | warn
        severity: high       # severity: low | medium | high
        tags: [<labels for filtering/searching logs>]
        prechecks:                # Optional deterministic filters before LLM
          - check: contains       # or 'exact', 'regex'
            values: ["refund", "apology"]
            action: block         # block or warn

logging:
  level: INFO
  destination: "safentic/logs/safentic_audit.log"
  jsonl: "safentic/logs/safentic_audit.jsonl"

Example Policy

tools:
  sample_tool:
    rules:
      - type: llm_verifier
        description: "Block outputs that contain disallowed terms"
        instruction: "Does this text contain disallowed terms or references?"
        model: gpt-4
        fields: [body]
        reference_file: sample_guidelines.txt
        response_format: boolean
        response_trigger: yes
        match_mode: exact
        level: block
        severity: high
        tags: [sample, denylist]
        prechecks:
          - check: contains
            values: ["refund", "apology", "guarantee", "sorry"]
            action: block
          - check: regex
            values: ["refund policy", "customer (apology|complaint)"]
            action: block

  another_tool:
    rules: []  # Explicitly allow all actions for this tool

logging:
  level: INFO
  destination: "safentic/logs/safentic_audit.log"
  jsonl: "safentic/logs/safentic_audit.jsonl"

Audit Logs

Every decision is logged with context for compliance and debugging:

{
  "timestamp": "2025-09-09T14:25:11Z",
  "agent_id": "demo-agent",
  "tool": "sample_tool",
  "allowed": false,
  "reason": "Blocked by policy",
  "rule": "sample_tool:denylist_check",
  "severity": "high",
  "level": "block",
  "tags": ["sample", "denylist"]
}

Log Fields

Field Description
timestamp When the action was evaluated
agent_id The agent issuing the action
tool Tool name
allowed Whether the action was permitted (true/false)
reason Why it was allowed or blocked
rule The rule that applied (if any)
severity Severity of the violation (low, medium, high)
level Enforcement level (block, warn)
tags Categories attached to the rule
extra Additional metadata (e.g., missing fields, matched text)

CLI Commands

Safentic ships with a CLI for validating policies, running one-off checks, and inspecting logs:

Validate a policy file

safentic validate-policy --policy config/policy.yaml --strict

Run a one-off tool check

safentic check-tool --tool sample_tool \
  --input-json '{"body": "some text"}' \
  --policy config/policy.yaml

Tail the audit log (JSONL by default)

safentic logs tail --path safentic/logs/safentic_audit.jsonl -f

Environment Variables

Set these before running Safentic:

Variable Required Description
OPENAI_API_KEY Yes API key for OpenAI models used in llm_verifier rules
SAFENTIC_POLICY_PATH No Path to your policy.yaml (default: config/policy.yaml)
SAFENTIC_LOG_PATH No Override the default text audit log path
SAFENTIC_JSON_LOG_PATH No Override the default JSONL audit log path
LOG_LEVEL No Sets logging verbosity (DEBUG, INFO, WARNING, ERROR)

Supported Frameworks

Safentic is agent-framework agnostic: it can wrap any agent (LangChain, AutoGen, MCP, custom, etc.) as long as the agent exposes a call_tool method. This means you can enforce policies on tool calls regardless of the agent framework.

Current LLM support:

  • The policy engine's semantic checks (LLMVerifier) currently support only OpenAI models (via OPENAI_API_KEY), but is agent-agnostic, as long as your agent integrates a call_tool method.

Deterministic Filtering:

  • You can now use prechecks for fast, predictable filtering (contains, exact, regex) before any LLM call. This is useful for blocking or warning on specific phrases or patterns.

Guidelines and Rule Logic:

  • The guidelines file is plain text (e.g., sample_guidelines.txt). The LLMVerifier uses your rule’s instruction, agent output, and the company policy doc to check for compliance. For sentiment or tone checks, you can use instructions like:
    • "Does the agent's response express negative sentiment?"
    • "Is the tone of this response aggressive or inappropriate?"
    • "Does this output violate our company’s respectful communication policy?"

Metadata

Release files for safentic 1.0.11

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for safentic 1.0.11
File Size Uploaded
safentic-1.0.11.tar.gz 27.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for safentic 1.0.11
File Interpreter ABI Platform
safentic-1.0.11-py3-none-any.whl Python 3 none any Details

Total release size: 58.1 kB

Release files / safentic-1.0.11.tar.gz

Download URL safentic-1.0.11.tar.gz
Size 27.9 kB
Tags Source
SHA-256 checksum
How to use checksums
936b7fcd18e34e7fd399adf00f779b893fcd6618bdee61a7022ab39578235a86
BLAKE2b-256 checksum
How to use checksums
5b146f0833321bb1cb9e20b39a0e599d0d7e81770ad9d436c5cbfb45f3f003ee
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.5

Release files / safentic-1.0.11-py3-none-any.whl

Download URL safentic-1.0.11-py3-none-any.whl
Size 30.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
10c184bacf7d8b06976cf5b81dfdef23aaa7f352266dc3ff472119afd4bacd0c
BLAKE2b-256 checksum
How to use checksums
6d76128c8422f09f2d0c94620ceff58927f29a29280b6f8d99610e5a5ae00d3f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.5

Release history Release notifications | RSS feed

This release

1.0.11 This release

2 release files

1.0.10

2 release files

1.0.9

2 release files

1.0.8

2 release files

1.0.7

2 release files

1.0.6

2 release files

1.0.5

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page