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_cust_your-tenant.<key-material> VERIPROOF_APPLICATION_ID=my-ai-app ANTHROPIC_API_KEY=sk-ant-...

Complete example

import { VeriproofClient, VeriproofSdkOptions, DecisionContext, DecisionType, RiskLevel, SessionIntent, SessionOutcome, } from '@veriproof/sdk-core'; import { VeriproofAISDKInstrumentation } from '@veriproof/sdk-instrumentation-ai-sdk'; import { anthropic } from '@ai-sdk/anthropic'; import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node'; // 1. Configure VeriProof once at startup const provider = new NodeTracerProvider(); const client = new VeriproofClient( VeriproofSdkOptions.createDevelopment({ apiKey: process.env.VERIPROOF_API_KEY!, applicationId: process.env.VERIPROOF_APPLICATION_ID!, }), ); const { exporter } = client.configureTracing({ serviceName: process.env.VERIPROOF_APPLICATION_ID!, tracerProvider: provider, setGlobal: true, }); provider.register(); // Create the instrumentation adapter — wraps AI SDK calls with OTel spans const aiSdk = new VeriproofAISDKInstrumentation(); // Graceful shutdown — flush any buffered spans before the process exits process.on('SIGTERM', async () => { await exporter.shutdown(); process.exit(0); }); // 2. Build a governance session const session = client.startSession( 'loan-risk-assessment', process.env.VERIPROOF_APPLICATION_ID, { sessionIntent: SessionIntent.Decision, decisionType: DecisionType.Recommendation }, ); // 3. Call the AI framework via the adapter — spans captured automatically const { text } = await aiSdk.generateText({ model: anthropic('claude-3-5-haiku-20241022'), prompt: 'Assess the creditworthiness of applicant A-1042. Be concise.', maxTokens: 256, }); // 4. Record the decision and complete the session session .setDecision(DecisionContext.simple('creditworthiness', text)) .setRiskLevel(RiskLevel.Medium) .setOutcome(SessionOutcome.Success); // 5. 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, veriproof.decision.type=recommendation
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