Agent Guardrails Docs

SDK Reference

TypeScript client for interacting with the Agent Guardrails on-chain program. The SDK wraps every instruction, handles PDA derivation, and provides typed helpers for policy management, guarded execution, and kill switch operations.

Installation

terminal
npm install @agentguards/sdk

Initialization

client.ts
import { GuardrailsClient } from "@agentguards/sdk";
import { AnchorProvider } from "@coral-xyz/anchor";

const provider = AnchorProvider.env();
const client = new GuardrailsClient(provider);

PDA Derivation

The SDK provides two helpers that derive program-derived addresses used by all instructions. These mirror the seeds defined in the on-chain program.

pda.ts
import { findPolicyPda, findTrackerPda } from "@agentguards/sdk";

// Seeds: ["policy", owner, agent]
const [policyPda, policyBump] = findPolicyPda(ownerPubkey, agentPubkey);

// Seeds: ["tracker", policy]
const [trackerPda, trackerBump] = findTrackerPda(policyPda);

Policy Management

initializePolicy

Creates a new PermissionPolicy account and its associated SpendTracker. The caller (wallet) becomes the policy owner.

ParamTypeDescription
agentPublicKeyPublic key of the AI agent keypair
argsInitializePolicyArgsPolicy configuration object (see below)

InitializePolicyArgs fields:

FieldTypeDescription
allowedProgramsPubkey[]Whitelisted program IDs (max 10)
maxTxLamportsu64Per-transaction SOL cap in lamports
maxTxTokenUnitsu64Per-transaction token unit cap
dailyBudgetLamportsu64Rolling 24h budget in lamports
sessionExpiryi64Unix timestamp when the session expires
squadsMultisigOption<Pubkey>Squads v4 multisig for escalation (optional)
escalationThresholdu64Amount that triggers multisig escalation
authorizedMonitorsPubkey[]Keypairs allowed to pause the agent (max 3)

Returns the transaction signature.

init-policy.ts
const txSig = await client.initializePolicy(agentKeypair.publicKey, {
  allowedPrograms: [JUPITER_PROGRAM_ID, MARINADE_PROGRAM_ID],
  maxTxLamports: new BN(500_000_000),     // 0.5 SOL
  maxTxTokenUnits: new BN(1_000_000),
  dailyBudgetLamports: new BN(5_000_000_000), // 5 SOL
  sessionExpiry: new BN(Math.floor(Date.now() / 1000) + 86400),
  squadsMultisig: null,
  escalationThreshold: new BN(2_000_000_000), // 2 SOL
  authorizedMonitors: [monitorKeypair.publicKey],
});

updatePolicy

Modifies an existing policy. All fields are optional — only provided fields are updated.

ParamTypeDescription
policyPdaPublicKeyAddress of the PermissionPolicy account
argsUpdatePolicyArgsPartial policy update (all fields optional)
update-policy.ts
await client.updatePolicy(policyPda, {
  maxTxLamports: new BN(1_000_000_000),   // raise to 1 SOL
  allowedPrograms: [JUPITER_PROGRAM_ID, MARINADE_PROGRAM_ID, DRIFT_PROGRAM_ID],
});

closePolicy

Permanently deletes a policy and its tracker. The policy must be paused first. Only the owner can close. Remaining lamports are refunded to the owner.

close-policy.ts
const txSig = await client.closePolicy(policyPda);

Execution

guardedExecute

The core instruction. Routes an agent transaction through the 12-step validation pipeline before executing the CPI. This is the only way an agent should interact with external programs.

ParamTypeDescription
agentKeypairAgent keypair (must match policy)
policyPdaPublicKeyThe PermissionPolicy PDA
trackerPdaPublicKeyThe SpendTracker PDA
targetProgramPublicKeyProgram to invoke via CPI
argsGuardedExecuteArgsInstruction data, amount hint, and optional input account index
remainingAccountsAccountMeta[]Accounts required by the target program
guarded-execute.ts
// Example: Jupiter swap through guarded execution
const txSig = await client.guardedExecute(
  agentKeypair,
  policyPda,
  trackerPda,
  JUPITER_PROGRAM_ID,
  {
    instructionData: swapIxData,
    amountHint: new BN(100_000_000), // 0.1 SOL
    inputAccountIndex: 2,
  },
  jupiterAccounts, // remaining accounts for the swap CPI
);

multisigExecute

Executes a transaction that has been approved by the Squads multisig. Used when an agent transaction exceeds the escalation threshold and requires human approval.

ParamTypeDescription
squadsProposalPublicKeySquads v4 proposal account (must be in Approved state)
All other parameters are identical to guardedExecute. The multisig proposal serves as the authorization that replaces the standard budget check.
multisig-execute.ts
const txSig = await client.multisigExecute(
  agentKeypair,
  policyPda,
  trackerPda,
  JUPITER_PROGRAM_ID,
  {
    instructionData: swapIxData,
    amountHint: new BN(3_000_000_000), // 3 SOL (above threshold)
    inputAccountIndex: 2,
  },
  jupiterAccounts,
  squadsProposalPda,
);

Kill Switch

The kill switch immediately freezes all agent activity by setting isActive = false on the policy account. Any subsequent call to guarded_execute will fail with PolicyPaused.

kill-switch.ts
// Pause — callable by owner or any authorized monitor
const pauseTx = await client.pauseAgent(policyPda, "Anomalous transfer pattern detected");

// Resume — owner only
const resumeTx = await client.resumeAgent(policyPda);
pauseAgent can be called by the policy owner or any of the authorizedMonitors. resumeAgent is restricted to the owner only.

Key Rotation

Atomically swaps the agent keypair associated with a policy. Creates new PDAs under the new agent key, copies all configuration and budget state, transfers remaining funds, and closes the old accounts.

rotate-key.ts
const { txSig, newPolicyPda } = await client.rotateAgentKey(
  oldPolicyPda,
  newAgentKeypair.publicKey,
);

// Old policy + tracker PDAs are closed
// New policy + tracker PDAs are created with identical config

Types

TypeKindDescription
InitializePolicyArgsInstruction argsConfiguration for creating a new policy (see initializePolicy above)
UpdatePolicyArgsInstruction argsPartial update fields (all optional)
GuardedExecuteArgsInstruction argsinstructionData (Buffer), amountHint (BN), inputAccountIndex (number, optional)
PermissionPolicyAccount structOn-chain policy state — owner, agent, limits, monitors, pause state
SpendTrackerAccount structRolling spend counters — 24h and 1h windows, transaction counts, destination tracking