Analytics Reference
NeuroLink provides comprehensive analytics capabilities for tracking token usage, costs, performance metrics, and quality evaluation across all AI provider interactions.
Overview
The analytics system in NeuroLink consists of several interconnected components:
| Component | Purpose |
|---|---|
| Token Usage Tracking | Monitor input/output tokens, cache tokens, and reasoning tokens |
| Cost Analytics | Estimate and track costs across providers and models |
| Performance Metrics | Measure response times, throughput, and memory usage |
| Quality Evaluation | Assess response relevance, accuracy, and completeness |
| Middleware Integration | Automatic analytics collection via middleware |
Token Usage Tracking
Basic Token Usage
NeuroLink automatically tracks token usage for every generation:
import { NeuroLink } from "@juspay/neurolink";
const neurolink = new NeuroLink();
const result = await neurolink.generate({
input: { text: "Explain quantum computing in simple terms" },
provider: "openai",
enableAnalytics: true,
});
// Access token usage
console.log("Token Usage:", {
input: result.usage?.input,
output: result.usage?.output,
total: result.usage?.total,
});
// Full analytics data
console.log("Analytics:", result.analytics);
TokenUsage Type
The TokenUsage type provides detailed token information:
type TokenUsage = {
/** Number of input/prompt tokens */
input: number;
/** Number of output/completion tokens */
output: number;
/** Total tokens (input + output) */
total: number;
/** Tokens used to create cache entries (Anthropic, Google) */
cacheCreationTokens?: number;
/** Tokens read from cache (cost savings) */
cacheReadTokens?: number;
/** Tokens used for reasoning/thinking (o1, Claude thinking) */
reasoning?: number;
/** Percentage of cost saved through caching */
cacheSavingsPercent?: number;
};
Cache Token Tracking
For providers that support prompt caching (Anthropic, Google), NeuroLink tracks cache metrics:
const result = await neurolink.generate({
input: { text: "Analyze this document..." },
provider: "anthropic",
enableAnalytics: true,
});
if (result.analytics?.tokenUsage) {
const { cacheCreationTokens, cacheReadTokens, cacheSavingsPercent } =
result.analytics.tokenUsage;
if (cacheCreationTokens) {
console.log(`Cache created: ${cacheCreationTokens} tokens`);
}
if (cacheReadTokens) {
console.log(`Cache hit: ${cacheReadTokens} tokens`);
console.log(`Cost savings: ${cacheSavingsPercent}%`);
}
}
Reasoning Token Tracking
For models with extended thinking capabilities (OpenAI o1, Anthropic Claude with thinking, Gemini 3):
const result = await neurolink.generate({
input: { text: "Solve this complex mathematical proof..." },
provider: "openai",
model: "o1-mini",
enableAnalytics: true,
});
if (result.analytics?.tokenUsage.reasoning) {
console.log(
`Reasoning tokens used: ${result.analytics.tokenUsage.reasoning}`,
);
}
Cost Analytics
Automatic Cost Estimation
NeuroLink automatically estimates costs based on provider pricing:
const result = await neurolink.generate({
input: { text: "Write a detailed business plan" },
provider: "openai",
model: "gpt-4o",
enableAnalytics: true,
});
if (result.analytics?.cost !== undefined) {
console.log(`Estimated cost: $${result.analytics.cost.toFixed(5)}`);
}
Cost Calculation Formula
Costs are calculated using per-token pricing:
// Internal cost calculation
const inputCost = (tokens.input / 1000) * costInfo.input;
const outputCost = (tokens.output / 1000) * costInfo.output;
const totalCost = inputCost + outputCost;
Provider Pricing Configuration
NeuroLink uses configurable pricing for each provider:
| Provider | Default Input Cost (per 1K) | Default Output Cost (per 1K) |
|---|---|---|
| OpenAI | $0.00015 | $0.0006 |
| Anthropic | $0.0015 | $0.0075 |
| Google AI | $0.000075 | $0.0003 |
| Google Vertex | $0.000075 | $0.0003 |
| Bedrock | $0.0015 | $0.0075 |
| Azure | $0.00015 | $0.0006 |
| Mistral | $0.0001 | $0.0003 |
| HuggingFace | $0.0002 | $0.0008 |
| Ollama | $0 | $0 |
Custom Cost Configuration
Override default pricing via environment variables:
# Custom pricing for Google AI
GOOGLE_AI_DEFAULT_INPUT_COST=0.0001
GOOGLE_AI_DEFAULT_OUTPUT_COST=0.0004
# Custom pricing for OpenAI
OPENAI_DEFAULT_INPUT_COST=0.0002
OPENAI_DEFAULT_OUTPUT_COST=0.0008