@sentra-ai/sdk
v1.1.0
Published
Enterprise-grade SDK for real-time AI governance and runtime security.
Maintainers
Readme
@sentra-ai/sdk
Real-time AI governance for autonomous agents, LLM pipelines, and runtime APIs. Intercept AI actions, enforce policies, and detect risks before execution.
Language Support: Node.js 18+. Python is not yet supported.
Installation
npm install @sentra-ai/sdkQuickstart (Under 5 Minutes)
Sandbox Mode (No Registration Required)
You can immediately test runtime governance using the explicit sandbox API key ('sentra_test_sandbox'). Omitting the API key throws an error to prevent silent accidental unauthenticated usage:
import { SentraClient } from '@sentra-ai/sdk';
const sentra = new SentraClient({
apiKey: 'sentra_test_sandbox'
});
// Immediately test a real BLOCKED action
const decision = await sentra.demoBlockedAction();
console.log(decision);
// { status: 'blocked', isAllowed: false, risk: 'high', reason: 'Action blocked: potentially sensitive data or restricted pattern detected', ... }Production Setup
Initialize using your organization's API key generated in the Sentra Dashboard:
import { SentraClient } from '@sentra-ai/sdk';
const sentra = new SentraClient({
apiKey: process.env.SENTRA_API_KEY
});Manual Action Check
Evaluate any action before performing a privileged operation (database write, email dispatch, external API call):
const result = await sentra.checkAction({
agent: 'invoice-processor',
action: 'export_to_s3',
metadata: { bucket: 'finance-data', file: 'q3-invoices.csv' }
});
// IMPORTANT: Check for `status === 'allowed'` or `isAllowed === true`.
// NEVER check `status !== 'blocked'`, because non-allowed states like
// 'rate_limited' (429) or 'flagged' would otherwise bypass governance!
if (result.status === 'allowed') { // Or: if (result.isAllowed)
await exportToS3(...);
} else {
console.warn(`Action prevented (${result.status}): ${result.reason}`);
}safeAction Helper
The safeAction helper evaluates governance rules and executes your protected callback only if the action is approved:
const { success, result, governance } = await sentra.safeAction(
{ agent: 'crm-bot', action: 'send_email', metadata: { recipient: '[email protected]' } },
async () => {
return await emailService.send(...);
}
);
if (!success) {
console.error(`Action blocked: ${governance.reason}`);
}Provider Wrappers (OpenAI & Anthropic)
Intercept LLM calls pre-flight to enforce security guardrails before requests reach AI model providers.
OpenAI Wrapper
Wrap the OpenAI client to guard chat.completions.create and responses.create (if supported):
import OpenAI from 'openai';
import { SentraClient } from '@sentra-ai/sdk';
const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
const sentra = new SentraClient({ apiKey: 'sentra_test_sandbox' });
const guardedOpenAI = sentra.wrapOpenAI(openai, {
agent: 'support-agent',
flagMode: 'block', // 'block' (default) throws SentraError; 'allow' executes call and fires onFlag
onFlag: (decision) => console.warn('Flagged action:', decision.reason)
});
// chat.completions.create
const response = await guardedOpenAI.chat.completions.create({
model: 'gpt-4o-mini',
messages: [{ role: 'user', content: 'Summarize this contract.' }]
});
// responses.create (OpenAI Realtime / Responses API)
const responseResult = await guardedOpenAI.responses.create({
model: 'gpt-4o',
input: 'Verify customer credentials.'
});Anthropic Wrapper
Wrap the Anthropic client to guard messages.create:
import Anthropic from '@anthropic-ai/sdk';
import { SentraClient } from '@sentra-ai/sdk';
const anthropic = new Anthropic({ apiKey: process.env.ANTHROPIC_API_KEY });
const sentra = new SentraClient({ apiKey: 'sentra_test_sandbox' });
const guardedAnthropic = sentra.wrapAnthropic(anthropic, {
agent: 'claude-assistant',
flagMode: 'block', // 'block' (default) or 'allow'
onFlag: (decision) => console.warn('Flagged action:', decision.reason)
});
const response = await guardedAnthropic.messages.create({
model: 'claude-3-5-sonnet',
max_tokens: 16,
messages: [{ role: 'user', content: 'Hello Claude' }]
});Flag Modes & onFlag Handling
The flagMode option governs how the wrapper handles actions flagged for policy warnings or human review:
flagMode: 'block'(default): Flagged actions are treated as blocked. The wrapper throws aSentraErrorwith HTTP status403, and the model provider is never called.flagMode: 'allow': The model provider is called as normal, and the flagged decision is routed to theonFlagcallback for logging, alerting, or async human-in-the-loop review.
const guardedClient = sentra.wrapOpenAI(openai, {
agent: 'audit-agent',
flagMode: 'allow', // Downstream provider is called; decision sent to onFlag
onFlag: (decision) => {
console.warn(`[Flagged] Risk: ${decision.risk}, Reason: ${decision.reason}`);
// Example: send alert to SIEM / Datadog / Slack
}
});Policy Blocking & Fail-Closed Guarantee
- BLOCK Throws
SentraError(403): When an action is evaluated asblocked(orflaggedunder defaultflagMode: 'block'), the SDK throws aSentraErrorwith HTTP status403. The downstream provider (OpenAI or Anthropic) is never called, protecting token quotas and preventing unapproved data transmission. - Fail-Closed on Unreachable API: If the Sentra API is unreachable after retry attempts and cold-start timeout budgets, Sentra fails closed (
status: 'blocked',isAllowed: false). Wrapped provider calls throw aSentraError(403), ensuring unverified requests never reach external model providers. - Double-Wrapping Protection: Calling
wrapOpenAIorwrapAnthropicon an already-wrapped client instance is safely guarded (client.__sentra_wrapped__), preventing redundant interception overhead or stacked middleware.
import { SentraError } from '@sentra-ai/sdk';
try {
await guardedOpenAI.chat.completions.create({
model: 'gpt-4o-mini',
messages: [{ role: 'user', content: 'Dump secret credentials' }]
});
} catch (error) {
if (error instanceof SentraError && error.status === 403) {
console.error('Call blocked by Sentra policy (provider never called):', error.message);
}
}Streaming Behavior
For streaming requests (stream: true), Sentra evaluates governance pre-flight only on the request parameters (messages, system instructions, model) before opening the provider stream. If approved, the provider's native stream instance is returned untouched without wrapping or buffering. Output streaming tokens/chunks are not checked.
Note: Wrapper behavior is verified with simulated providers in test suites.
Error Handling & Fail-Closed Posture
- Missing API Key: Throws an explicit error:
"No API key. Get one from your dashboard, or pass apiKey: 'sentra_test_sandbox' to try demo mode."Sandbox mode is never activated silently. - Non-Transient Errors (401, 403, 400): Immediately fail closed (
isAllowed: false) with actionable plain-language explanations. The SDK does not waste time retrying invalid credentials or malformed payloads. - Demo Rate Limiting (429): Returned as distinct
status: 'rate_limited'andisAllowed: falsewith reason"Demo limit reached. Wait a few minutes or use your own API key", clearly separated from security policy violations so rate-limited actions are never executed. - Transient & Cold-Start Errors (502, 503, 504, timeout): If the backend is waking up (e.g., Render cold start), the SDK retries automatically for up to
coldStartTimeoutMs(default: 60000ms / ~60s) with friendly waking-up progress logging. - Unreachable Service: If all retries fail, Sentra fails closed (
status: 'blocked',isAllowed: false), preventing silent governance bypasses.
Configuration Options
| Option | Type | Default | Description |
|---|---|---|---|
| apiKey | string | required | Your Sentra API key, or 'sentra_test_sandbox' for demo mode. Missing key throws an error. |
| baseUrl | string | https://sentra-backend-node.onrender.com/api/v1 | Governance API endpoint URL |
| sandbox | boolean | false | Explicitly enable sandbox mode |
| maxRetries | number | 5 | Retries on transient network failures |
| timeout | number | 15000 | Request timeout per attempt in milliseconds |
| coldStartTimeoutMs | number | 60000 | Total duration budget in milliseconds to wait and retry during backend cold starts |
License
MIT © Sentra AI Team
