GEMINI LABJP
FLASH35 — Gemini 3.5 Flash is now GA and powers gemini-flash-latest, making everyday generation faster and more affordableAGENTS — Managed Agents launch in public preview in the Gemini API, running in secure, isolated Google-hosted Linux sandboxesMEDIA — Nano Banana 2 Lite and Gemini Omni Flash bring faster image and high-quality video generation across AI Studio and the APITTS — Streaming speech generation is now supported for gemini-3.1-flash-tts-preview via streamGenerateContentTRANSLATE — A new audio model detects 70+ languages for live speech-to-speech translation while preserving natural intonationSPENDCAP — Project-level spend caps for billing have been added in Google AI Studio to keep costs under controlFLASH35 — Gemini 3.5 Flash is now GA and powers gemini-flash-latest, making everyday generation faster and more affordableAGENTS — Managed Agents launch in public preview in the Gemini API, running in secure, isolated Google-hosted Linux sandboxesMEDIA — Nano Banana 2 Lite and Gemini Omni Flash bring faster image and high-quality video generation across AI Studio and the APITTS — Streaming speech generation is now supported for gemini-3.1-flash-tts-preview via streamGenerateContentTRANSLATE — A new audio model detects 70+ languages for live speech-to-speech translation while preserving natural intonationSPENDCAP — Project-level spend caps for billing have been added in Google AI Studio to keep costs under control
Articles/API / SDK
API / SDK/2026-06-22Advanced

Compressing Gemini API Chat History with Rolling Summaries — Designing Chatbots That Survive Hundreds of Turns

When a Gemini chatbot grows long enough, your bills balloon and one day a request hits the token ceiling. The rolling-summary pattern keeps long chats stable.

gemini-api274chat4historysummaryrolling-summaryproduction137

Premium Article

If you have shipped a Gemini-backed chatbot, you have probably seen the moment when one specific user starts getting silent failures while everyone else is fine. Dig into the logs and you find Request contains too many tokens repeating, and the cause turns out to be a chat history that simply got too long.

I have walked into this wall on a personal side project and on a Discord bot, and the fix that has consistently worked for me is an old idea applied carefully: rolling summaries. The implementation is small, the cost is predictable, and once it is in place you stop worrying about the user who chats every day.

Why a plain sliding window is not enough

The first instinct is to keep only the last N turns. The implementation takes ten lines, but it ruins the user experience. A user who told the bot their name eleven turns ago will hear the bot ask for it again, which feels broken. Pruning by recency alone throws away exactly the kind of information that makes a chat useful.

Rolling summaries solve this with a two-tier approach: older turns are compressed into a short paragraph that is kept in the prompt, while the most recent turns are preserved verbatim. The compressed portion stays roughly the same size no matter how long the conversation grows, so the total prompt budget stays bounded.

Architecture in three pieces

Three small functions are enough.

  • count_tokens(history) reports the current size of the history.
  • should_compress(history, threshold) decides whether it is time to compress.
  • compress(history, keep_recent) replaces the older portion with a summary and keeps the last few turns intact.

The two interesting questions are when to trigger compression and what to put in the summary. Pick the threshold based on cost rather than the model's hard limit. Gemini 2.5 Pro can hold a million tokens, but sending hundreds of thousands every turn is not financially realistic for a chatbot.

Thank you for reading this far.

Continue Reading

What follows includes implementation code, benchmarks, and practical content we hope you'll find useful. This site runs without ads — server and development costs are supported entirely by members like you. If it's been helpful, we'd be truly grateful for your support.

WHAT YOU'LL LEARN
Turn a chatbot whose bill balloons and eventually hits the token ceiling into one whose input cost is bounded
Pre-empt the three production-only pitfalls — default-model swaps, summaries dropping facts, and the double-compression race — with working code
Encode the line between what is safe to summarize and what must never be summarized, so sessions stay stable across hundreds of turns
Secure payment via Stripe · Cancel anytime

Unlock This Article

Get full access to the rest of this article. Buy once, read anytime. This site is ad-free — your support goes directly toward keeping it running.

or
Unlock all articles with Membership →
Share

Thank You for Reading

Gemini Lab is ad-free, supported entirely by members like you. We publish practical guides daily with implementation code, benchmarks, and production-ready patterns. If you've found it useful, we'd love to have you on board.

  • Copy-paste ready implementation code
  • New advanced guides published daily
  • $5/mo or $10 for lifetime access
View Membership →

Related Articles

API / SDK2026-07-06
Measure a Managed Agent's Behavior Against Fixed Scenarios Before It Reaches Production
The public-preview Managed Agents run autonomously inside an isolated sandbox, so a small prompt or config change can quietly shift their behavior. Diffing the output once, the way you would for a single prompt, is not enough. Here is how to build a regression harness that runs fixed scenarios repeatedly and judges on pass rate, plus a shadow to canary to full promotion with automatic rollback, all with runnable Python.
API / SDK2026-07-05
Catching only the deprecations that touch you — feeding the official changelog to url-context
I found out an image model was being shut down three days before the deadline. Here is a deprecation radar that reads the official changelog through url-context and surfaces only the models I actually use, with working Python and the over-alerting tuning I had to do in production.
API / SDK2026-07-04
When Gemini API Leaks Japanese Into Your English Output Once in a While — Field Notes on Measuring the Contamination Rate and Tightening It in Stages
You told Gemini to answer in English, and 3 out of 100 runs slip a Japanese sentence into the tail. Here is why you cannot stop that 'once in a while', and a production pattern that measures the contamination rate as an SLO and tightens it with graded recovery, with working code.
📚RECOMMENDED BOOKS
Build a Large Language Model (From Scratch)
Sebastian Raschka
LLM Dev
Prompt Engineering for LLMs
Berryman & Ziegler
Prompting
AI Engineering
Chip Huyen
AI Eng
* Contains affiliate links
See all →