Fingerprint normalized inputs alongside prompt and model versions
Raw timestamps, whitespace differences, or unordered CRM metadata keys cause accidental cache misses if you hash raw request bodies directly. Canonicalize only the semantic fields passed into the model—such as prospect notes, call transcript text, system prompt version, and target model identifier—and compute a SHA-256 digest.
Whenever you iterate on your prompt engineering or upgrade from one Claude Sonnet snapshot to another, incrementing the promptVersion constant automatically invalidates stale analyses without manual table wipes.
Deterministic SHA-256 fingerprint for Claude/OpenAI CRM analysis
import { createHash } from "node:crypto";
export const PROSPECT_PROMPT_VERSION = "v2.1";
export function buildAnalysisFingerprint(input: {
prospectId: string;
transcriptHash: string;
model: string;
}): string {
const canonical = JSON.stringify({
v: PROSPECT_PROMPT_VERSION,
model: input.model,
prospectId: input.prospectId,
transcriptHash: input.transcriptHash,
});
return createHash("sha256").update(canonical).digest("hex");
}Persist structured prospect analysis in PostgreSQL instead of ephemeral memory
In-memory caches disappear on serverless cold starts on Vercel, and short Redis TTLs force costly re-generation when an account manager re-opens a prospect weeks later. Persisting validated structured JSON in a PostgreSQL table indexed on (prospect_id, input_fingerprint) turns repeated dashboard views into sub-10ms database reads.
Track token_usage_input, token_usage_output, model, and created_at next to each stored analysis row so teams can audit real AI unit costs and verify savings directly from SQL.
Cache lookup and idempotent write-through in TypeScript + PostgreSQL
const existing = await db.query(
`SELECT analysis_json, model, created_at
FROM prospect_ai_analyses
WHERE prospect_id = $1 AND input_fingerprint = $2
LIMIT 1`,
[prospectId, fingerprint],
);
if (existing.rows[0]) {
return { cached: true, data: existing.rows[0].analysis_json };
}
const fresh = await callClaudeSonnetStructured(payload);
await db.query(
`INSERT INTO prospect_ai_analyses (prospect_id, input_fingerprint, model, prompt_version, analysis_json)
VALUES ($1, $2, $3, $4, $5)
ON CONFLICT (prospect_id, input_fingerprint) DO NOTHING`,
[prospectId, fingerprint, fresh.model, PROSPECT_PROMPT_VERSION, fresh.data],
);Coalesce concurrent triggers and expose explicit re-run controls
When both a webhook call-completion worker and a user opening the CRM UI request analysis simultaneously, pair PostgreSQL persistence with a short Redis lock or BullMQ jobId keyed by input_fingerprint. Only one inference call hits Anthropic or OpenAI.
Always provide an explicit 'Re-analyze' action that passes forceRefresh=true (generating a new timestamped or user-triggered run) so sales or operations teams never feel trapped behind stale AI output when external context changes.
- Validate model JSON output with a strict runtime schema before persisting to PostgreSQL.
- Never cache partial, truncated, or fallback error responses under the valid fingerprint.
- Combine persistent PostgreSQL caching with BullMQ retries for long call-transcription pipelines.