Dedupe leads, enrich missing firmographics, flag stale records, and queue cleanup tasks so your pipeline reports stay trustworthy.
You are a CRM data steward. You keep the database clean without breaking it. WORKFLOW: 1. Call detect_duplicates by email + domain + fuzzy company name. 2. Call enrich_record for accounts missing firmographics. 3. Call flag_stale records with no activity in 90+ days and no open opp. 4. Call queue_cleanup tasks rather than auto-deleting; humans approve before merge or archive. RULES: - Never auto-merge or delete records. Always queue for human approval. - Duplicate detection must use at least 2 signals (email + domain, name + phone). - Enrichment must cite the source URL so reps can verify. - Stale records with open opps over $10k are protected from any cleanup queue.
import { agent, tool } from "@agent-sdk"
import Exa from "exa-js"
import { z } from "zod"
const dedupeInput = z.object({
records: z.array(z.object({
id: z.string(),
email: z.string().email(),
company: z.string(),
domain: z.string().optional(),
})),
})
export default agent({
model: "claude-sonnet-4-6",
permissionMode: "bypassPermissions",
maxTurns: 30,
systemPrompt: `...`, // see System Prompt section above
tools: {
detect_duplicates: tool({
description: "Find dupes using email + domain + fuzzy company name",
inputSchema: dedupeInput,
execute: async ({ records }) => { /* clusters */ },
}),
enrich_record: tool({
description: "Fill missing firmographics with cited sources",
inputSchema: z.object({ recordId: z.string() }),
execute: async ({ recordId }) => {
const exa = new Exa(process.env.EXA_API_KEY)
return exa.search(recordId, { numResults: 4 })
},
}),
flag_stale: tool({
description: "Flag records with no activity in 90+ days",
inputSchema: z.object({ days: z.number().int().min(30) }),
execute: async ({ days }) => { /* stale[] */ },
}),
queue_cleanup: tool({
description: "Queue merge/archive tasks for human approval",
inputSchema: z.object({ tasks: z.array(z.any()) }),
execute: async ({ tasks }) => { /* job ids */ },
}),
},
})EXA_API_KEYExa.ai key for enrichmentCRM_API_KEYSalesforce / HubSpot API keyDirty CRM data costs more than dirty code. This runs nightly, queues clean fixes for humans, and never breaks pipeline reports.