Claude API Research Assistant: Build Tutorial
Building a research assistant with the Claude API means wiring together three things: a search/retrieval tool Claude can call, a system prompt that enforces citation discipline, and a loop that handles multi-turn tool use until Claude has enough information to answer. This tutorial walks through all three with working code, from a single-shot Q&A tool to a full multi-step research loop that streams results to a UI.
If you landed here looking for "how do I build a research assistant on the Claude API," the short answer is: give Claude a web_search-style tool (or a wrapper around your own search/document API), let it decide when to call it, feed results back as tool results, and keep looping until it stops requesting tools. The rest of this guide covers each piece in detail.
What a research assistant actually needs
A research assistant is not a chatbot with a longer system prompt. It needs:
- A retrieval mechanism — a search API, a vector store, or scraped documents it can query
- Tool-calling support — Claude decides when to search, what query to use, and when it has enough information
- A loop, not a single request — most non-trivial questions require 2-4 rounds of search-then-reason
- Citation tracking — you want the assistant to reference where each claim came from
- Streaming — research answers are long; streaming keeps the UI responsive while Claude writes
Step 1: Define the search tool
Claude's tool-use feature lets you describe a function and let the model decide when to call it. Here's a minimal search tool definition:
{
"name": "search_web",
"description": "Search the web for current information. Use this when you need facts, recent events, or data you don't already know.",
"input_schema": {
"type": "object",
"properties": {
"query": { "type": "string", "description": "The search query" }
},
"required": ["query"]
}
}
You'll implement search_web server-side — calling a real search API (Bing, Serper, Tavily, your own indexed documents, whatever you have access to) and returning results as plain text or JSON.
Step 2: Write a system prompt that forces grounded answers
The biggest failure mode for research assistants is confident answers with no basis. Constrain it explicitly:
You are a research assistant. When asked a factual question:
1. Use search_web to gather information before answering.
2. Never state a fact you haven't retrieved or already verified.
3. Cite sources inline as [1], [2], etc., and list them at the end.
4. If search results conflict, say so explicitly.
5. If you're not confident after searching, say what's missing.
This alone cuts hallucinated claims dramatically because it gives Claude an explicit "search first" instruction rather than relying on implicit behavior.
Step 3: Build the tool-use loop
The core logic is a loop: send the message, check if Claude requested a tool, execute it, send the result back, repeat until Claude returns a final text response.
async function runResearch(question) {
const messages = [{ role: "user", content: question }];
while (true) {
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: 2048,
system: RESEARCH_SYSTEM_PROMPT,
tools: [searchToolDefinition],
messages
})
});
const data = await res.json();
messages.push({ role: "assistant", content: data.content });
const toolCalls = data.content.filter(b => b.type === "tool_use");
if (toolCalls.length === 0) {
return data.content.find(b => b.type === "text").text;
}
const toolResults = [];
for (const call of toolCalls) {
const result = await searchWeb(call.input.query);
toolResults.push({
type: "tool_result",
tool_use_id: call.id,
content: JSON.stringify(result)
});
}
messages.push({ role: "user", content: toolResults });
}
}
This loop keeps running as long as Claude keeps calling the tool, which is exactly the behavior you want for multi-step research — Claude might search once, read the results, decide it needs a follow-up query, search again, then write the final answer.
If you're routing requests through SubToAPI, this loop works unchanged — it's a standard Claude Messages API call with an Authorization: Bearer $SUBTOAPI_KEY header and your sub_live_... key, so any existing Claude tool-use code ports over directly. See /docs/tools for the full tool-use reference and /docs/messages for the base request/response shape.
Step 4: Add streaming for long answers
Research answers tend to be long — several paragraphs with citations. Streaming avoids making users stare at a spinner for 10+ seconds. Set "stream": true and parse server-sent events incrementally; the final tool-use loop iteration (the one that returns text instead of a tool call) is the one you stream to the client. Details and a working example are in /docs/streaming.
Step 5: Track and dedupe sources
Keep a running array of { url, title, query } for every search result returned, keyed by an incrementing citation number. When Claude's final answer references [1], [2], map those back to your source list and render them as a reference section below the answer. Dedupe by URL across multiple search calls so the same source doesn't get cited twice under different numbers.
Step 6: Guard against runaway loops
Cap the number of tool-use rounds (5-6 is usually plenty) to avoid infinite search loops on ambiguous questions, and set a reasonable max_tokens per call. If you hit the cap without a final answer, have Claude summarize what it found so far instead of failing silently.
Where SubToAPI fits
None of the above is specific to any provider wrapper — it's standard Claude tool use. What SubToAPI adds is operational plumbing once you move this from a prototype to something with real users: issue separate sub_live_... keys per project or environment, get per-key usage and cost breakdowns so you know which research queries are expensive, and add teammates without sharing a root Anthropic key. Plans start at €9/month for solo use, with Team (€19/seat) and Scale (€49/seat) tiers for larger usage, and there's a free trial at /signup if you want to test the loop above against a real endpoint. Full setup is in /docs/quickstart.
Questions
Does Claude have built-in web search, or do I need my own search API? Claude's tool-use feature lets the model call any tool you define, including a web search tool — but you need to implement the actual search call yourself using a third-party search API or your own document index. Claude decides when and what to search for; it doesn't browse the web on its own.
How many tool-use rounds does a typical research question need? Simple factual questions usually resolve in one search-then-answer round. Multi-part or comparative questions ("compare X and Y's latest releases") often need 2-4 rounds as Claude issues follow-up searches based on what the first results contained.
Can I use this pattern with my own private documents instead of web search? Yes — swap the search tool's implementation to query a vector database or document store instead of a web search API. The tool-use loop and system prompt structure stay identical; only what happens inside search_web changes.