V3 APIs
ThinkHive SDK v3.0 introduces a run-centric architecture with powerful APIs for runs, claims, calibration, and ticket linking.
Runs API
The Runs API tracks complete agent executions.
import { runs } from '@thinkhive/sdk';
// Create a run
const run = await runs.create({
name: 'support-agent-run',
input: userMessage,
output: agentResponse,
outcome: 'success',
metadata: {
agentId: 'support-123',
customerId: 'cust_456',
},
});
// Analyze a run
const analysis = await runs.analyze(run.id);
console.log(analysis.ragEvaluation);
console.log(analysis.hallucinationReport);
// Get run details
const runDetails = await runs.get(run.id);
// List runs with filters
const recentRuns = await runs.list({
agentId: 'support-123',
outcome: 'failure',
limit: 50,
offset: 0,
});Run Methods
| Method | Description |
|---|---|
runs.create(options) | Create a new run |
runs.get(runId) | Get run by ID |
runs.list(filters) | List runs with filters |
runs.analyze(runId) | Trigger analysis on a run |
runs.update(runId, data) | Update run metadata |
Claims API
The Claims API extracts and analyzes facts vs. inferences from runs.
import { claims, isFact, isInference } from '@thinkhive/sdk';
// Get claims from a run
const runClaims = await claims.getRunAnalysis(run.id);
// Filter by type
const facts = runClaims.filter(isFact);
const inferences = runClaims.filter(isInference);
// Get high-confidence claims
const highConfidence = claims.getHighConfidenceClaims(runClaims, 0.95);
// Group claims
const byType = claims.groupClaimsByType(runClaims);
const byCategory = claims.groupClaimsByCategory(runClaims);Claim Structure
interface Claim {
id: string;
content: string;
type: 'fact' | 'inference';
confidence: number; // 0-1
source?: string; // Source document ID
supported: boolean; // Verified against sources
category?: string; // e.g., 'product', 'policy', 'pricing'
}Helper Functions
import {
isFact,
isInference,
isHighConfidence,
isSupported,
} from '@thinkhive/sdk';
// Type guards
if (isFact(claim)) {
console.log('This is a verifiable fact');
}
if (isInference(claim)) {
console.log('This is a derived conclusion');
}
// Confidence checks
if (isHighConfidence(claim, 0.9)) {
console.log('High confidence claim');
}Calibration API
The Calibration API measures how well confidence scores predict actual outcomes.
import { calibration, calculateBrierScore } from '@thinkhive/sdk';
// Get calibration metrics for a run
const scores = await calibration.getRunCalibration(run.id);
console.log(scores.brierScore); // Lower is better (0-1)
console.log(scores.expectedCalibrationError); // ECE metric
console.log(scores.status); // 'well_calibrated', 'over_confident', etc.
// Calculate Brier score manually
const predictions = [0.9, 0.7, 0.8, 0.6];
const actuals = [1, 1, 0, 1]; // 1 = correct, 0 = incorrect
const brier = calculateBrierScore(predictions, actuals);Calibration Metrics
| Metric | Range | Ideal | Description |
|---|---|---|---|
brierScore | 0-1 | 0 | Lower is better; measures prediction accuracy |
expectedCalibrationError | 0-1 | 0 | Lower is better; measures calibration quality |
status | - | well_calibrated | Overall calibration assessment |
Calibration Status Values
well_calibrated- Predictions match reality (ECE < 0.1)under_confident- Predictions too conservativeover_confident- Predictions too aggressiveinsufficient_data- Not enough data to assess
Ticket Linking API
Link runs to support tickets using 7 deterministic methods.
import {
linking,
linkRunToZendeskTicket,
generateZendeskMarker,
LINK_METHOD_CONFIDENCE,
} from '@thinkhive/sdk';
// Generate a marker for ticket correlation
const marker = generateZendeskMarker(run.id);
// Returns: "THINKHIVE:run_abc123"
// Add marker to ticket
await zendeskAPI.addComment(ticketId, `Analyzed by ThinkHive: ${marker}`);
// Link a run to a Zendesk ticket
const link = await linkRunToZendeskTicket(run.id, ticketId);
console.log(link.method); // e.g., 'marker_embedding'
console.log(link.confidence); // e.g., 0.99
// Get available linking methods for a run
const methods = await linking.getMethods(run.id);
const bestMethod = methods.reduce((a, b) =>
a.confidence > b.confidence ? a : b
);Linking Methods
| Method | Confidence | Description |
|---|---|---|
explicit_run_id | 1.0 | Run ID stored in ticket |
marker_embedding | 0.99 | ThinkHive marker in ticket |
timestamp_correlation | 0.85 | Matched by time window |
conversation_thread | 0.90 | Same conversation thread |
session_id | 0.95 | Same customer session |
semantic_similarity | 0.70 | Content matching |
manual_linking | 1.0 | Manually linked by user |
Customer Context API
Track customer state over time for business correlation.
import { customerContext, captureCustomerContext } from '@thinkhive/sdk';
// Capture current customer state
const snapshot = await captureCustomerContext({
customerId: 'cust_123',
metrics: {
subscription_tier: 'premium',
arr: 5000,
health_score: 0.85,
last_support_contact: '2024-01-15',
nps_score: 8,
},
});
// Get context at a specific time
const historical = await customerContext.getContextAsOf(
'cust_123',
new Date('2024-01-01')
);
// Analyze context changes
const before = await customerContext.getContextAsOf(customerId, startDate);
const after = await customerContext.getContextAsOf(customerId, endDate);
const arrChange = after.metrics.arr - before.metrics.arr;
const healthTrend = after.metrics.health_score - before.metrics.health_score;Context Snapshots
interface CustomerContextSnapshot {
customerId: string;
timestamp: Date;
metrics: {
subscription_tier?: string;
arr?: number;
health_score?: number;
last_support_contact?: string;
nps_score?: number;
[key: string]: any; // Custom metrics
};
}Error Handling
All V3 APIs throw typed errors:
import { ThinkHiveApiError, ThinkHiveValidationError } from '@thinkhive/sdk';
try {
await runs.get('invalid_id');
} catch (error) {
if (error instanceof ThinkHiveApiError) {
console.log(error.status); // HTTP status code
console.log(error.message); // Error message
console.log(error.code); // Error code
}
if (error instanceof ThinkHiveValidationError) {
console.log(error.field); // Invalid field name
console.log(error.message); // Validation message
}
}Rate Limits: V3 APIs are subject to your tier’s rate limits. See API Tiers for details.
Next Steps
- Framework Integrations - Auto-instrument OpenAI and LangChain
- Examples - Complete working examples
- API Reference - REST API documentation