Add live search to a customer support agent by integrating a search API call that triggers when the agent's internal knowledge base has no relevant answer. This prevents the agent from hallucinating about product features, pricing, or outage status when the KB is stale. Scavio's API returns current Google results that the agent can use as grounding context before generating a response. This tutorial builds a support agent middleware that decides when to search and how to inject results into the agent's context.
Prerequisites
- Python 3.8+ or Node.js 18+ installed
- requests library (Python) or built-in fetch (JS)
- A Scavio API key from scavio.dev
- An existing support agent or chatbot framework
Walkthrough
Step 1: Define the search trigger logic
Build a classifier that decides whether a support query needs live search or can be answered from the internal KB alone.
import os, requests
API_KEY = os.environ['SCAVIO_API_KEY']
SEARCH_TRIGGERS = ['current', 'latest', 'today', 'outage', 'status', 'pricing', 'update', 'new feature', 'changelog', 'release']
def needs_live_search(query: str) -> bool:
query_lower = query.lower()
return any(trigger in query_lower for trigger in SEARCH_TRIGGERS)Step 2: Fetch live context from search
When search is triggered, query Scavio with the customer's question scoped to your product's domain for the most relevant results.
def fetch_support_context(query: str, product_domain: str = '') -> str:
search_query = f'{query} site:{product_domain}' if product_domain else query
resp = requests.post('https://api.scavio.dev/api/v1/search',
headers={'x-api-key': API_KEY},
json={'platform': 'google', 'query': search_query}, timeout=10)
results = resp.json().get('organic_results', [])
context_parts = []
for r in results[:3]:
context_parts.append(f"Title: {r.get('title', '')}\nURL: {r.get('link', '')}\nSnippet: {r.get('snippet', '')}")
return '\n\n'.join(context_parts)Step 3: Inject context into the agent prompt
Prepend the live search results to the agent's system prompt so it uses current information when answering.
def build_support_prompt(user_query: str, kb_answer: str, product_domain: str) -> str:
prompt = f'Customer question: {user_query}\n\n'
prompt += f'Knowledge base answer: {kb_answer}\n\n'
if needs_live_search(user_query):
live_context = fetch_support_context(user_query, product_domain)
prompt += f'Live web context (use if KB answer is outdated):\n{live_context}\n\n'
prompt += 'Respond helpfully. Prefer live data over KB if there is a conflict. Cite URLs when using web results.'
return prompt
result = build_support_prompt(
'Is there a current outage?',
'No known outages.',
'status.example.com'
)
print(result)Step 4: Handle fallback when search has no results
Gracefully degrade when the search API returns no results or times out, falling back to the KB answer only.
def safe_support_search(query: str, product_domain: str = '') -> str:
try:
context = fetch_support_context(query, product_domain)
if context.strip():
return context
except Exception as e:
print(f'Search fallback: {e}')
return ''
def answer_support_query(user_query: str, kb_answer: str, product_domain: str) -> dict:
live_context = safe_support_search(user_query, product_domain) if needs_live_search(user_query) else ''
return {
'query': user_query,
'kb_answer': kb_answer,
'live_context': live_context,
'used_search': bool(live_context),
}Python Example
import requests, os
H = {'x-api-key': os.environ['SCAVIO_API_KEY']}
def support_search(query, domain=''):
q = f'{query} site:{domain}' if domain else query
data = requests.post('https://api.scavio.dev/api/v1/search', headers=H,
json={'platform': 'google', 'query': q}, timeout=10).json()
return [{'title': r['title'], 'url': r['link'], 'snippet': r.get('snippet', '')}
for r in data.get('organic_results', [])[:3]]
print(support_search('current outage status', 'status.example.com'))JavaScript Example
const H = {'x-api-key': process.env.SCAVIO_API_KEY, 'Content-Type': 'application/json'};
async function supportSearch(query, domain = '') {
const q = domain ? `${query} site:${domain}` : query;
const r = await fetch('https://api.scavio.dev/api/v1/search', {
method: 'POST', headers: H, body: JSON.stringify({platform: 'google', query: q})
});
const results = (await r.json()).organic_results || [];
return results.slice(0, 3).map(r => ({title: r.title, url: r.link, snippet: r.snippet}));
}
supportSearch('current outage status', 'status.example.com').then(console.log);Expected Output
A support agent middleware that triggers live web search for time-sensitive queries and injects fresh context into the agent's response pipeline.