> ## Documentation Index
> Fetch the complete documentation index at: https://docs.venice.ai/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> The Venice API is OpenAI-compatible: use any OpenAI SDK with the base URL `https://api.venice.ai/api/v1` and a Venice API key, or pay per request from a USDC wallet through x402 with no account.
> Read model IDs from `GET /models` or `GET /models/traits` at runtime instead of hardcoding them from memory or from these pages. Models are added and retired often.
> Every model's price is in https://docs.venice.ai/pricing.md. Quote video and audio jobs with `POST /video/quote` or `POST /audio/quote` before you queue them.
> Search these docs from an MCP client at https://docs.venice.ai/mcp. The full agent guide is https://docs.venice.ai/agents.md.

# Prompt Caching

> Cut Venice API cost and latency by caching repeated prompt content across requests.

Prompt caching stores processed input tokens so subsequent requests with identical prefixes can reuse them instead of reprocessing. This reduces latency on long prompts and cuts cost: cache reads are billed at a fraction of the input price, often 10% or less.

Venice handles caching automatically for supported models, but understanding how each provider implements caching helps you maximize cache hit rates and minimize costs.

## How Caching Works

Caching operates on **prefix matching**: the system stores processed tokens and reuses them when subsequent requests start with the same content.

Consider a chatbot with a 2,000-token system prompt:

**1. Request 1**

System prompt (2,000 tokens) + user message (50 tokens)

**Processed**: 2,050 tokens · **From cache**: 0 tokens

Prefix written to cache.

**2. Request 2**

System prompt (2,000 tokens) + user message (80 tokens)

**Processed**: 80 tokens · **From cache**: 2,000 tokens

**3. Request 3**

System prompt (2,000 tokens) + user message (120 tokens)

**Processed**: 120 tokens · **From cache**: 2,000 tokens

**Total without caching**: 2,050 + 2,080 + 2,120 = 6,250 tokens at full price

**Total with caching**: 2,050 + 80 + 120 = 2,250 tokens at full price, 4,000 tokens at discounted rate

> [!WARNING]
>
> Caching only works on the **prefix**. Any change to the beginning of your prompt invalidates the cache for everything that follows. Always put static content (system prompt, documents, examples) before dynamic content (user messages).

## Pricing

Most text models support caching. Each model's cache read and cache write prices are on its page under [Text models](https://docs.venice.ai/models/text.md), and in `model_spec.pricing.cache_input` and `model_spec.pricing.cache_write` in the [List Models](https://docs.venice.ai/api-reference/endpoint/models/list.md) response.

> [!NOTE]
>
> Some models, including Claude and GPT-5.6 and later, charge more for cache writes than for regular input. The first request that fills the cache costs more, and later cache hits are billed at the cheaper cache read price.

Models that price [one-hour cache writes](#one-hour-cache-lifetime) separately also list `model_spec.pricing.cache_write_1h`. On those models, `cache_write` covers five-minute writes and `cache_write_1h` covers one-hour writes.

## Provider-Specific Behavior

Venice normalizes caching across providers. For most models, caching is automatic. Just send your requests and check the response for cache statistics. A few providers only cache content marked with `cache_control`, and Venice adds those markers for you.

Caching behavior is ultimately controlled by each provider and may change, so check provider docs for the latest details.

| Models | Who places cache markers | Cache writes |
| - | - | - |
| Claude | Venice, automatically. You can add your own. | 1.25x input for five-minute writes, 2x for one-hour writes |
| Qwen 3.6 Plus, Qwen 3.7 Plus, Qwen 3.7 Max, Qwen 3.8 Max, Qwen 4 Max and Gemini 3.x | Venice, automatically | At the model's `cache_write` price when it lists one |
| GPT-5.6 and later, including GPT-6 | OpenAI, at the end of the latest message | 1.25x input. Entries last at least 30 minutes. |
| Earlier GPT models | None needed | Not billed |
| All other models | None needed. The provider caches automatically. | Not billed unless the model lists `cache_write` |

Cache read prices vary by model, from a few percent of the input price to about half of it. Check `model_spec.pricing.cache_input` for the model you use.

### Claude (Anthropic)

Claude requires explicit cache breakpoints at the protocol level. Venice handles this automatically:

* **System prompt**: Venice marks the end of the system prompt. Leading `developer` messages are sent to Claude as part of the system prompt, so they are cached with it.
* **Conversation history**: Venice marks your previous assistant reply, so everything before the newest user message is read from cache.
* **Tool loops**: when a request ends with tool results, Venice marks the newest tool result instead. Each round is written to the cache once and read on the next round.

This means your conversation history is read from cache, and only the latest turn is processed as new input:

| Turn | Prompt Tokens | Cache Read | Cache Write | Savings |
| - | - | - | - | - |
| 1 | 10,979 | 0 | 10,938 | First write |
| 2 | 11,031 | 10,938 | 31 | 99.7% cached |
| 3 | 11,062 | 10,969 | 52 | 99.5% cached |

**Additional details:**

* **Up to 4 breakpoints per request**: The system uses the longest matching prefix
* **Cache key is byte-exact**: Whitespace changes, different image encodings, or reordered tools break cache hits
* **Cache writes cost more than input**: 1.25x the input price for five-minute writes and 2x for one-hour writes. Cache reads cost 10% of the input price on most Claude models, 5% on Claude Opus 5.5 and Sonnet 5.5, and 2.5% on Claude Fable 5.1 and 5.2.

#### Manual cache control

For special cases like caching a large document on the first turn, you can add explicit breakpoints:

```json theme={"system"}
{
  "messages": [
    {
      "role": "system",
      "content": [{
        "type": "text",
        "text": "You are a legal assistant...",
        "cache_control": { "type": "ephemeral" }
      }]
    },
    {
      "role": "user", 
      "content": [{
        "type": "text",
        "text": "[Long contract document...]",
        "cache_control": { "type": "ephemeral" }
      }]
    },
    { "role": "assistant", "content": "I've reviewed the contract." },
    { "role": "user", "content": "What are the termination clauses?" }
  ]
}
```

This ensures both the system prompt and document are cached from the first request. For typical conversations, you don't need manual markers.

#### How your markers combine with Venice's

* A request can have at most four cache breakpoints. If you send more, Venice keeps the last four, which cover the longest prefixes.
* Venice adds its automatic markers only in the slots you leave free, and doesn't add one to a system prompt you already marked.
* Venice keeps `cache_control` on system and user content blocks, on tool messages (on the message itself or inside its content), and on Anthropic-style `tool_result` blocks.
* Claude accepts `ttl` values of `"5m"` (the default) and `"1h"`, and requires one-hour blocks to come before five-minute blocks, counting from the system prompt. Venice's automatic markers follow your TTLs, and Venice drops markers on tool results that break either rule. Other blocks that break them make Claude reject the request.

#### One-hour cache lifetime

By default, Claude cache entries last five minutes. To keep a block cached for an hour, set `ttl` to `"1h"` in its `cache_control` object:

```json theme={"system"}
{
  "type": "text",
  "text": "[Long contract document...]",
  "cache_control": { "type": "ephemeral", "ttl": "1h" }
}
```

`ttl` accepts `"5m"` (the default) or `"1h"`. One-hour writes are billed at the model's `cache_write_1h` price when its pricing lists one (Anthropic charges 2x the input price). The response reports them in `prompt_tokens_details.cache_creation_1h_input_tokens`.

### Qwen and Gemini 3.x

Venice marks the end of the system prompt and the message before your newest user message, usually your previous assistant reply. On Qwen models hosted by Alibaba, it marks the newest tool result in tool loops. Markers you add inside system, user or assistant message content are passed through; markers on tool messages are not.

### All Other Models

Caching is **automatic**, and you don't need `cache_control`. OpenAI's GPT-5.6 and later models place their own breakpoint at the end of the latest message and keep cache entries for at least 30 minutes. Keep prompts above the [minimum length](#meet-minimum-token-thresholds) and use `prompt_cache_key` for consistent routing.

## Request Parameters

| Parameter | Type | Models | Description |
| - | - | - | - |
| `prompt_cache_key` | string | All | Routing hint for cache affinity. Venice keeps requests with the same key on the same provider and passes the key to providers that use it for cache routing. |
| `prompt_cache_retention` | string | Earlier GPT models | `"extended"` or `"24h"` keeps cached prefixes for up to 24 hours on models that support extended retention, such as GPT-5.2, GPT-5.4 and GPT-5.5. |
| `cache_control` | object | Claude | Marks content blocks for caching. See [Claude](#claude-anthropic). |

### `prompt_cache_key`

For conversations or agentic workflows, use a consistent `prompt_cache_key` to improve cache hit rates:

```json theme={"system"}
{
  "model": "claude-sonnet-5-5",
  "prompt_cache_key": "session-abc-123",
  "messages": [...]
}
```

Venice keeps requests that share a key on the same provider and passes the key to providers that use it to route to a warm cache, such as OpenAI. If you don't send one, Venice routes by your first user message and sends providers a key based on your account, so set your own when you run many conversations in parallel or rewrite earlier messages between turns. Use a session ID or conversation ID as the key.

## Response Fields

The response `usage` object includes cache statistics:

```json theme={"system"}
{
  "usage": {
    "prompt_tokens": 5500,
    "completion_tokens": 200,
    "total_tokens": 5700,
    "prompt_tokens_details": {
      "cached_tokens": 5000
    }
  }
}
```

| Field | Description |
| - | - |
| `prompt_tokens` | Total input tokens, including tokens read from and written to the cache |
| `prompt_tokens_details.cached_tokens` | Tokens served from cache (billed at discounted rate) |
| `prompt_tokens_details.cache_creation_input_tokens` | Tokens written to cache, billed at the model's cache write price (higher than input on Claude and GPT-5.6 and later) |
| `prompt_tokens_details.cache_write_tokens` | Same value as `cache_creation_input_tokens`, under the field name OpenRouter-compatible clients read |
| `prompt_tokens_details.cache_creation_5m_input_tokens` | Cache write tokens stored with the five-minute TTL. Present when the provider reports writes by TTL. |
| `prompt_tokens_details.cache_creation_1h_input_tokens` | Cache write tokens stored with the one-hour TTL (`"ttl": "1h"`). Present when the provider reports writes by TTL. |

The cache write fields appear only when the request wrote to the cache.

**Billing breakdown** (using Claude Sonnet 5.5 as example):

* 5,000 cached tokens × \$0.10/1M = \$0.0005
* 500 uncached tokens × \$2.00/1M = \$0.001
* Total: \$0.0015 (vs \$0.011 without caching, 86% savings)

## Best Practices

### Structure prompts for caching

Place static content at the beginning, dynamic content at the end.

**Good structure**

| Position | Content | Cached? |
| - | - | - |
| 1 | System instructions | Yes |
| 2 | Reference documents | Yes |
| 3 | Few-shot examples | Yes |
| 4 | User query | No |

**Bad structure**

| Position | Content | Cached? |
| - | - | - |
| 1 | Current timestamp | No (invalidates everything after) |
| 2 | System instructions | No |
| 3 | User query | No |

### Keep prefixes byte-identical

Cache keys are computed from exact byte sequences. Even trivial differences break cache hits:

* Different whitespace or newlines
* Timestamps or request IDs in prompts
* Randomized few-shot example ordering
* Different formatting of the same content

### Meet minimum token thresholds

Each model has a minimum prefix length below which nothing is cached, and no error is returned. On Claude it is 512 tokens for Opus 5.5, Opus 5, Sonnet 5.5, Fable 5, Fable 5.1, Fable 5.2 and Haiku 5.5, 1,024 for Opus 4.8, Sonnet 5 and Sonnet 4.6, 2,048 for Opus 4.7, and 4,096 for Opus 4.6 and Opus 4.5. OpenAI models need 1,024 tokens, and other providers typically need around 1,024. For small prompts, consider:

* Adding more context or examples to reach the threshold
* Bundling multiple small requests into batched prompts
* Accepting that caching won't apply for simple queries

### Use `prompt_cache_key` for conversations

For ongoing conversations, set a consistent `prompt_cache_key`:

```json theme={"system"}
// Turn 1
{ "prompt_cache_key": "conv-xyz", "messages": [...] }

// Turn 2
{ "prompt_cache_key": "conv-xyz", "messages": [...] }

// Turn 3
{ "prompt_cache_key": "conv-xyz", "messages": [...] }
```

This improves the likelihood that all turns hit the same server with warm cache.

### Monitor cache performance

Track these metrics:

* **Cache hit rate**: `cached_tokens / prompt_tokens`
* **Cost savings**: Compare actual cost vs. uncached cost
* **Latency reduction**: Time-to-first-token with vs. without cache hits

If `cached_tokens` is consistently 0:

1. Prompts may be below minimum token threshold
2. Prompts may be changing between requests
3. Requests may be hitting different servers (use `prompt_cache_key`)
4. Cache may have expired (requests too infrequent)

### Consider cache economics

**Claude cache writes**: a five-minute write costs 25% more than regular input, and a one-hour write costs twice as much. Reads then cost 10% of the input price or less.

| Scenario | Cache write premium worth it? |
| - | - |
| 1 request with this prompt | No (you pay the write premium and never read it) |
| 2+ requests with the same prefix within five minutes | Yes (a five-minute write pays off on the 2nd request) |
| Requests with the same prefix spread over an hour | Use `"ttl": "1h"` (a one-hour write pays off on the 3rd request) |
| Rapidly changing prompts | No (constant write costs) |
| Stable system prompt, many queries | Yes (amortized over many reads) |

## Cache Lifetime

Caches expire after a period of inactivity (typically 5-10 minutes). This means:

| Traffic pattern | Caching benefit |
| - | - |
| Continuous requests (\< 5 min gaps) | High: cache stays warm |
| Bursty traffic (gaps > 10 min) | Limited: cache expires between bursts |
| Sporadic requests (hours apart) | None: cache always cold |

For Claude, you can extend a cache entry to one hour with a [one-hour cache lifetime](#one-hour-cache-lifetime). OpenAI's GPT-5.6 and later keep entries for at least 30 minutes. On earlier GPT models that support extended retention, set `prompt_cache_retention` to `"24h"` to keep entries for up to 24 hours.

## Caching with Tools and Functions

Function definitions can be cached along with system prompts:

```json theme={"system"}
{
  "model": "claude-sonnet-5-5",
  "tools": [
    {
      "type": "function",
      "function": {
        "name": "search_database",
        "description": "Search the product database",
        "parameters": { ... }
      }
    }
  ],
  "messages": [
    {
      "role": "system",
      "content": [
        {
          "type": "text",
          "text": "You are a shopping assistant...",
          "cache_control": { "type": "ephemeral" }
        }
      ]
    },
    ...
  ]
}
```

The tool definitions become part of the cached prefix. If you have many tools, this can significantly reduce per-request costs.

### Tool loops

In an agentic loop, each request ends with the results of the previous tool calls. On Claude, and on Qwen models hosted by Alibaba, Venice places the automatic marker on the newest tool result, so each round is written to the cache once and read by the next round. You don't need markers of your own.

To mark a tool result yourself on Claude, set `cache_control` on the tool message or inside its content:

```json theme={"system"}
{
  "role": "tool",
  "tool_call_id": "call_1",
  "content": [
    {
      "type": "text",
      "text": "[Large file contents...]",
      "cache_control": { "type": "ephemeral" }
    }
  ]
}
```

Anthropic-style `tool_result` blocks inside a user message keep their `cache_control` too.

## Caching with Images and Documents

For vision models, images can be included in cached content:

```json theme={"system"}
{
  "model": "claude-sonnet-5-5",
  "messages": [
    {
      "role": "user",
      "content": [
        {
          "type": "image_url",
          "image_url": { "url": "data:image/png;base64,..." }
        },
        {
          "type": "text",
          "text": "This is the floor plan. I'll ask several questions about it.",
          "cache_control": { "type": "ephemeral" }
        }
      ]
    },
    {
      "role": "assistant",
      "content": "I can see the floor plan. What would you like to know?"
    },
    {
      "role": "user",
      "content": "How many bedrooms are there?"
    }
  ]
}
```

The image and initial context are cached, so follow-up questions about the same image don't re-process it.

## Troubleshooting

> [accordion] cached\_tokens is always 0
>
> | Cause | Solution |
> | - | - |
> | Prompt too short | Make the cached prefix longer than the model's [minimum](#meet-minimum-token-thresholds) |
> | Prefix changed | Check for dynamic content at the start of your prompt |
> | First request | Expected: first request writes to cache, subsequent requests read |
> | Cache expired | Reduce time between requests to under 5 minutes |
> | Different servers | Add `prompt_cache_key` to route requests consistently |

> [accordion] cache\_creation\_input\_tokens on every request
>
> | Cause | Solution |
> | - | - |
> | Prompt changing | Remove timestamps, request IDs, or other dynamic content from the prefix |
> | Marker on changing content | Put your own `cache_control` on the last block that stays the same across requests, not on content that changes every time |
> | Single user message | Expected for first turn. Cache grows with conversation history. |

> [accordion] Higher costs than expected
>
> | Cause | Solution |
> | - | - |
> | Cache write premium | Claude charges 1.25x input for five-minute writes and 2x for one-hour writes, and GPT-5.6 and later charge 1.25x. Writes only pay off if you reuse the prefix. |
> | One-hour TTL on short-lived prompts | Use the default five-minute TTL for prefixes you reuse within minutes; `"ttl": "1h"` costs twice the input price to write |
> | Low reuse | If each prompt is unique, you pay write costs without read benefits |
> | Bad prompt structure | Move dynamic content to the end so the prefix stays stable |


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.