# Prompt Caching API Example: Cut Token Costs in Node.js

DevFeed: [Prompt Caching API Example: Cut Token Costs in Node.js](<https://devfeed.tech/articles/prompt-caching-api-example-cut-token-costs-in-node-js-93003.md>)

Original publisher: [Read original article](<https://www.sitepoint.com/prompt-caching-api-example/>)

Author: SitePoint Team

Published: 2026-10-21T15:42:19Z

Content type: tutorial

Language: en

Sources: [SitePoint](<https://devfeed.tech/sources/sitepoint.md>)

Topics: [prompt](<https://devfeed.tech/topics/prompt.md>), [Tutorial](<https://devfeed.tech/topics/tutorial.md>), [Enhancing AI model performance](<https://devfeed.tech/topics/enhancing-ai-model-performance.md>), [OpenAI](<https://devfeed.tech/topics/openai.md>), [Claude](<https://devfeed.tech/topics/claude.md>), [Prompt Engineering](<https://devfeed.tech/topics/prompt-engineering.md>), [API](<https://devfeed.tech/topics/api.md>), [cost-optimization](<https://devfeed.tech/topics/cost-optimization.md>)

Tags: [1-3](<https://devfeed.tech/tags/1-3.md>), [1-5](<https://devfeed.tech/tags/1-5.md>), [ai](<https://devfeed.tech/tags/ai.md>), [anthropic](<https://devfeed.tech/tags/anthropic.md>), [api](<https://devfeed.tech/tags/api.md>), [apis](<https://devfeed.tech/tags/apis.md>), [cache](<https://devfeed.tech/tags/cache.md>), [caching](<https://devfeed.tech/tags/caching.md>), [costs](<https://devfeed.tech/tags/costs.md>), [example](<https://devfeed.tech/tags/example.md>), [gemini](<https://devfeed.tech/tags/gemini.md>), [metrics](<https://devfeed.tech/tags/metrics.md>), [node](<https://devfeed.tech/tags/node.md>), [node-js](<https://devfeed.tech/tags/node-js.md>), [openai](<https://devfeed.tech/tags/openai.md>), [programming](<https://devfeed.tech/tags/programming.md>), [prompt](<https://devfeed.tech/tags/prompt.md>), [prompt-caching](<https://devfeed.tech/tags/prompt-caching.md>), [stable](<https://devfeed.tech/tags/stable.md>), [token](<https://devfeed.tech/tags/token.md>), [token-costs](<https://devfeed.tech/tags/token-costs.md>), [verify](<https://devfeed.tech/tags/verify.md>), [with](<https://devfeed.tech/tags/with.md>)

## AI overview

Prompt caching can reduce costs when repeated requests share an identical, sufficiently long prompt prefix, but OpenAI, Anthropic, and Gemini require different handling. OpenAI caches automatically for prefixes of at least 1,024 tokens and discounts cached input tokens by 50%. Anthropic requires a cache_control breakpoint, has a five-minute TTL, discounts cache reads by 90%, and charges 25% extra for cache creation. Gemini uses a separately created cachedContent resource with an explicit TTL, requires at least 32,768 tokens for explicit caching, and offers a 75% discount for Gemini 1.5 Pro. The TypeScript example wraps provider-specific builders and usage metrics in a shared request factory and logger. Keep static content byte-identical at the start of each prompt, append variable content afterward, and verify hits through each provider's usage fields. Gemini cache resources need to be reused and recreated after expiry; its in-memory example loses them on process restarts. Savings depend on prefix size and repeated use within the TTL.

## Source excerpt

Structure stable prompt prefixes across OpenAI, Anthropic, and Gemini, then verify cache hits with each provider's usage metrics in Node.js. Continue reading Prompt Caching API Example: Cut Token Costs in Node.js on SitePoint.