# Caching

> Pay less for repeated work: the provider's prompt cache, and the gateway's cache of identical calls.

Applies to: the Sypnia engine (open source, self-hosted) and Sypnia Cloud (paid service).  
Source: https://ai.docs.sypnia.com/usage/caching/

For agents: Two caches. (1) Provider prompt caching: automatic where the model supports it; long stable prefixes (route instructions first, then tools, then messages) are read again at the model's cached price; usage shows cached_input_tokens. (2) Gateway response cache: off by default; route cache {ttl: 1 m–24 h}; key = route version + full request (messages, params, schema) + end user unless cache.shared: true; hit costs nothing and is marked sypnia.cache: hit and a span attribute; only for non-streaming or replayed streaming; never for tools that change things; stored encrypted for the ttl only (an exception to 'bodies not stored', documented); purge with ai cache purge --task.

## The provider's cache

Many models charge less for the part of a request they read again a few minutes later. The gateway orders every request so the stable part comes first (the route's instructions, then the tools, then the messages), so long instructions are read at the cheaper price on every call after the first. Usage shows them as `cached_input_tokens`.

There is nothing to turn on.

## The gateway's cache of identical calls

Off by default. Turn it on for a route whose answer can be reused:

`sypnia.toml`

```toml
[ai.tasks.bread-of-the-day]
cache = { ttl = "6h" }
```

|                                  |                                                                                                                                                |
| -------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------- |
| **What counts as the same call** | the same route version, messages, options and schema, and the same person of your app; `cache: { shared: true }` shares answers between people |
| **A hit**                        | costs nothing; the response says `sypnia.cache: "hit"` and the trace marks it                                                                  |
| **How long**                     | from 1 minute to 24 hours                                                                                                                      |
| **Where**                        | the answer is kept encrypted for that time only, then deleted: the one case where Sypnia AI keeps an answer without `store`                    |
| **Never for**                    | calls with tools                                                                                                                               |

`npx @sypnia/ai cache purge --task bread-of-the-day` empties a route's cache, for example after the menu changes.
