npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

langchain-agent-toolkit

v0.1.0

Published

Verification, observability, and quality scoring utilities for LangChain tool-calling agents. Includes confidence scoring, data freshness validation, numerical consistency checks, ticker symbol hallucination detection, error categorization, token tracking

Readme

langchain-agent-toolkit

Verification, observability, and quality scoring utilities for LangChain tool-calling agents.

Built in production for the Ghostfolio AI Finance Agent — extracted as a reusable package for any domain-specific LLM agent.

Installation

npm install langchain-agent-toolkit

What's Included

Verification (Post-Response Quality Checks)

Validate your agent's responses before returning them to users.

| Function | What It Does | |---|---| | calculateConfidenceScore | Scores agent responses 0-100 based on tool usage, verification results, and data retrieval | | validateDataFreshness | Detects stale data in tool outputs (configurable per-asset thresholds) | | validateNumericalCrosscheck | Checks numerical consistency (e.g., allocations sum to ~100%) | | validateTickerSymbols | Detects hallucinated ticker symbols not present in tool output |

Observability (Monitoring & Diagnostics)

Track costs, classify errors, and sanitize traces for production agents.

| Function | What It Does | |---|---| | categorizeError | Classifies errors into actionable buckets: llm_failure, tool_failure, input_validation, verification_failure, unknown | | extractTokenUsage | Extracts token counts from LangChain AIMessage (supports both usage_metadata and response_metadata formats) | | accumulateTokenUsage | Sums token usage across multi-turn agent loops | | sanitizeToolCallsForTrace | Redacts sensitive fields from tool outputs before sending to LangSmith/other trace backends |

Quick Start

Confidence Scoring

import {
  calculateConfidenceScore,
  VerificationResult
} from 'langchain-agent-toolkit';

const verifications: VerificationResult[] = [
  { type: 'ticker_validation', passed: true, details: '', severity: 'info' },
  { type: 'data_freshness', passed: false, details: 'Stale data', severity: 'warning' }
];

const { score, verificationResult } = calculateConfidenceScore({
  toolCallCount: 2,
  verificationResults: verifications,
  hasToolErrors: false,
  dataRetrievedCount: 2
});

console.log(score);                    // 85 (100 - 15 for warning)
console.log(verificationResult.passed); // true (above 70 threshold)

Data Freshness Validation

import { validateDataFreshness, ToolCallRecord } from 'langchain-agent-toolkit';

const toolCalls: ToolCallRecord[] = [
  {
    name: 'market_data',
    input: { symbols: ['AAPL'] },
    output: {
      quotes: { AAPL: { marketPrice: 150 } },
      retrievedAt: new Date(Date.now() - 25 * 3600000).toISOString() // 25h ago
    }
  }
];

const result = validateDataFreshness('AAPL is at $150', toolCalls);
console.log(result.passed);  // false
console.log(result.details); // "Stale data detected: AAPL data is 25.0h old (threshold: 24h)."

Ticker Symbol Hallucination Detection

import { validateTickerSymbols, ToolCallRecord } from 'langchain-agent-toolkit';

const toolCalls: ToolCallRecord[] = [
  {
    name: 'portfolio_analysis',
    input: {},
    output: { holdings: [{ symbol: 'AAPL' }, { symbol: 'MSFT' }] }
  }
];

// LLM response mentions GOOGL but it wasn't in the tool output
const result = validateTickerSymbols(
  'Your portfolio has AAPL, MSFT, and GOOGL performing well.',
  toolCalls
);

console.log(result.passed);  // false
console.log(result.details); // "Symbols mentioned in response but not found in tool data: GOOGL."

Error Categorization

import { categorizeError } from 'langchain-agent-toolkit';

categorizeError(new Error('429 Too Many Requests')); // 'llm_failure'
categorizeError(new Error('Validation failed'));      // 'input_validation'
categorizeError(new Error('Prisma client error'));    // 'tool_failure'
categorizeError(new Error('Something unknown'));      // 'unknown'

Token Tracking

import { extractTokenUsage, accumulateTokenUsage } from 'langchain-agent-toolkit';

// Works with LangChain AIMessage objects
const usage = extractTokenUsage(aiMessage);
console.log(usage); // { inputTokens: 100, outputTokens: 50, totalTokens: 150 }

// Accumulate across multi-turn conversations
const total = accumulateTokenUsage(turn1Usage, turn2Usage);

Trace Sanitization

import { sanitizeToolCallsForTrace, ToolCallRecord } from 'langchain-agent-toolkit';

const toolCalls: ToolCallRecord[] = [
  {
    name: 'portfolio_analysis',
    input: {},
    output: {
      holdings: [{ symbol: 'AAPL', quantity: 100, marketPrice: 150 }],
      summary: { netWorth: 50000 }
    }
  }
];

const sanitized = sanitizeToolCallsForTrace(toolCalls);
// holdings[0].quantity  => '[REDACTED]'
// holdings[0].symbol    => 'AAPL' (preserved)
// summary.netWorth      => '[REDACTED]'

Customization

All functions accept an options object for customization:

// Custom freshness thresholds
validateDataFreshness(response, toolCalls, {
  defaultThresholdMs: 12 * 3600000,  // 12 hours instead of 24
  cryptoThresholdMs: 30 * 60000,     // 30 minutes instead of 1 hour
  isCryptoSymbol: (s) => s.startsWith('X')  // custom crypto detection
});

// Custom confidence scoring weights
calculateConfidenceScore(factors, {
  lowConfidenceThreshold: 60,  // more lenient
  errorPenalty: 30,            // harsher on errors
  warningPenalty: 10           // more lenient on warnings
});

// Custom redaction rules for your domain
sanitizeToolCallsForTrace(toolCalls, {
  redactionRules: {
    my_tool: {
      holdingFields: ['secretField', 'pii'],
      summaryFields: ['totalValue']
    }
  }
});

// Custom error patterns
categorizeError(error, {
  patterns: [
    { patterns: ['anthropic', 'claude'], category: 'llm_failure' },
    { patterns: ['database', 'sql'], category: 'tool_failure' }
  ]
});

Types

The package exports all TypeScript interfaces:

import type {
  ToolCallRecord,
  VerificationResult,
  AgentMetadata,
  TokenUsage,
  AgentErrorCategory,
  ConfidenceFactors,
  ConfidenceScoreOptions,
  DataFreshnessOptions,
  NumericalCrosscheckOptions,
  TickerValidationOptions,
  ErrorCategorizerOptions,
  ErrorPattern,
  TraceSanitizerOptions,
  RedactionRule
} from 'langchain-agent-toolkit';

Framework Compatibility

  • LangChain.jsextractTokenUsage works with AIMessage from @langchain/core/messages
  • Vercel AI SDK — Types are compatible; pass tool call records in the standard format
  • Custom agents — No framework dependency except for token tracking (which accepts any)

Origin

Extracted from the Ghostfolio AI Finance Agent, built for the Gauntlet AI AgentForge training program. The verification and observability patterns were developed to solve real production problems: LLM hallucination of financial data, stale market prices, cost tracking across multi-turn conversations, and PII protection in observability traces.

License

AGPL-3.0 — consistent with Ghostfolio.