AI agents repeat queries more than humans do. A research crew running the same 30 keywords every morning hits a 60-80% cache rate after one week. This tutorial wires the cache.
Prerequisites
- Python 3.10+
- Scavio API key
- SQLite (built-in)
Walkthrough
Step 1: Define the cache key
Query plus surface plus modifiers.
import hashlib, json
def key(query, surface='search', **modifiers):
payload = json.dumps({'q': query, 's': surface, **modifiers}, sort_keys=True)
return hashlib.sha1(payload.encode()).hexdigest()Step 2: SQLite store
key, payload (JSON), timestamp.
import sqlite3, time
conn = sqlite3.connect('search.db')
conn.execute('CREATE TABLE IF NOT EXISTS cache(k TEXT PRIMARY KEY, payload TEXT, ts REAL)')Step 3: Wrapper with TTL
1 hour for SERP, 30 min for Reddit, 24 hours for static.
import os, requests
H = {'x-api-key': os.environ['SCAVIO_API_KEY']}
def search(query, ttl=3600):
k = key(query, 'search')
row = conn.execute('SELECT payload, ts FROM cache WHERE k=?', (k,)).fetchone()
if row and time.time() - row[1] < ttl:
return json.loads(row[0])
data = requests.post('https://api.scavio.dev/api/v1/search', headers=H, json={'query': query}).json()
conn.execute('INSERT OR REPLACE INTO cache VALUES (?, ?, ?)', (k, json.dumps(data), time.time()))
conn.commit()
return dataStep 4: Per-surface TTL tuning
Different surfaces need different freshness.
TTL = {
'search': 3600,
'reddit_search': 1800,
'amazon_search': 21600,
'extract': 86400
}Step 5: Monitor cache hit rate
Log hits vs misses.
# Every call logs hit/miss to a counter table.
# Daily: SELECT date, hits / (hits+misses) AS rate FROM stats.Python Example
# Daily Scavio spend before cache: ~$0.42.
# Daily Scavio spend after 67% hit rate: ~$0.14.
# Tail latency drops from 1.2s to 35ms on hits.JavaScript Example
// Same architecture in TS with better-sqlite3.Expected Output
60-80% cache hit rate after one week. Per-day Scavio cost drops accordingly. Tail latency on repeat queries falls to single-digit ms.