<!-- https://avertgate.com/docs/sdk · Integration -->

# SDK

If you write the agent yourself, wrap tool functions directly in code. The same policy engine, inspectors and audit log are used.

## Wrap a tool

<!-- agent.ts -->
```ts
import { createAvertGate, AvertGateSecurityError } from "@avertgate/cli/sdk";

const gate = createAvertGate({
  policyPath: "./policy.yaml",
  agentId: "support-agent-v1",
});

const issueRefund = gate.wrapTool("issue_refund", async ({ orderId, amount }) => {
  return payments.refund(orderId, amount);
});

try {
  await issueRefund({ orderId: "ORD-100123", amount: 750 });
} catch (err) {
  if (err instanceof AvertGateSecurityError) {
    console.log(err.decision, err.reason, err.ruleId);
  }
}
```

## Low level: guard()

<!-- agent.ts -->
```ts
const result = await gate.guard(
  "send_email",
  { to: recipient, body },
  () => mailer.send({ to, body }),
  { sessionId: "sess-42" },
);
```

## Configuration options

| Option | Description |
| --- | --- |
| `policy` / `policyPath` | File path, raw YAML or an object. |
| `agentId`, `orgId` | Identity and tenant. |
| `auditDbPath` | SQLite path; `:memory:` for an ephemeral log. |
| `apiKey`, `baseUrl` | Cloud-managed policy: resolved through `AvertGate.create()`. |
| `requestApproval` | Approval callback. Default: deny (fail-closed). |
| `shadowMode` | Observe-only mode. |
| `enableHotReload` | Hot-reload when the policy file changes. |
| `tracer`, `metricsRegistry` | OpenTelemetry and Prometheus. |

## Framework adapters

`wrapOpenAITool` wraps an OpenAI tool definition and its executor together; `wrapLangChainTool` is for LangChain tools. Both support the `catchErrors` option.

## Cloud-managed policy

<!-- agent.ts -->
```ts
const gate = await AvertGate.create({
  apiKey: process.env.AVERTGATE_API_KEY, // ag_live_...
  agentId: "support-agent-v1",
});
```

This form pulls the policy from the dashboard via `/api/gateway/resolve` and sends events to `/api/events/ingest`.
