# Prompt caching

Providers cache the beginning of a prompt so repeated turns are cheaper and
faster. Most providers do it on their own. A few need an explicit marker
(`cache_control`): Anthropic, Alibaba/Qwen and Gemini. ApiX adds that marker for
you on those models.

### Automatic

When the model needs explicit breakpoints and your request has none of its own,
ApiX marks two places: the end of the system prompt and the last text block.
They are the parts that repeat in a conversation, so the next turn reads them
from cache.

The minimum length depends on the model (roughly 1,024–4,096 tokens depending on
the family; the gateway uses each provider's documented minimum). Below that
nothing is cached, and a marker there would only pay for the write.

On providers that cache automatically, ApiX does **not** touch the body: adding
a marker there does nothing and would break the provider's response cache.

### Turn it off

```json
{"prompt_cache_options": {"mode": "off"}}
```

Also accepted: `{"cache_control": {"type": "none"}}`, `{"prompt_cache_options":
{"disabled": true}}`, `{"apix_prompt_cache": "off"}`, or the server-wide
`APIX_PROMPT_CACHE=off`.

Your own breakpoints always win: if you send `cache_control` anywhere, ApiX does
not add or move anything.

### TTL

By default the provider decides (usually 5 minutes). A longer TTL is opt-in:

```bash
APIX_CACHE_TTL=1h
```

`1h` costs more to write (Anthropic charges 2x instead of 1.25x), so use it for
long sessions, not one-shot requests. A model can also pin its own TTL in the
catalog.

### What it saves

Cache reads are billed at a fraction of normal input. You can see them in
`usage`: `prompt_tokens_details.cached_tokens` (OpenAI style),
`cache_read_input_tokens` and `cache_creation_input_tokens` (Anthropic style).
`GET /apix/usage` totals them as `cache_read_tokens`, `cache_write_tokens` and
`cache_saved_usd`.

### Response cache

For models that declare a response cache, ApiX asks the provider to cache the
whole reply. Sending the exact same request again within the TTL returns the
stored response instead of running the model.
