# PCACHE.SET

> Store or replace a cached LLM response for an exact prompt, with optional TTL, metadata and token usage.

URL: https://docs.hotkv.com/commands/pcache-set/

```
PCACHE.SET namespace prompt response [TTL seconds] [META metadata] [MODEL model [INPUT n] [OUTPUT n] [CACHEREAD n] [CACHEWRITE5M n] [CACHEWRITE1H n]]
```

- Group: Prompt cache
- Complexity: O(1)
- HotKV Cloud: Performance and Scale plans
- On-Premise: requires the prompt-cache license feature

The prompt cache is an exact-match cache for LLM prompt and response pairs, organised in named namespaces. `PCACHE.SET` stores `response` under the exact `prompt` string in
`namespace`. If the prompt is already cached, its response, metadata, usage
and expiry are replaced. The namespace must exist (see [PCACHE.CREATE](https://docs.hotkv.com/commands/pcache-create/index.md)).

## Options

| Option | Effect |
| --- | --- |
| `TTL seconds` | Time to live for this entry. `0` or omitted uses the namespace's default TTL. |
| `META metadata` | Any string you want to keep with the entry, such as JSON. [PCACHE.GETEX](https://docs.hotkv.com/commands/pcache-getex/index.md) returns it. |
| `MODEL model` | The model that produced the response. Required when you pass any token count below. |
| `INPUT n` | Uncached input tokens the original call used. |
| `OUTPUT n` | Output tokens the original call used. |
| `CACHEREAD n` | Provider cache-read tokens the original call used. |
| `CACHEWRITE5M n` | Tokens the original call wrote to a 5-minute provider cache. |
| `CACHEWRITE1H n` | Tokens the original call wrote to a 1-hour provider cache. |

Options come in name and value pairs after `response`, in any order.

The usage options record what the original, uncached model call consumed.
Every later hit on the entry adds those tokens to the namespace's
`usage_saved_*` counters, and when the model has a price (built in, or set
with [LLMSTATS.PRICE](https://docs.hotkv.com/commands/llmstats-price/index.md)) it also adds the cost those
tokens would have had to `usage_saved_cost_usd`. Token counts are
non-negative integers. Passing a token count without `MODEL` is rejected.

## Return value

| Case | RESP2 | RESP3 |
| --- | --- | --- |
| Entry stored | Simple string `OK` | Same |

Errors:

- `ERR namespace not found` if the namespace does not exist.
- `ERR syntax error` if an option has no value.
- `ERR unknown option` for any other option name.
- `ERR invalid TTL` if `TTL` is not a non-negative integer.
- `ERR usage token counts (INPUT, OUTPUT, CACHEREAD, CACHEWRITE5M, CACHEWRITE1H) need MODEL` if you pass a token count without `MODEL`.
- `ERR invalid INPUT`, `ERR invalid OUTPUT`, `ERR invalid CACHEREAD`, `ERR invalid CACHEWRITE5M` or `ERR invalid CACHEWRITE1H` if a token count is not a non-negative integer.
- `ERR wrong number of arguments for 'pcache.set' command` if the argument count is wrong.
- `NOLICENSE prompt-cache requires a HotKV license. Contact sales@hotkv.com` if your license does not include the `prompt-cache` feature.

## Examples

```
127.0.0.1:6379> PCACHE.SET llm_prompts "Translate: Hello" "Bonjour"
OK
127.0.0.1:6379> PCACHE.SET llm_prompts "Translate: Hello" "Bonjour" TTL 600 META "{\"lang\":\"fr\"}"
OK
127.0.0.1:6379> PCACHE.SET llm_prompts "Translate: Hello" "Bonjour" MODEL gpt-4o INPUT 20 OUTPUT 3
OK
127.0.0.1:6379> PCACHE.SET llm_prompts "Translate: Hello" "Bonjour" INPUT 20
(error) ERR usage token counts (INPUT, OUTPUT, CACHEREAD, CACHEWRITE5M, CACHEWRITE1H) need MODEL
```

## Compatibility

HotKV-only command. Redis and Valkey have no equivalent.

## See also

[PCACHE.CREATE](https://docs.hotkv.com/commands/pcache-create/index.md), [PCACHE.GET](https://docs.hotkv.com/commands/pcache-get/index.md), [PCACHE.GETEX](https://docs.hotkv.com/commands/pcache-getex/index.md), [PCACHE.DEL](https://docs.hotkv.com/commands/pcache-del/index.md), [PCACHE.STATS](https://docs.hotkv.com/commands/pcache-stats/index.md), [PCACHE.FLUSH](https://docs.hotkv.com/commands/pcache-flush/index.md), [PCACHE.DROP](https://docs.hotkv.com/commands/pcache-drop/index.md), [PCACHE.LIST](https://docs.hotkv.com/commands/pcache-list/index.md), [PCACHE.CONFIG](https://docs.hotkv.com/commands/pcache-config/index.md), [AI features guide](https://docs.hotkv.com/ai-features/index.md)