Skip to Content
ExamplesTypeScript / Node.jsVercel AI SDK

Vercel AI SDK

Automatically capture governance traces for every generateText, streamText, generateObject, and streamObject call using the VeriProof Vercel AI SDK instrumentation adapter.

TypeScriptVercel AI SDKNode.js ≥ 18

Prerequisites

npm install @veriproof/sdk-core @veriproof/sdk-instrumentation-ai-sdk ai

You’ll also need an AI provider. This example uses the Anthropic provider:

npm install @ai-sdk/anthropic

Environment

# .env VERIPROOF_API_KEY=vp_live_... VERIPROOF_APPLICATION_ID=my-ai-app ANTHROPIC_API_KEY=sk-ant-...

Complete example

import { configureVeriproof, VeriproofSdkOptions, VeriproofSession, DecisionType, SessionIntent, RiskLevel, StepOutcome } from '@veriproof/sdk-core'; import { VeriproofAISDKInstrumentation } from '@veriproof/sdk-instrumentation-ai-sdk'; import { generateText } from 'ai'; import { anthropic } from '@ai-sdk/anthropic'; // 1. Configure VeriProof once at startup const { provider } = configureVeriproof( VeriproofSdkOptions.createDevelopment({ apiKey: process.env.VERIPROOF_API_KEY!, applicationId: process.env.VERIPROOF_APPLICATION_ID!, }), { serviceName: process.env.VERIPROOF_APPLICATION_ID!, setGlobal: true, instrumentations: [new VeriproofAISDKInstrumentation()], }, ); // Graceful shutdown — flush any buffered spans before the process exits process.on('SIGTERM', async () => { await provider.shutdown(); process.exit(0); }); // 2. Build a governance session const session = new VeriproofSession({ applicationId: process.env.VERIPROOF_APPLICATION_ID!, sessionName: 'loan-risk-assessment', intent: SessionIntent.DECISION_SUPPORT, }); // 3. Add a step for the AI call await session.addStep({ stepName: 'assess-creditworthiness', stepType: 'ANALYSIS', input: { applicantId: 'A-1042', requestedAmount: 25000 }, }); // 4. Call the AI framework — spans captured automatically const { text } = await generateText({ model: anthropic('claude-3-5-haiku-20241022'), prompt: 'Assess the creditworthiness of applicant A-1042. Be concise.', maxTokens: 256, }); // 5. Record the decision and close the step await session.setDecision({ decisionType: DecisionType.RISK_ASSESSMENT, decisionSummary: text, riskLevel: RiskLevel.MEDIUM, outcome: StepOutcome.COMPLETED, }); // 6. Complete the session — this flushes and Merkle-seals the trace const result = await session.complete(); console.log('Session anchor:', result.merkleRoot);

What you’ll see in VeriProof

After running this example you’ll see a session in the dashboard with:

SpanKey attributes
session loan-risk-assessmentveriproof.session.intent=DECISION_SUPPORT, veriproof.session.status=COMPLETED
step assess-creditworthinessveriproof.step.type=ANALYSIS, veriproof.decision.risk_level=MEDIUM
ai.generateTextai.model.id=claude-3-5-haiku-20241022, ai.usage.inputTokens, ai.usage.outputTokens

The ai.generateText span is captured automatically by VeriproofAISDKInstrumentation — no manual span creation required.

The instrumentation captures model metadata (provider, model ID, token counts) but does not capture prompt or completion content by default. Set enableContentCapture: true in VeriproofSdkOptions to opt in — only do this if your data handling policies permit it.


Streaming

streamText is captured identically — the instrumentation hooks into the stream’s completion event:

import { streamText } from 'ai'; const { textStream } = await streamText({ model: anthropic('claude-3-5-haiku-20241022'), prompt: 'Summarise the key risks for this loan application.', maxTokens: 512, }); for await (const chunk of textStream) { process.stdout.write(chunk); } // Span is recorded when the stream is fully consumed

Next steps

Last updated on