Safa API · aisafa.xyz

SillyTavern Burning Through Your Claude API Budget? Prompt Caching Can Cut It 80% (2026 Guide)

发布于 2026-07-15 · Safa API

SillyTavern resends your full character card, world info, and growing chat history with every single message, so a 20-turn roleplay session can cost 10-20x more in input tokens than the first message alone. Claude's prompt caching feature lets the API reuse that unchanged prefix instead of reprocessing it, cutting input costs by roughly 70-90% on long sessions once it's configured correctly.

Why SillyTavern Token Costs Spiral Out of Control

Unlike a stateless chatbot, SillyTavern is built around persistent context: character description, personality, example dialogues, world info entries, author's notes, and the full message log all get concatenated into one prompt and sent again on every reply. If your character card plus lorebook is 3,000 tokens and your chat history grows to 8,000 tokens by message 30, you're paying full input-token price for those same 11,000 tokens on every single turn, even though almost none of it changed since the previous message.

For a busy roleplay session running Claude Opus or Sonnet at standard rates, that repeated re-processing is usually the single biggest line item on the bill, far more than the actual new reply text. This is exactly the workload prompt caching was designed for.

How Claude Prompt Caching Actually Works

Anthropic's prompt caching lets you mark a stable prefix of your prompt (system instructions, character card, lorebook, early chat history) as cacheable. The first request writes that prefix to a short-lived cache; every subsequent request that reuses the identical prefix reads from the cache instead of paying full input-token price for it. Cached reads are billed at a small fraction of the normal input rate, while only the new turn (and anything genuinely new) is billed normally. The cache typically lives for a few minutes, which is more than enough for the back-and-forth pace of an active chat.

The catch: caching only works if the cached portion of the prompt stays byte-for-byte identical between calls. If your frontend resends the character card and lorebook in the exact same position every time, which SillyTavern does by default, you get a very high cache-hit rate for free. If a proxy or client re-orders fields, injects timestamps, or shuffles world-info entries dynamically, the cache breaks on every call and you get none of the savings.

Setting Up Prompt Caching for SillyTavern

Getting caching to actually trigger comes down to two things: using an API endpoint/model that supports it, and keeping your prompt structure stable.

  1. Chat Completion source: In SillyTavern, go to the API Connections panel and pick Chat Completion → Custom (OpenAI-compatible) so you can point it at any Claude-capable relay.
  2. Keep your character card and lorebook first: Put static content (persona, character card, world info) at the very start of the context and avoid injecting variable content (current date/time, random author's notes) above it, that variable content should go near the end, right before the newest message.
  3. Use a model/provider that actually caches: Not every relay passes through Anthropic's cache-control headers correctly. Confirm your provider supports and returns cache read/write token counts in the response, otherwise you're paying full price with no way to tell.
  4. Watch your context length setting: Trimming ancient chat history is fine, but don't reorder or reformat the surviving messages between calls, any change to the cached prefix invalidates it.
{
  "chat_completion_source": "custom",
  "custom_url": "https://api.aisafa.xyz/v1",
  "custom_model": "claude-sonnet-5-20260514",
  "api_key": "sk-your-key",
  "prompt_order": ["system", "character_card", "world_info", "chat_history", "user_input"]
}

Real Cost Math: Before and After Caching

Take a typical roleplay session: 3,000-token character card + lorebook, growing chat history, 40 turns per session.

ScenarioInput tokens/turn (avg)Effective cost vs. no caching
No caching~9,000100%
Caching, stable prefix~9,000 (but ~7,500 served from cache)~15-25%
Caching + trimmed old history~6,000 (mostly cached)~10-20%

In practice that means a session that would have cost you $2-3 in input tokens on a long-running Opus roleplay can drop to $0.30-0.60 once caching is actually hitting, the same reduction users report on other long-context, repeated-prefix workloads like coding agents.

A Cheaper, No-Fuss Way to Run This Setup

If you'd rather not fight with API key whitelisting, region blocks, or a US billing address just to plug Claude into SillyTavern, an OpenAI-compatible relay like Safa API is worth pointing your Custom Chat Completion endpoint at. It supports Claude's prompt caching end-to-end (so the setup above works exactly as described), prices Claude/GPT/Gemini access noticeably lower than official rates, and doesn't require a credit card at all, sign-up and top-ups work with Alipay, which matters if you're outside the US or just don't want to hand over card details for a hobby project. One API key and one base URL also let you swap between Claude, GPT, and Gemini models in SillyTavern's model dropdown without juggling three separate accounts and three separate bills.

常见问题

Does prompt caching change how SillyTavern responds?

No. Caching only affects billing and latency for the repeated prefix, the model still sees the exact same prompt and generates the same quality of response. It's purely a cost optimization, not a behavior change.

Why isn't my cache hitting even though I set it up?

The most common cause is a dynamic element (timestamp, random greeting variation, reordered lorebook entries) sitting inside what should be the stable prefix. Check your provider's response for cache read/write token counts; if cache_read is always 0, something upstream of the model is still varying that prefix.

Does prompt caching work with Gemini and GPT too, not just Claude?

Gemini and GPT have their own equivalent caching mechanisms with different rules and discount rates. If you're routing multiple model families through one relay, confirm each provider's caching is actually enabled end-to-end rather than assuming it carries over automatically.

立即开始使用 Safa API API 中转

官方直连 · 一个接口接入 Claude / GPT / Gemini · 7×24 稳定

免费注册试用 →