Skip to content

Vercel AI SDK

Gate every Vercel AI SDK tool call through LatchGate’s enforcement pipeline.

Terminal window
npm install @latchgate/ai-sdk latchgate ai
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();
}

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.

const { tools, close } = await latchgateToolset({
gateUrl: "http://localhost:3000",
include: new Set(["http_fetch", "github_read"]),
exclude: new Set(["send_message"]),
});

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();

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);
},
});

Control the HTTP timeout for the discovery phase (default 15000ms):

const { tools, close } = await latchgateToolset({
gateUrl: "http://localhost:3000",
timeout: 10000,
});

Full API reference: latchgate-integrations/ai-sdk/README.md.