Vercel AI SDK
Gate every Vercel AI SDK tool call through LatchGate’s enforcement pipeline.
Prerequisites
Section titled “Prerequisites”- LatchGate running (
latchgate upor production deployment)
Install
Section titled “Install”npm install @latchgate/ai-sdk latchgate aiQuick start
Section titled “Quick start”import { generateText } from "ai";import { openai } from "@ai-sdk/openai";import { latchgateToolset } from "@latchgate/ai-sdk";
const { tools, close } = await latchgateToolset({ gateUrl: "http://localhost:3000",});
try { const { text } = await generateText({ model: openai("gpt-4o"), tools, maxSteps: 5, prompt: "Fetch https://httpbin.org/get and summarize the response", });
console.log(text);} finally { await close();}How it works
Section titled “How it works”latchgateToolset() discovers all registered LatchGate actions via the REST API and returns a { tools, actionIds, close } object. tools is a record of AI SDK tool objects with JSON Schema definitions from each action’s manifest (via the AI SDK’s jsonSchema() helper). When the LLM calls a tool, the wrapper routes it through LatchGateClient.execute() — the full enforcement pipeline runs transparently.
Call close() when the toolset is no longer needed to release the underlying transport. If you provided a pre-configured client via options, close() is a no-op — the client lifecycle remains yours.
Filtering actions
Section titled “Filtering actions”const { tools, close } = await latchgateToolset({ gateUrl: "http://localhost:3000", include: new Set(["http_fetch", "github_read"]), exclude: new Set(["send_message"]),});UDS transport (production)
Section titled “UDS transport (production)”Create a LatchGateClient with UDS transport and pass it to the toolset:
import { LatchGateClient } from "latchgate";import { latchgateToolset } from "@latchgate/ai-sdk";
const client = new LatchGateClient({ socket: "/run/latchgate/gate.sock", agentId: "my-ai-sdk-agent",});
const { tools, close } = await latchgateToolset({ client });Alternatively, set the LATCHGATE_URL environment variable and omit gateUrl:
// With LATCHGATE_URL=http://localhost:3000 set:const { tools, close } = await latchgateToolset();Audit callback
Section titled “Audit callback”Use onAudit for programmatic access to receipt metadata after each successful execution:
const { tools, close } = await latchgateToolset({ gateUrl: "http://localhost:3000", onAudit: (record) => { // record.actionId, record.receiptId, record.traceId, record.verification auditLog.store(record); },});Discovery timeout
Section titled “Discovery timeout”Control the HTTP timeout for the discovery phase (default 15000ms):
const { tools, close } = await latchgateToolset({ gateUrl: "http://localhost:3000", timeout: 10000,});More details
Section titled “More details”Full API reference: latchgate-integrations/ai-sdk/README.md.