DEVUP Docs
Back to Dashboard
Model-dependent optimization

Prompt Caching

Prompt caching can reduce repeated input processing for supported models and providers when requests share a reusable prompt prefix.

Caching availability varies by model and upstream provider. Cache hits are not guaranteed, and any latency or cost improvements depend on the selected model's current pricing. Applications should never depend on cache state for functional correctness.

How it works

Some upstream providers may automatically cache eligible prompt prefixes. When you send a request with a repeated prefix, the provider checks for reusable cached input.

1. Stable Prefix
2. Cache Lookup
3. Possible Cache Hit
4. Process Remaining Input
5. Generate Response

Important

Prompt caching is an optimization. The request must still produce the same correct behavior when no cache hit occurs.

Implementation

You do not need to send special parameters to enable caching on supported models. Structure your prompts so that identical, reusable content appears at the very beginning of the request.

import os
from openai import OpenAI

client = OpenAI(
    api_key=os.environ["DEVUP_API_KEY"],
    base_url="https://api.devupai.com/v1",
)

# Long system prompt that stays the same across requests
SYSTEM_PROMPT = """You are a helpful AI assistant with deep expertise in Python.\n[... thousands of tokens of instructions or context ...]\n"""

# First request - full processing
response1 = client.chat.completions.create(
    model="deepseek-ai/DeepSeek-V4-Pro",
    messages=[
        {"role": "system", "content": SYSTEM_PROMPT},
        {"role": "user", "content": "How do I use list comprehensions?"},
    ],
)

# Second request - matching prefix may be reused
response2 = client.chat.completions.create(
    model="deepseek-ai/DeepSeek-V4-Pro",
    messages=[
        {"role": "system", "content": SYSTEM_PROMPT},
        {"role": "user", "content": "What are Python generators?"},
    ],
)

Best practices

Place stable content first

Ensure static instructions, documents, or context appear at the very beginning of your prompt sequence. Put variable user content later.

Byte-for-byte consistency

Keep repeated prefixes byte-for-byte consistent when required by the provider. Avoid inserting timestamps or request-specific values into reusable prefixes.

Measure, don't assume

Measure actual latency and usage metadata instead of assuming a hit. Cache states are ephemeral.

Security consideration

Do not include secrets or user-isolated PII in shared prompt prefixes if those prefixes might be reused across different user sessions.

Common use cases

When constructing your prompt architecture, these elements are typically the best candidates for the static prefix:

ExamplePrefix structure
Long system instructionsStable assistant policies
RAG / document Q&ARepeated reference documents
ClassificationFew-shot examples
Code assistantCodebase context
Multi-turn conversationsBe careful: previous turns may change each request and break the reusable prefix.

Checking cache usage

Cache usage metadata may be available when returned by the selected provider. When supported, the response usage object indicates how many tokens were served from cache:

Illustrative Response
json
{
  "usage": {
    "prompt_tokens": 5000,
    "completion_tokens": 50,
    "total_tokens": 5050,
    "prompt_tokens_details": {
      "cached_tokens": 4800
    }
  }
}

Pricing

Cached-input pricing, when available, is model-specific. Review the live model pricing before relying on caching for cost estimates.

Model Support

Check the selected model page or live model catalog for current caching support.

What's next