RAG citations fail when the LLM cannot tie a claim back to a source URL. Scavio's typed JSON gives every retrieved snippet a link field, making the citation step deterministic. This tutorial walks the build.
Prerequisites
- Python 3.10+
- Scavio API key
- An LLM API key
Walkthrough
Step 1: Retrieve candidates
Scavio /search for the question.
import os, requests
H = {'x-api-key': os.environ['SCAVIO_API_KEY']}
def retrieve(q, n=8):
r = requests.post('https://api.scavio.dev/api/v1/search', headers=H, json={'query': q}).json()
return r.get('organic_results', [])[:n]Step 2: Build a numbered source list
Each source has an index.
def sources(results):
return [(i, r['link'], r.get('snippet', '')) for i, r in enumerate(results, start=1)]Step 3: Prompt the LLM with cite markers
Strict instruction.
# Prompt:
# 'Answer the question using ONLY the provided sources.
# Every claim must be followed by [N] where N is the source index.
# Sources:
# [1] {url}: {snippet}
# [2] {url}: {snippet}
# ...'Step 4: Validate citation markers
Regex check before showing user.
import re
def validate(answer, n_sources):
cites = set(int(m) for m in re.findall(r'\[(\d+)\]', answer))
return cites and max(cites) <= n_sourcesStep 5: Render answer with linked citations
[1] becomes a clickable link.
def render(answer, sources):
out = answer
for i, url, _ in sources:
out = out.replace(f'[{i}]', f'[[{i}]]({url})')
return outPython Example
# Per query: 1 Scavio call + 1 LLM call. Cost ~$0.005-0.02 depending on LLM.JavaScript Example
// Same in TS.Expected Output
User-facing answer with clickable citations. Each citation links to the original source, satisfying RAG trust requirements.