Menu

Prompt Caching: Your Prompt's Prefix Is a Cache Key
📰

Prompt Caching: Your Prompt's Prefix Is a Cache Key

DEV Community: node·Danny Holloran·11 days ago
#7iLMnAlE
#devtonodejs#devto#dev#prompt#request
Reading 0:00
15s threshold

Look at the input token count on a typical LLM feature and most of it is the same on every request. The system prompt, the tool definitions, the twelve few-shot examples, the product docs you stuffed in for grounding: thousands of tokens, identical every time, paid for every time. The user's actual question is a rounding error at the end.

Prompt caching exists for exactly this. The provider saves the model's processed state for the start of your prompt, and the next request that begins with the same tokens skips that work. It's cheaper and faster. The catch is that it matches on an exact prefix, which means the order you assemble your prompt in suddenly matters a lot more than it used to.

How the prefix match works

The cache key is the prompt itself, read from the beginning. If request two starts with the exact same tokens as request one, up to a cache point, it can reuse request one's work.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.