copilot-cli icon indicating copy to clipboard operation
copilot-cli copied to clipboard

Enhance the prompt caching for Claude Sonnet model to reduce latency and token costs

Open Qiuym9 opened this issue 5 months ago • 4 comments

Describe the feature or problem you'd like to solve

When using GitHub Copilot CLI with the Claude Sonnet model, there is no visible optimization for Anthropic's prompt caching feature. For long system prompts or repeated context (e.g., large codebases, long instruction blocks), each request re-processes the same tokens, leading to higher latency and unnecessary token usage.

Proposed solution

Leverage Anthropic's prompt caching API (cache_control breakpoints) for static portions of the prompt — such as system instructions, repo context, and tool definitions. This would:

  • Reduce time-to-first-token for follow-up turns in the same session
  • Lower API costs by reusing cached prefixes (cached tokens are ~90% cheaper)
  • Improve responsiveness for users working in large codebases

Example prompts or workflows

Leverage Anthropic's prompt caching API (cache_control breakpoints) for static portions of the prompt — such as system instructions, repo context, and tool definitions. Specifically:

  1. Cache TTL configuration: Allow users to configure the cache TTL via a settings option — choosing between the default 5-minute TTL or an extended 1-hour TTL (supported by Anthropic's API), suitable for long working sessions.
  2. Cache visibility CLI command: Add a command (e.g., copilot cache status) to display per-turn cache hit/miss stats using the usage fields already returned by Anthropic's API (cache_read_input_tokens, cache_creation_input_tokens), helping users understand caching efficiency and debug unexpected misses.

Benefits:

  • Reduce time-to-first-token for repeated context in long sessions
  • Lower API costs (cached tokens are ~90% cheaper)
  • Give power users transparency and control over caching behavior

Additional context

  1. A user runs copilot cache status after a multi-turn session and sees that 80% of system prompt tokens were served from cache, confirming cost savings.
  2. A user sets cache TTL to 1 hour in config (copilot config set cache-ttl 1h) to avoid cache expiry during a long debugging session on a large codebase.
  3. A developer asks repeated questions about the same large file — cache hits on the file context reduce response latency from ~3s to ~0.5s after the first turn.
  4. A user notices cache misses on every turn via copilot cache status and realizes their dynamic timestamp in the system prompt is breaking the cache prefix.
  5. A team configures 1h TTL in a shared .copilot/config to optimize CI/CD pipelines where the same repo context is queried repeatedly within an hour.

Qiuym9 avatar Jun 15 '26 10:06 Qiuym9

3808

Pada Sen, 15 Jun 2026, 18.02, Yumeng Qiu @.***> menulis:

Qiuym9 created an issue (github/copilot-cli#3808) https://github.com/github/copilot-cli/issues/3808 Describe the feature or problem you'd like to solve

When using GitHub Copilot CLI with the Claude Sonnet model, there is no visible optimization for Anthropic's prompt caching feature. For long system prompts or repeated context (e.g., large codebases, long instruction blocks), each request re-processes the same tokens, leading to higher latency and unnecessary token usage. Proposed solution

Leverage Anthropic's prompt caching API (cache_control breakpoints) for static portions of the prompt — such as system instructions, repo context, and tool definitions. This would:

  • Reduce time-to-first-token for follow-up turns in the same session
  • Lower API costs by reusing cached prefixes (cached tokens are ~90% cheaper)
  • Improve responsiveness for users working in large codebases

Example prompts or workflows

Leverage Anthropic's prompt caching API (cache_control breakpoints) for static portions of the prompt — such as system instructions, repo context, and tool definitions. Specifically:

  1. Cache TTL configuration: Allow users to configure the cache TTL via a settings option — choosing between the default 5-minute TTL or an extended 1-hour TTL (supported by Anthropic's API), suitable for long working sessions.
  2. Cache visibility CLI command: Add a command (e.g., copilot cache status) to display per-turn cache hit/miss stats using the usage fields already returned by Anthropic's API (cache_read_input_tokens, cache_creation_input_tokens), helping users understand caching efficiency and debug unexpected misses.

Benefits:

  • Reduce time-to-first-token for repeated context in long sessions
  • Lower API costs (cached tokens are ~90% cheaper)
  • Give power users transparency and control over caching behavior

Additional context

  1. A user runs copilot cache status after a multi-turn session and sees that 80% of system prompt tokens were served from cache, confirming cost savings.
  2. A user sets cache TTL to 1 hour in config (copilot config set cache-ttl 1h) to avoid cache expiry during a long debugging session on a large codebase.
  3. A developer asks repeated questions about the same large file — cache hits on the file context reduce response latency from ~3s to ~0.5s after the first turn.
  4. A user notices cache misses on every turn via copilot cache status and realizes their dynamic timestamp in the system prompt is breaking the cache prefix.
  5. A team configures 1h TTL in a shared .copilot/config to optimize CI/CD pipelines where the same repo context is queried repeatedly within an hour.

— Reply to this email directly, view it on GitHub https://github.com/github/copilot-cli/issues/3808?email_source=notifications&email_token=CFNECYWNHTXCNC57OCJ4P33477CRZA5CNFSL4Z3JMQ5C6L3HNF2C22DVMIXUS43TOVSS6NBWGY2DAMJXGE4DNJTSMVQXG33OVJZXKYTTMNZGSYTFMSSWK5TFNZ2KYZTPN52GK4S7MNWGSY3L, or unsubscribe https://github.com/notifications/unsubscribe-auth/CFNECYQ6JLEJQ647KSUQUDD477CRZAVCNFSNUABFKJSXA33TNF2G64TZHM2TQNJYGYYDMNRUHNEXG43VMU5TINRWGQYDCNZRHA3KC5QC . Triage notifications, keep track of coding agent tasks and review pull requests on the go with GitHub Mobile for iOS https://github.com/notifications/mobile/ios/CFNECYWKYUBVKHLMWMITSSL477CRZA5CNFSL4Z3JMQ5C6L3HNF2C22DVMIXUS43TOVSS6NBWGY2DAMJXGE4DNJTSMVQXG33OVJZXKYTTMNZGSYTFMSSWK5TFNZ2KUZTPN52GK4S7NFXXG and Android https://github.com/notifications/mobile/android/CFNECYRSTYSFV7ZZPSRYUZL477CRZA5CNFSL4Z3JMQ5C6L3HNF2C22DVMIXUS43TOVSS6NBWGY2DAMJXGE4DNJTSMVQXG33OVJZXKYTTMNZGSYTFMSSWK5TFNZ2K4ZTPN52GK4S7MFXGI4TPNFSA. Download it today! You are receiving this because you are subscribed to this thread.Message ID: @.***>

Following #4256, the configurable-TTL half of this issue is still open. Some measurements in case they help prioritize it.

Caveat first: this is an unattended agent workload, a long-lived Claude session that dispatches sub-agents and idles while CI runs. Sessions are driven through the Github Copilot App. But it's the case Anthropic names when explaining why the 1-hour tier exists (link):

If you have prompts that are used at a regular cadence (that is, system prompts that are used more frequently than every 5 minutes), continue to use the 5-minute cache, because this will continue to be refreshed at no additional charge.

The 1-hour cache is best used in the following scenarios:

  • When you have prompts that are likely used less frequently than 5 minutes, but more frequently than every hour. For example, when an >agentic side-agent will take longer than 5 minutes, or when storing a long chat conversation with a user and you generally expect that >user may not respond in the next 5 minutes.

  • When latency is important and your follow up prompts may be sent beyond 5 minutes.

  • When you want to improve your rate limit utilization, because cache hits are not deducted against your rate limit.

The ask: an opt-in 1-hour TTL on the Anthropic path, with 5 minutes staying the default. Automatic selection would help more users but is a real policy call. 1-hour writes cost 2× base, so it shouldn't be the interactive default.

91% of the affected spend falls inside one hour

Cache-write spend that follows a cache expiry, split by how long the session idled. The shares and session counts are real; the dollar column is hypothetical, normalized to $1,000 of that spend so the shape is easier to read:

idle before the rewrite sessions share per $1,000 (hypothetical)
5–60 min (1 h would cover) 285 91% $910
beyond 60 min (wouldn't help) 80 9% $90

Real totals over the month are several times that scale, and I'm happy to share them privately.

Waits past an hour are mostly overnight and should go cold. Everything else is a prefix that was hot minutes earlier, re-uploaded at 12.5× the read price ($6.25/M vs $0.50/M on Opus).

What that $1,000 becomes with a 1-hour TTL

Same hypothetical $1,000, same token volume, priced off the public rates (read 0.1× base, 5-min write 1.25×, 1-hour write 2×):

  • the $910 of rewrites inside the hour turn into reads: ~$73 (reads are 8% of a write)
  • the $90 beyond an hour is still a cold rewrite, now at the 2× tier: ~$144
  • total ~$220 instead of $1,000

One thing that number doesn't include: the writes that established those caches also move to the 2× tier, so they get 1.6× more expensive. That spend isn't in this slice, so the real saving is smaller than 78%.

The 5-minute edge is sharp

Claude Opus calls binned by idle time between requests (main-context calls, 2026-07-10 → 08-10), counting turns where the cache write exceeds half the prompt, i.e. the prefix was rewritten rather than extended:

idle sessions prefix rewritten
240 s 139 3.5%
270 s 102 11.6%
300 s 103 32.0%
330 s 62 100.0%
420 s 56 100.0%

55–199 distinct sessions per bin, so it isn't a few pathological runs.

Method (gaps are request-start to request-start; threshold sensitivity)
  • created_at in the CLI's local session store is completion time, so gaps are reconstructed start-to-start using duration_ms. This matters because the TTL is anchored at request start: "Time spent generating a response counts against the lifetime."
  • "Rewritten" is a ratio (cache_write / input_tokens > 0.5) rather than a fixed token threshold, which would under-count rewrites in smaller contexts. Thresholds from 0.1 to 0.9 all preserve the separation.
  • Snapshot frozen at a fixed row id so the numbers are reproducible. Costs are list-price-equivalent from the CLI's own recorded per-token rates, not an invoice.

Today the lifetime isn't expressible

In the published SDK types (sdk/index.d.ts):

declare type CacheControlCheckpoint = {
  type: "ephemeral";
};

referenced as copilot_cache_control?: CacheControlCheckpoint, with no lifetime on it. Our session data agrees the 1-hour tier has never been exercised: every Claude model shows exactly one cache-write price, always 1.25× base, and the 2× rate never appears.

I'm not going to guess what enabling it costs on your side: proxy validation, billing, and multi-provider behavior aren't visible from the client types.

Happy to gather more data if needed

joheredi avatar Aug 10 '26 19:08 joheredi

+1 for the configurable 1h TTL. Measurements from a month of interactive use (Copilot CLI, Claude Opus/Sonnet), in case they help prioritise:

  • The Copilot backend already honours 1h TTL. With a third-party client on the same Copilot account sending cache_control: {type: "ephemeral", ttl: "1h"}, a cache read succeeded on a follow-up request 6.5 minutes later. The same test with the default TTL had to rewrite the cache.
  • Billing. An isolated test of about 4.5M tokens written to the 1h cache on Sonnet 5.5 was billed at about 2× the input price (~$4/M), against $2.50/M for 5-minute cache writes.
  • Simulation on my own history. I replayed 30 days of local CLI usage: 7.5k Claude requests, ~57k AI credits, 316 gaps of 5–60 minutes between turns. With a 1h TTL those gaps become cache reads instead of full rewrites. Net result: about 8% fewer AI credits, even after paying the 2× write price. It saved money on 18 of 23 active days; the remaining days had almost no pauses.
  • The CLI currently reports cacheTtlSeconds: 300 in session.usage_checkpoint with no way to change it.

Suggested: a setting or environment variable (e.g. cacheRetention: "5m" | "1h" | "auto"). "auto" could switch to 1h once the context is large and turns are slow. Showing cache expiry in the UI would also help.

ntvf avatar Oct 12 '26 11:10 ntvf

+1 for an opt-in 1-hour prompt-cache lifetime for Claude models in Copilot CLI.

Please expose a setting or environment variable that lets users choose between 5m and 1h, keeping five minutes as the default.

The use case is long coding sessions where reviewing changes, running tests, or stepping away creates gaps longer than five minutes between requests. I’d like the option to pay the higher initial cache-write cost rather than potentially rewrite a large cached prefix after each gap.

Please also display the effective cache lifetime alongside cache-read/write usage so users can verify that their selection is being applied.

Is there a remaining CLI, backend, or billing limitation preventing this from being exposed?

masonpetrosky avatar Oct 14 '26 18:10 masonpetrosky