La mise en cache des prompts est un mécanisme important pour réduire les coûts d’inférence des modèles. En mettant en cache le contenu d’un prompt déjà traité, celui-ci peut être réutilisé dans les requêtes ultérieures, réduisant ainsi les calculs redondants, abaissant les coûts et améliorant l’efficacité des réponses.Documentation Index
Fetch the complete documentation index at: https://docs.aihubmix.com/llms.txt
Use this file to discover all available pages before exploring further.
Principe
Lorsque vous envoyez une requête avec la mise en cache des prompts activée, le système vérifie si le préfixe du prompt a été mis en cache lors de requêtes récentes. Si tel est le cas, il utilise le cache, ce qui réduit le temps de traitement et les coûts ; sinon, il traite l’intégralité du prompt et met en cache le préfixe après le début de la réponse. C’est particulièrement utile dans les scénarios suivants :- Prompts contenant de nombreux exemples
- Contexte ou informations de fond étendus
- Tâches répétitives avec des instructions cohérentes
- Longues conversations multi-tours
Mécanisme principal
Les différents fournisseurs de modèles prennent en charge le cache de différentes manières :Mise en cache automatique
La mise en cache automatique ne nécessite aucune configuration supplémentaire ; le système identifie et met automatiquement en cache le contenu réutilisable, applicable à des modèles comme OpenAI, DeepSeek, etc.OpenAI
- Minimum prompt length: 1024 tokens
- Cost: Writing to cache is free; reading from cache costs 0.25x to 0.5x the original price
Gemini
- Implicit context caching is enabled by default, and caching is automatically effective without manual configuration.
- Caching is only effective when the content, model, and parameters are identical; any differences will be treated as a new request and will not hit the cache.
- The cache validity period is set by the developer, and it can also be left unset. If unspecified, it defaults to 1 hour. There are no minimum or maximum duration limits, and costs depend on the number of cached tokens and cache duration.
DeepSeek / Grok / Moonshot / Groq
- Cost: Writing to cache is free or at the same price, reading from cache is below the original price
Claude Model Display Caching
- Requires manual specification of the cache location via
cache_control - Allows fine-grained control over caching granularity
- Applicable to Anthropic Claude models
OpenAI Compatible Interface
You can set caching breakpoints insystem, user (including images), and tools using the cache_control field. The following examples only show the key structure:
System Message Caching (default 5 minutes TTL):
cache_control at the top level of the tool object (at the same level as type and function):
Anthropic Compatible Interface
Caching Duration
- Default: 5 minutes
- Optional: 1 hour (“ttl”: “1h”)
For more information, please refer to: Claude Prompt Caching
Usage Recommendations
- Maintain Stable Prefixes
- Cache Large Texts
- RAG data
- Long texts
- CSV / JSON data
- Role settings
- Control TTL
- Short sessions → 5 minutes
- Long sessions → 1 hour (more cost-effective)
- Reduce Cache Writes