در یک گردش کاری معمولی هوش مصنوعی، ممکن است همان توکن های ورودی را بارها و بارها به یک مدل ارسال کنید. Gemini API دو مکانیسم کش متفاوت ارائه می دهد:
کش ضمنی (به طور خودکار در مدل های Gemini 2.5 فعال می شود، بدون ضمانت صرفه جویی در هزینه)
کش صریح (در اکثر مدل ها به صورت دستی فعال می شود، تضمین صرفه جویی در هزینه)
ذخیره سازی آشکار در مواردی مفید است که می خواهید صرفه جویی در هزینه را تضمین کنید، اما با برخی کار توسعه دهنده اضافه شده است.
ذخیره سازی ضمنی
کش ضمنی به طور پیش فرض برای همه مدل های Gemini 2.5 فعال است. اگر درخواست شما به حافظه پنهان برسد، ما به طور خودکار در هزینه صرفه جویی می کنیم. برای فعال کردن این کار نیازی به انجام کاری نیست. از 8 مه 2025 قابل اجرا است. حداقل تعداد توکن ورودی برای ذخیره متنی 1024 برای 2.5 Flash و 4096 برای 2.5 Pro است.
برای افزایش احتمال ضربه پنهان ضمنی:
سعی کنید مطالب بزرگ و رایج را در ابتدای درخواست خود قرار دهید
سعی کنید در مدت زمان کوتاهی درخواست هایی با پیشوند مشابه ارسال کنید
میتوانید تعداد نشانههایی را که در حافظه پنهان بازدید کردهاند را در قسمت usage_metadata شی پاسخ ببینید.
ذخیره سازی آشکار
با استفاده از ویژگی cache صریح Gemini API، میتوانید یک بار مقداری از محتوا را به مدل ارسال کنید، نشانههای ورودی را در حافظه پنهان نگه دارید و سپس برای درخواستهای بعدی به نشانههای کش شده مراجعه کنید. در حجمهای معین، استفاده از توکنهای ذخیرهسازی شده هزینه کمتری نسبت به ارسال مکرر در یک مجموعه توکنها دارد.
هنگامی که مجموعه ای از نشانه ها را در حافظه پنهان ذخیره می کنید، می توانید انتخاب کنید که چه مدت می خواهید کش وجود داشته باشد قبل از اینکه نشانه ها به طور خودکار حذف شوند. این مدت زمان ذخیره سازی، زمان زندگی (TTL) نامیده می شود. اگر تنظیم نشود، TTL به طور پیش فرض روی 1 ساعت است. هزینه ذخیره سازی به اندازه توکن ورودی و مدت زمانی که می خواهید توکن ها باقی بمانند بستگی دارد.
این بخش فرض میکند که Gemini SDK را نصب کردهاید (یا curl را نصب کردهاید) و یک کلید API را پیکربندی کردهاید، همانطور که در شروع سریع نشان داده شده است.
ذخیره سازی آشکار با استفاده از کتابخانه OpenAI
اگر از کتابخانه OpenAI استفاده میکنید، میتوانید کش کردن صریح را با استفاده از ویژگی cached_content در extra_body فعال کنید.
زمان استفاده از کش صریح
ذخیره سازی متن به ویژه برای سناریوهایی که در آن یک زمینه اولیه قابل توجه به طور مکرر توسط درخواست های کوتاهتر ارجاع داده می شود، مناسب است. استفاده از کش زمینه برای موارد استفاده مانند:
ذخیره سازی متن یک ویژگی پولی است که برای کاهش هزینه های عملیاتی کلی طراحی شده است. صورتحساب بر اساس عوامل زیر است:
تعداد نشانههای حافظه پنهان: تعداد نشانههای ورودی ذخیرهشده در حافظه پنهان که در صورت درج در درخواستهای بعدی با نرخ کاهشیافته صورتحساب میشوند.
مدت زمان ذخیره سازی: مقدار زمانی که توکن های ذخیره شده در حافظه پنهان ذخیره می شوند (TTL) که بر اساس مدت زمان TTL تعداد توکن های ذخیره شده صورتحساب می شود. هیچ محدودیتی برای حداقل یا حداکثر در TTL وجود ندارد.
عوامل دیگر: هزینههای دیگری اعمال میشود، مانند نشانههای ورودی و نشانههای خروجی غیر ذخیرهسازی شده در حافظه پنهان.
برای جزئیات قیمت به روز، به صفحه قیمت گذاری Gemini API مراجعه کنید. برای یادگیری نحوه شمارش نشانهها، راهنمای توکن را ببینید.
ملاحظات اضافی
هنگام استفاده از کش زمینه، ملاحظات زیر را در نظر داشته باشید:
حداقل تعداد توکن ورودی برای کش متنی 1024 برای 2.5 Flash و 2048 برای 2.5 Pro است. حداکثر همان حداکثر برای مدل داده شده است. (برای اطلاعات بیشتر در مورد شمارش نشانه ها، به راهنمای توکن مراجعه کنید).
این مدل هیچ تمایزی بین نشانههای حافظه پنهان و نشانههای ورودی معمولی قائل نمیشود. محتوای ذخیره شده در حافظه پنهان پیشوندی برای درخواست است.
هیچ محدودیت یا نرخ خاصی برای ذخیره سازی متن وجود ندارد. محدودیتهای نرخ استاندارد برای GenerateContent اعمال میشود و محدودیتهای رمز شامل توکنهای ذخیرهشده نیز میشود.
تعداد توکنهای ذخیرهشده در usage_metadata از عملیات ایجاد، دریافت و فهرست سرویس کش و همچنین در GenerateContent هنگام استفاده از کش برگردانده میشود.
تاریخ آخرین بهروزرسانی 2025-08-22 بهوقت ساعت هماهنگ جهانی.
[[["درک آسان","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,["# Context caching\n\nPython 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----------------\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 4,096 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----------------\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\n### Explicit 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----------------------------\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\n### How 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\n### Additional 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."]]