[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-08-22 (世界標準時間)。"],[],[],null,["Python JavaScript Go REST\n\nIn a typical AI workflow, you might pass the same input tokens over and over to\na model. The Gemini API offers two different caching mechanisms:\n\n- Implicit caching (automatically enabled on Gemini 2.5 models, no cost saving guarantee)\n- Explicit caching (can be manually enabled on most models, cost saving guarantee)\n\nExplicit caching is useful in cases where you want to guarantee cost savings,\nbut with some added developer work.\n\nImplicit caching\n\nImplicit caching is enabled by default for all Gemini 2.5 models. We automatically\npass on cost savings if your request hits caches. There is nothing you need to do\nin order to enable this. It is effective as of May 8th, 2025. The minimum input\ntoken count for context caching is 1,024 for 2.5 Flash and 2,048 for 2.5 Pro.\n\nTo increase the chance of an implicit cache hit:\n\n- Try putting large and common contents at the beginning of your prompt\n- Try to send requests with similar prefix in a short amount of time\n\nYou can see the number of tokens which were cache hits in the response object's\n`usage_metadata` field.\n\nExplicit caching\n\nUsing the Gemini API explicit caching feature, you can pass some content\nto the model once, cache the input tokens, and then refer to the cached tokens\nfor subsequent requests. At certain volumes, using cached tokens is lower cost\nthan passing in the same corpus of tokens repeatedly.\n\nWhen you cache a set of tokens, you can choose how long you want the cache to\nexist before the tokens are automatically deleted. This caching duration is\ncalled the *time to live* (TTL). If not set, the TTL defaults to 1 hour. The\ncost for caching depends on the input token size and how long you want the\ntokens to persist.\n\nThis section assumes that you've installed a Gemini SDK (or have curl installed)\nand that you've configured an API key, as shown in the\n[quickstart](/gemini-api/docs/quickstart).\n\nExplicit caching using the OpenAI library\n\nIf you're using an [OpenAI library](/gemini-api/docs/openai), you can enable\nexplicit caching using the `cached_content` property on\n[`extra_body`](/gemini-api/docs/openai#extra-body).\n\nWhen to use explicit caching\n\nContext caching is particularly well suited to scenarios where a substantial\ninitial context is referenced repeatedly by shorter requests. Consider using\ncontext caching for use cases such as:\n\n- Chatbots with extensive [system instructions](/gemini-api/docs/system-instructions)\n- Repetitive analysis of lengthy video files\n- Recurring queries against large document sets\n- Frequent code repository analysis or bug fixing\n\nHow explicit caching reduces costs\n\nContext caching is a paid feature designed to reduce overall operational costs.\nBilling is based on the following factors:\n\n1. **Cache token count:** The number of input tokens cached, billed at a reduced rate when included in subsequent prompts.\n2. **Storage duration:** The amount of time cached tokens are stored (TTL), billed based on the TTL duration of cached token count. There are no minimum or maximum bounds on the TTL.\n3. **Other factors:** Other charges apply, such as for non-cached input tokens and output tokens.\n\nFor up-to-date pricing details, refer to the Gemini API [pricing\npage](/pricing). To learn how to count tokens, see the [Token\nguide](/gemini-api/docs/tokens).\n\nAdditional considerations\n\nKeep the following considerations in mind when using context caching:\n\n- The *minimum* input token count for context caching is 1,024 for 2.5 Flash and 2,048 for 2.5 Pro. The *maximum* is the same as the maximum for the given model. (For more on counting tokens, see the [Token guide](/gemini-api/docs/tokens)).\n- The model doesn't make any distinction between cached tokens and regular input tokens. Cached content is a prefix to the prompt.\n- There are no special rate or usage limits on context caching; the standard rate limits for `GenerateContent` apply, and token limits include cached tokens.\n- The number of cached tokens is returned in the `usage_metadata` from the create, get, and list operations of the cache service, and also in `GenerateContent` when using the cache."]]