Prerequisites
- An Agentset API key and namespace ID.
- An OpenAI API key (or another AI SDK provider).
.env.local
System prompt
Use the system prompt below as a starting point and modify it for your use case:Sample system prompt
Sample system prompt
Define the search tool
Wrapns.search in an AI SDK tool. The model calls this tool with a query it generates, so describe the tool and its input clearly.
Run the agentic loop
Pass the tool tostreamText. Set stopWhen so the model can run several searches before answering—without a limit, a tool-calling loop can run indefinitely.
search tool as many times as it needs, then streams the final answer. Multi-part questions—like the billing example above—typically trigger separate searches for “failed payments” and “refunds.”
Multi-turn conversations
For a chat application, pass the full message history asmessages instead of a single prompt. The model uses earlier turns to interpret follow-up questions and search accordingly.
Add citations
Return a stable identifier with each chunk and instruct the model to cite by it. Because the model reads chunks across several searches, citing by ID is more reliable than position-based numbering.Tune retrieval
Configure the underlying search per call to balance recall, relevance, and token usage.| Option | Recommendation |
|---|---|
topK | Raise for broad questions, lower to save tokens. The model can search again if needed. |
rerank | Keep enabled to surface the most relevant chunks first. |
mode | Use keyword for exact terms (error codes, IDs); semantic (default) otherwise. |
filter | Scope results by metadata, e.g. per-user or per-document. |
Next steps
- Search — Configure search parameters and reranking
- Citations — Add source attribution to responses
- Filtering — Scope searches with metadata filters
- Observability — Trace each search and generation step