Return source citations with Vercel AI SDK
Use an AI SDK tool to fetch context before generating an answer. Return the source citations as structured metadata alongside the generated text and log the Nicia receipt ID for later inspection.
Use Node.js 24 or newer, an OpenAI API key, and a space key
with read on a space that contains a document or record you can ask about.
mkdir vercel-ai-sdkcd vercel-ai-sdknpm init -ynpm pkg set type=modulenpm install @ai-sdk/openai@4.0.126 @nicia-ai/client@^0.1.1 ai@7.0.185 zod@4.6.7Save this as recipe.ts.
import { openai } from "@ai-sdk/openai";import { generateText, stepCountIs, tool, type LanguageModel } from "ai";import { z } from "zod";import { NiciaSpaceClient } from "@nicia-ai/client";
export async function answer( space: NiciaSpaceClient, prompt: string, model: LanguageModel = openai("gpt-4.1-mini"),) { const result = await generateText({ model, system: "Answer from the lookup result. Cite the provided source slugs. If evidence is missing or disputed, say so.", prompt, tools: { lookup: tool({ description: "Get receipted context and source citations for the user's question.", inputSchema: z.object({}), execute: async () => { const { text, citations, receipt, warnings } = await space.getContext( { prompt }, ); console.info("Nicia receipt", receipt); return { text, citations, receipt, warnings }; }, }), }, prepareStep: ({ stepNumber }) => ({ toolChoice: stepNumber === 0 ? { type: "tool", toolName: "lookup" } : "none", }), stopWhen: stepCountIs(2), }); const lookup = result.steps.flatMap((step) => step.staticToolResults)[0] ?.output; if (!lookup) throw new Error("No context lookup result"); return { text: result.text, citations: lookup.citations, receipt: lookup.receipt, };}
export function receiptNow(space: NiciaSpaceClient, receiptId: string) { return space.getReceiptChanges(receiptId);}
if (import.meta.main) { const { NICIA_SPACE, NICIA_SPACE_KEY, NICIA_API_URL = "https://api.nicia.ai", } = process.env; if (!NICIA_SPACE || !NICIA_SPACE_KEY) throw new Error("Set NICIA_SPACE and NICIA_SPACE_KEY"); const space = new NiciaSpaceClient({ baseUrl: NICIA_API_URL, handle: NICIA_SPACE, credential: NICIA_SPACE_KEY, }); const [command, input] = process.argv.slice(2); if (!input) throw new Error( 'Usage: node recipe.ts ask "<question>" | audit <receipt-id>', ); if (command === "ask") console.log(JSON.stringify(await answer(space, input), null, 2)); else if (command === "audit") console.log(JSON.stringify(await receiptNow(space, input), null, 2)); else throw new Error( 'Usage: node recipe.ts ask "<question>" | audit <receipt-id>', );}Ask and audit
Section titled “Ask and audit”export NICIA_SPACE='your-space-handle'read -rsp 'Nicia space key: ' NICIA_SPACE_KEY; printf '\n'; export NICIA_SPACE_KEYread -rsp 'OpenAI API key: ' OPENAI_API_KEY; printf '\n'; export OPENAI_API_KEYnode recipe.ts ask 'What does our refund policy say?'Use a question relevant to your space. The output contains the answer, citations from the tool response, and the receipt ID. Keep the receipt ID with your response log. After updating a cited source, use the same space key to audit the original read.
node recipe.ts audit '<receipt-id>'The client’s getReceiptChanges calls the receipt’s /now endpoint.
Inspect changed for newer versions or answers for a standing answer whose
grounds moved. A receipt records the context the lookup served.