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

@ferrow/prompt-cache-toolkit

v1.0.0

Published

Maximize Claude API token efficiency with prompt caching

Downloads

51

Readme

Prompt Cache Toolkit

CI

Small helper for working with Claude's prompt-caching feature. Wraps a piece of text (system instructions, documents, etc.) so it can be reused across requests with the cache_control marker Claude's API expects, and gives you simple bookkeeping around cache hits if you feed it usage data.

Quick Start

npm install prompt-cache-toolkit
const { createCachedPrompt } = require('prompt-cache-toolkit');
const Anthropic = require('@anthropic-ai/sdk');

const client = new Anthropic({
  apiKey: process.env.ANTHROPIC_API_KEY,
});

// Define cached context (system instructions, documents, etc.)
const cached = createCachedPrompt(`
You are a code reviewer. Review this code for:
- Security issues
- Performance problems
- Best practices
`);

const response = await client.messages.create({
  model: 'claude-sonnet-5',
  max_tokens: 1024,
  system: cached.asSystemPrompt(),
  messages: [{ role: 'user', content: codeSnippet1 }],
});

// Feed the API's usage block back in if you want hit-rate/savings tracking
cached.recordUsage(response.usage);

console.log(`Estimated tokens saved: ${cached.tokensSaved()}`);

What it actually does

  • Formats a string into the cache_control: { type: 'ephemeral' } shape Claude's Messages API expects for prompt caching.
  • Warns (via console.warn) if the content looks larger than a configurable token estimate.
  • If — and only if — you call .recordUsage() with the usage object from each API response, it keeps a running count of cache creates vs. cache reads, and can report a hit rate and an estimated token savings. Nothing is tracked automatically; the library has no visibility into your API calls unless you feed it the usage data yourself.

Use Cases

  • AI Agents: Cache system prompts & knowledge bases
  • Document Analysis: Batch process documents with persistent context
  • Content Generation: Reusable brand guidelines or style sheets
  • Code Review: Consistent review criteria across a team

API

createCachedPrompt(content, options?)

Returns a cache-aware prompt wrapper.

Options:

  • maxTokens?: number – Warn if cached content exceeds this (default: 8000)
  • ttl?: number – Stored on the instance but not currently enforced anywhere in this library; it's advisory only (Claude's server-side cache TTL is controlled by Anthropic, not by this option).

Methods:

  • .asSystemPrompt() – Format for Claude's system parameter
  • .recordUsage(usage) – Feed in a response's usage object to update stats. Required before .hitRate() or .tokensSaved() will report anything meaningful.
  • .tokensSaved() – An estimate based on recorded cache-read vs. cache-creation token averages, not a value returned by the API.
  • .hitRate() – Cache hits / total recorded requests (0–1). Only reflects calls you've passed to .recordUsage().
  • .getStats() – Returns the raw counters (cacheCreationTokens, cacheReadTokens, totalHits, totalRequests).

Contributing

Issues and PRs welcome.

License

MIT


Sponsored by Ferrow


Part of the ferrow-toolkit collection · Sponsored by Ferrow