Passer au contenu principal
CodeRelayHub

← Retour au digest d’ingénierie

Cache Writes vs. Reads: Follow the Token Bill

Treat prompt caching as an investment with a measurable payback period, not a checkbox labeled cheaper. Start by separating the cost of creating reusable context from the cost of consuming it. Then check whether your workload actually returns often enough to recover that investment.

1. Understand what gets reused

A prompt cache reuses previously processed input context, rather than returning a saved answer. A cache write stores an eligible prompt prefix; a cache read reuses that prefix on a later request. The model still processes new content and generates a response. Anthropic documents this as checking for a cached prefix before processing the remaining input. ([platform.claude.com](https://platform.claude.com/docs/en/build-with-claude/prompt-caching))

For a coding assistant, consider separating repository instructions and stable reference material from the current task. Treat those as different budgeting categories: reusable context, changing input, and generated output.

2. Read the actual price categories

For a concrete reference, Anthropic lists Claude Haiku 4.5 at $1 per million ordinary input tokens, $1.25 per million five-minute cache-write tokens, $2 per million one-hour cache-write tokens, and $0.10 per million cache-read tokens. Output costs $5 per million tokens. These are published direct-provider rates, not a quote for CodeRelay Hub. Verify the applicable hub rates before estimating production spend. ([platform.claude.com](https://platform.claude.com/docs/en/about-claude/pricing))

A write premium and a read discount describe different operations. Do not apply the read rate to every input token simply because a request uses caching. Budget each reported category separately.

3. Calculate when the write pays back

Using those published rates, consider an illustrative 10,000-token prefix. Ordinary processing costs $0.01 per request. A five-minute cache write costs $0.0125, and each subsequent read costs $0.001. One write followed by one read therefore costs $0.0135, compared with $0.02 for processing the prefix twice without caching. These are calculated prefix costs, not measured application results. ([platform.claude.com](https://platform.claude.com/docs/en/about-claude/pricing))

For your own rate card, let B be ordinary input cost for the prefix, W its write cost, R its read cost, and N the number of later reads. Caching wins when W + N × R is less than (N + 1) × B. Exclude identical output and changing-input costs from this comparison, then include them again in the full request budget.

4. Put stable content before changing content

OpenAI documents exact prefix matching: reusable material belongs at the beginning, with request-specific content afterward. Changing an early instruction can prevent reuse of the material following it. ([developers.openai.com](https://developers.openai.com/api/docs/guides/prompt-caching))

  • Keep stable instructions and examples together.
  • Use deterministic ordering when assembling reusable context.
  • Place timestamps and request identifiers outside the stable prefix.
  • Version prompt templates so billing changes can be traced to deployments.

Make these application-level conventions, rather than relying on every caller to assemble prompts consistently.

5. Match expiration to real traffic

Anthropic offers five-minute and one-hour cache lifetimes, with the longer lifetime carrying a higher write price. Successful reuse refreshes the lifetime. That makes request spacing relevant: a continuously reused prefix and an intermittently accessed prefix have different retention needs. ([platform.claude.com](https://platform.claude.com/docs/en/build-with-claude/prompt-caching))

Before choosing a longer lifetime, inspect the gaps between requests sharing a prefix. Estimate both options using observed reuse, including repeated writes after expiration. Avoid sending artificial keepalive requests unless their complete cost has a justified benefit.

6. Verify the integration and the bill

When integrating through CodeRelay Hub, test the complete client-to-endpoint path. Do not assume that an accepted request proves caching worked. Record the selected model, prompt-template version, available cache usage counters, total charge, and latency.

Run a cold request, repeat its prefix, and then change an early prefix element. Compare the reported categories rather than judging by response speed alone. For production reporting, prioritize cost per completed task over an isolated cache-hit percentage. A useful cache is one whose reads repay its writes without encouraging unnecessary context.

Open a free workspace to start checking your cache costs.

Testez sur une vraie tâche : ouvrez un espace de travail et utilisez le crédit d’essai.

S'inscrire