npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

perplexity-cache

v1.0.1

Published

A tiny TypeScript wrapper around the official Perplexity SDK that adds transparent caching for:

Downloads

99

Readme

perplexity-cache

A tiny TypeScript wrapper around the official Perplexity SDK that adds transparent caching for:

  • client.search.create(...)
  • client.responses.create(...)

It is designed as a drop-in API-compatible surface so you can keep your existing calling code and reduce duplicate API calls during iteration.

Why Use It

  • Avoid paying repeatedly for identical prompts during development.
  • Speed up repeated queries with local cache hits.
  • Keep the same method shape as the original Perplexity client (search.create and responses.create).

How It Works

PerplexityCached wraps a regular Perplexity client and a Cacheable instance.

  • On create(...), it builds a deterministic SHA-256 cache key from args.
  • If cache is enabled and a value exists, it returns the cached value.
  • Otherwise, it calls the network API and stores the response.

Cache Key Details

  • search.create: key = SHA-256 of normalized args.
    • If query is an array, it is normalized to a newline-joined string before hashing.
  • responses.create: key = SHA-256 of responses:${JSON.stringify(args)}.

This keeps search and responses keyspaces independent.

Requirements

  • Node.js 18+
  • A valid Perplexity API key in PERPLEXITY_API_KEY
  • Dependencies:
    • @perplexity-ai/perplexity_ai
    • cacheable
    • a cache store implementation (examples use @keyv/sqlite)

Installation

From this repository root:

npm install @perplexity-ai/perplexity_ai cacheable @keyv/sqlite

If you run examples with tsx, install it where you execute commands:

npm install -D tsx typescript

Quick Start

import Path from "node:path";
import KeyvSqlite from "@keyv/sqlite";
import { Cacheable, KeyvStoreAdapter } from "cacheable";
import { Perplexity } from "@perplexity-ai/perplexity_ai";
import { PerplexityCached } from "./src/perplexity_cached";

// Create the base Perplexity client with your API key
const baseClient = new Perplexity({
	apiKey: process.env.PERPLEXITY_API_KEY!,
});

// Create a Cacheable instance with a SQLite store - you can use any Keyv-compatible store or even an in-memory one for testing
const sqlitePath = Path.resolve(__dirname, ".perplexity_cache.sqlite");
const store = new KeyvSqlite(`sqlite://${sqlitePath}`);
const cacheable = new Cacheable({ secondary: store as KeyvStoreAdapter });

// Create a PerplexityCached instance that wraps the base client and cacheable
const perplexityCached = new PerplexityCached(baseClient, cacheable);

// Now you can use perplexityCached just like the original client, but with caching
const response = await perplexityCached.responses.create({
	model: "perplexity/sonar",
	input: "Summarize this page: https://example.com",
	tools: [{ type: "fetch_url" }],
});

// display the result
console.log(response.output_text);

Constructor

new PerplexityCached(perplexityClient, cache, { cacheEnabled?: boolean })
  • perplexityClient: instance of Perplexity
  • cache: instance of Cacheable
  • cacheEnabled (optional, default true):
    • true: read from cache and fallback to network on miss
    • false: skip cache reads, always call network, still write results to cache

API Surface

The wrapper exposes:

  • client.search.create(args)
  • client.responses.create(args)
  • client.setCacheEnabled(boolean)
  • await client.cleanCache()

Both methods keep the input/output typing from the official SDK.

Running Included Examples

From repository root:

# search.create with visible cache-hit timing
npx tsx ./examples/search_create.ts

# responses.create with fetch_url tool
npx tsx ./examples/response_create.ts

# pro-search preset response example
npx tsx ./examples/pro_search.ts

Notes

  • No TTL is enforced by this wrapper directly. Configure expiration in your Cacheable setup if needed.
  • Cached response payloads are returned as-is.
  • Cache key stability depends on argument structure and ordering in provided objects.

File Layout

perplexity-cache/
  src/perplexity_cached.ts
  examples/
    search_create.ts
    response_create.ts
    pro_search.ts