Tutorial

How to Cache Search Results for AI Agents

AI agents repeat queries more than humans. SQLite + 50ms cache hits cut Scavio cost 60-80% on repeat traffic.

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.

Python
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.

Python
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.

Python
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 data

Step 4: Per-surface TTL tuning

Different surfaces need different freshness.

Python
TTL = {
    'search': 3600,
    'reddit_search': 1800,
    'amazon_search': 21600,
    'extract': 86400
}

Step 5: Monitor cache hit rate

Log hits vs misses.

Text
# Every call logs hit/miss to a counter table.
# Daily: SELECT date, hits / (hits+misses) AS rate FROM stats.

Python Example

Python
# 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

JavaScript
// Same architecture in TS with better-sqlite3.

Expected Output

JSON
60-80% cache hit rate after one week. Per-day Scavio cost drops accordingly. Tail latency on repeat queries falls to single-digit ms.

Related Tutorials

Frequently Asked Questions

Most developers complete this tutorial in 15 to 30 minutes. You will need a Scavio API key (free tier works) and a working Python or JavaScript environment.

Python 3.10+. Scavio API key. SQLite (built-in). A Scavio API key gives you 500 free credits per month.

Yes. The free tier includes 500 credits per month, which is more than enough to complete this tutorial and prototype a working solution.

Scavio has a native LangChain package (langchain-scavio), an MCP server, and a plain REST API that works with any HTTP client. This tutorial uses the raw REST API, but you can adapt to your framework of choice.

Start Building

AI agents repeat queries more than humans. SQLite + 50ms cache hits cut Scavio cost 60-80% on repeat traffic.