Build a Claude API Lead Qualification Chatbot
Building a lead qualification chatbot with the Claude API means combining a structured conversation flow, a scoring rubric, and a way to extract structured data (like BANT fields or ICP fit) from a natural conversation. This article walks through the actual architecture: prompt design, tool use for structured output, scoring logic, and how to connect it to your CRM without writing a custom auth layer.
The core problem most teams hit isn't "can Claude hold a conversation" — it can. The problem is turning that conversation into a qualification score and a clean payload your sales team or CRM can act on. That requires three things: a system prompt that enforces qualification criteria, tool use (function calling) to force structured output, and a pipeline that persists the result. Below is a working approach you can adapt.
Define your qualification criteria first
Before writing any code, write down what "qualified" means for your business. A typical B2B SaaS rubric uses BANT or a custom ICP scorecard:
- Budget: can they afford the product tier they're asking about?
- Authority: are they a decision-maker or influencer?
- Need: does their stated problem match what you solve?
- Timeline: are they evaluating now or "just looking"?
Convert this into explicit instructions in the system prompt. Vague prompts like "qualify the lead" produce inconsistent scoring. Be specific about thresholds.
You are a lead qualification assistant for a B2B SaaS product.
Ask questions naturally, one at a time, to determine:
1. Company size (1-10, 11-50, 51-200, 200+)
2. Role (decision-maker, influencer, end-user)
3. Primary problem they're trying to solve
4. Timeline (this month, this quarter, exploring)
5. Budget range if volunteered (never ask directly)
Do not reveal the scoring criteria to the user.
Keep responses under 3 sentences. Be conversational, not interrogative.
When you have enough information, call the `qualify_lead` tool.
Use tool calling to force structured output
The biggest mistake in these builds is parsing free text for scores. Instead, define a tool schema and let Claude call it once it has gathered enough information. This guarantees a predictable JSON shape for your backend.
{
"name": "qualify_lead",
"description": "Submit the final qualification result once enough information has been gathered.",
"input_schema": {
"type": "object",
"properties": {
"company_size": { "type": "string" },
"role": { "type": "string", "enum": ["decision_maker", "influencer", "end_user", "unknown"] },
"problem_summary": { "type": "string" },
"timeline": { "type": "string", "enum": ["immediate", "this_quarter", "exploring"] },
"budget_signal": { "type": "string" },
"score": { "type": "integer", "minimum": 0, "maximum": 100 },
"qualified": { "type": "boolean" }
},
"required": ["company_size", "role", "problem_summary", "timeline", "score", "qualified"]
}
}
If you're routing requests through SubToAPI, the request/response shape matches the standard Messages format, so tool use works exactly as documented at /docs/tools. Define the tool once, pass it on every call, and inspect the response for a tool_use content block instead of regex-matching the assistant's reply.
Wire up the conversation loop
A minimal server-side loop looks like this:
async function sendMessage(messages, tools) {
const res = await fetch("https://api.subtoapi.app/v1/messages", {
method: "POST",
headers: {
"Authorization": `Bearer ${process.env.SUBTOAPI_KEY}`,
"Content-Type": "application/json"
},
body: JSON.stringify({
model: "claude-sonnet-4-5",
max_tokens: 500,
system: QUALIFICATION_SYSTEM_PROMPT,
messages,
tools
})
});
return res.json();
}
On the client side, render Claude's text reply as the chatbot message. On the server side, check each response for a tool_use block named qualify_lead. When you see it, stop the conversation, persist the structured payload (score, qualified flag, problem summary) to your CRM or database, and optionally send an internal Slack notification for high-scoring leads.
const toolUse = response.content.find(c => c.type === "tool_use");
if (toolUse && toolUse.name === "qualify_lead") {
await saveLeadResult(toolUse.input);
return "Thanks — someone from our team will reach out shortly.";
}
Full request/response structure, including streaming, is documented at /docs/messages and /docs/streaming if you want the bot to feel responsive while it's "thinking" through longer qualification sequences.
Scoring logic: let Claude score, but validate server-side
Letting the model assign a 0–100 score inside the tool call is convenient, but don't trust it blindly for routing decisions. Add a thin server-side layer that re-derives a weighted score from the structured fields (role, timeline, company size) so your sales team gets consistent numbers even if the model's internal scoring drifts across conversations.
function computeFinalScore(input) {
let score = 0;
if (input.role === "decision_maker") score += 40;
if (input.timeline === "immediate") score += 30;
if (["51-200", "200+"].includes(input.company_size)) score += 20;
if (input.budget_signal) score += 10;
return Math.min(score, 100);
}
This also protects you from prompt injection attempts where a user tries to talk the model into inflating its own score.
Handling edge cases
- Ambiguous answers: instruct Claude to ask one clarifying follow-up before defaulting to "unknown" rather than guessing.
- Hostile or spam users: add an early-exit instruction — if the user is clearly not a fit (student project, competitor research), end the conversation politely rather than burning tokens on a long qualification flow.
- Multi-session leads: persist conversation state (e.g., in a database keyed by session ID) so a returning visitor doesn't restart qualification from scratch. Claude doesn't retain memory between API calls — you own that state.
Getting this into production
Once the logic works locally, the remaining work is operational: API key management per environment, rate limiting so one bad actor can't drive up costs, and visibility into token usage per conversation. SubToAPI handles this layer directly — you get a sub_live_... key scoped to your app, per-key usage metadata so you can see exactly how many tokens your qualification bot is burning per lead, and team seats if multiple people manage the integration. Pricing starts at €9/month on the Solo plan, with a free trial at /signup; see /pricing for Team and Scale tiers if you're running this across multiple products or clients. Setup takes about the same time as following /docs/quickstart.
Questions
Does the chatbot need a separate NLP model for scoring? No. Claude can produce the structured score directly via tool calling, and you add a thin server-side validation layer on top of the fields it returns — no separate classifier needed.
How many messages should a qualification flow take? Keep it to 4–6 exchanges. Longer flows increase drop-off; if you haven't gathered enough signal by then, route to a human rather than continuing to probe.
Can this integrate with an existing CRM? Yes — once Claude calls the qualify_lead tool, your backend receives a plain JSON object you can POST to any CRM's API (HubSpot, Salesforce, Pipedrive) using their existing webhook or REST integration.