ScavioScavio
ProductPricingDocs
Sign InGet Started
  1. Home
  2. Tutorials
  3. How to Cache Search Results for AI Agents
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.

Get Free API KeyAPI Docs

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

  • How to Build a LangChain DaaS Pipeline in 2026

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 50 free credits on signup.

Yes. The free tier includes 50 credits on signup, 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.

Related Resources

Best Of

Best Budget Search APIs for AI Agents Under $10/mo (2026)

Read more
Best Of

Best Search API for Deep Research Agents in 2026

Read more
Use Case

MCP Search Gateway for Multi-Agent Systems

Read more
Use Case

Pi Coding Agent Multi-Platform Search

Read more
Glossary

Search Cache Layer

Read more
Glossary

Search API Provider Landscape (2026)

Read more

Start Building

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

Get Free API KeyRead the Docs
ScavioScavio

Real-time search API for AI agents. Search every platform, not just Google.

Product

  • Features
  • Pricing
  • Dashboard
  • Affiliates

Developers

  • Documentation
  • API Reference
  • Quickstart
  • MCP Integration
  • Python SDK

Alternatives

  • Tavily Alternative
  • SerpAPI Alternative
  • Firecrawl Alternative
  • Exa Alternative

Tools

  • JSON Formatter
  • cURL to Code
  • Token Counter
  • All Tools

© 2026 Scavio. All rights reserved.

Featured on TAAFT
Terms of ServicePrivacy Policy