Your fallback chain makes sure every hiker gets an answer, but you pay full price for each answer’s input tokens. In Price Each Request From Its Usage, /ask got 0 cached tokens across a day of 200 questions.
In this lesson, you’ll order the prompt so the provider’s prompt cache reuses most of it, then confirm the hits and the cost from usage fields you already record. Start with why /ask missed.
Find Out Why /ask Missed the Cache
In Price Each Request From Its Usage, /hut-info sent a long hut guide ahead of each question. OpenAI cached it on its own, and Anthropic did so once you added a breakpoint. The tokens() function mapped both providers’ usage fields into one cached count.
A prompt cache compares a request with earlier ones from its first token and stops at the first difference. Only that shared prefix is cheaper, so the stable parts of a prompt must come first. /ask missed for two reasons:
- Retrieval put different reports in every prompt, so questions shared only the tools and instructions.
- Those came to under 300 tokens, below OpenAI’s 1,024-token minimum for a cached prefix.
The newest reports for all 22 places on the trail fit in about 1,400 tokens. So /ask now sends them all with every hiker’s question as the current reports block. That costs more input tokens, but it ends the retrieval misses you debugged in Observing AI Features.
First, record a baseline as in Price Each Request From Its Usage. Set SAMPLE_RATE in trace.js to 1 and rename today’s trace file. Run npm start | tee server.log and replay a day’s 200 questions with npm run traffic -- --month=july. Then run npm run cost -- traces/<today>.jsonl server.log and note the cost of /ask per thousand questions as your baseline. Rename the trace traces/retrieval.jsonl.
Put the Stable Part First
Send the tools, the instructions, and the reports block first. The parts that change—the date and the question—go last. Both providers place tool definitions ahead of the instructions, so the prefix starts with the tool definitions: