Skip to Content

LangChain.js

Automatically capture spans for every LangChain.js chain, prompt template, tool call, and retrieval step using the VeriProof LangChain.js instrumentation adapter.

TypeScriptLangChain.jsNode.js ≥ 18

Prerequisites

npm install @veriproof/sdk-core @veriproof/sdk-instrumentation-langchainjs @langchain/core @langchain/anthropic

Environment

VERIPROOF_API_KEY=vp_cust_your-tenant.<key-material> VERIPROOF_APPLICATION_ID=my-langchain-app ANTHROPIC_API_KEY=sk-ant-...

Complete example

import { VeriproofClient, VeriproofSdkOptions, DecisionContext, RiskLevel, SessionIntent, SessionOutcome, } from '@veriproof/sdk-core'; import { createVeriproofLangChainCallbackHandler } from '@veriproof/sdk-instrumentation-langchainjs'; import { ChatAnthropic } from '@langchain/anthropic'; import { ChatPromptTemplate } from '@langchain/core/prompts'; import { StringOutputParser } from '@langchain/core/output_parsers'; 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(); process.on('SIGTERM', async () => { await exporter.shutdown(); }); // 2. Create the LangChain callback handler — attaches VeriProof spans to chain runs const veriproofCallback = createVeriproofLangChainCallbackHandler({ captureInputs: true, captureOutputs: true, }); // 3. Build a governance session const session = client.startSession( 'contract-summarisation', process.env.VERIPROOF_APPLICATION_ID, { sessionIntent: SessionIntent.Review }, ); // 4. Build and invoke a LangChain chain — all spans captured automatically const model = new ChatAnthropic({ model: 'claude-3-5-haiku-20241022', maxTokens: 512 }); const prompt = ChatPromptTemplate.fromMessages([ ['system', 'You are a legal analyst. Summarise the key obligations of each party.'], ['human', '{contract_text}'], ]); const chain = prompt.pipe(model).pipe(new StringOutputParser()); const summary = await chain.invoke( { contract_text: '...contract text here...' }, { callbacks: [veriproofCallback] }, ); // 5. Record the decision and complete the session session .setDecision(DecisionContext.simple('contract-summary', summary)) .setRiskLevel(RiskLevel.Low) .setOutcome(SessionOutcome.Success); const result = await session.complete(); console.log('Session ID:', result.sessionId);

What you’ll see in VeriProof

The LangChain.js callback handler creates child spans for each component in the chain:

SpanCaptured from
session contract-summarisationVeriproofClient.startSession()
chainLangChain callback handler — chain start/end
ChatPromptTemplateLangChain callback handler — prompt formatting
ChatAnthropicLangChain callback handler — model invocation
StringOutputParserLangChain callback handler — output parsing

All LangChain spans are automatically linked as children of the VeriProof session span, so the full chain execution is visible as a single trace in the dashboard.


Next steps

Last updated on