tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Generative AI > Google Gemini API > Gemini Multi-turn Chat with History Management

Gemini Multi-turn Chat with History Management

Author: Venkata Sudhakar

Multi-turn chat with Gemini requires you to manage conversation history explicitly. The Gemini API is stateless � you send the full conversation history with every request. The model uses this history for context: follow-up questions, pronoun references, corrections, and topic continuity all depend on the model seeing previous turns. Proper history management means including enough context for coherent replies while keeping the total token count affordable and within the model context window limit.

The Gemini SDK provides a ChatSession via client.chats.create() that manages history automatically � you call session.send_message() and it appends turns internally. For production systems needing trimming, injection, or cross-session persistence, you manage the contents list yourself. Use ChatSession for rapid prototyping; use manual history management for production where you need to control exactly what context the model sees and persist history between user visits.

The below example shows both approaches: the simple ChatSession pattern for development, and a production-ready manual history manager with turn trimming, context injection, and cross-session persistence for returning customers.


Production history manager with trimming and cross-session persistence,


It gives the following output showing context carried across both approaches,

=== ChatSession approach ===
User:  Hi, I bought a Samsung TV last week.
Agent: Welcome! I can help with your Samsung TV. What do you need assistance with?

User:  The remote is not working. What should I do?
Agent: Try replacing the batteries first. If that does not help, make sure there
       is nothing blocking the IR sensor on the front of the TV.

User:  I tried new batteries already - still not working.
Agent: Since new batteries did not help, the remote may be faulty. I can arrange
       a replacement remote for your Samsung TV. May I have your order ID?

Total history turns: 6

=== Session 1 ===
Agent: I can help with Samsung TV warranty registration. Please share the
       model number and serial number.

Agent: Warranty registered for Samsung QA65Q80C, serial SN123456789.
       You will receive a confirmation email within 24 hours.

=== Session 2 - returning customer ===
Agent: Welcome back! Yes, the warranty for your Samsung QA65Q80C
       (serial SN123456789) was registered. Can I help with anything else?

History turns saved: 4

# Session 2: agent remembered model and serial from Session 1 via persisted history
# Manual approach gives full control over what context the model sees

History management production rules: always trim history to the last 10 to 20 turns before sending to control token cost � older turns rarely affect current response quality. Persist history in Redis with a TTL of 24 hours for active support tickets and 90 days for account-level context. When injecting memories (Tutorial 324) alongside history, put the memory context in the system instruction rather than the history list � it is always available without consuming history slots. For compliance, log full untruncated history to Cloud Storage or BigQuery before trimming so you have the complete audit trail even though the model only sees the recent window.


 
  


  
bl  br