◉GEMINI LABJP
●ARGON — gemini-4-argon is generally available as of 10/13, aimed at complex coding, agents, math and science●DEEP RES — deep-research-pro-preview-12-2025 shuts down 10/23, 6 days left. Move to deep-research-preview-04-2026●NANO — gemini-nano-banana-2.1 is GA (10/06). gemini-3.1-flash-image is deprecated with no shutdown date yet●MCP — One write-up cut an 85,000-token tool result to 310 tokens by wrapping it in an ADK FunctionTool●FLASH — gemini-3.7-flash and 3.5-flash now route automatically to newer Flash models (10/08)●NEW — Stop Re-Reading Every Crash Report: Gre…●ARGON — gemini-4-argon is generally available as of 10/13, aimed at complex coding, agents, math and science●DEEP RES — deep-research-pro-preview-12-2025 shuts down 10/23, 6 days left. Move to deep-research-preview-04-2026●NANO — gemini-nano-banana-2.1 is GA (10/06). gemini-3.1-flash-image is deprecated with no shutdown date yet●MCP — One write-up cut an 85,000-token tool result to 310 tokens by wrapping it in an ADK FunctionTool●FLASH — gemini-3.7-flash and 3.5-flash now route automatically to newer Flash models (10/08)●NEW — Stop Re-Reading Every Crash Report: Gre…
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-api286chat4historysummaryrolling-summaryproduction140

✦ 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-15
Sample what you already accepted — an audit budget that catches silent quality drift
A confidence gate only ever looks at output the model hesitated on. Silent drift sinks into the batch that sailed through. Working from a fixed 30-minutes-a-day review budget, this walks through deriving detection time from the binomial, reallocating the same budget across risk strata, and catching slow decay with a cumulative monitor.
◈ 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.
◈ API / SDK2026-05-20
Evolving Gemini API Structured Output Schemas in Production — Design Notes from an Indie App
How I rebuilt the JSON contract layer for a Gemini-powered recommendation feature in a long-running indie app — Dual-Emit, Sunset protocol, and a Python compatibility checker.