liveBook · Manning

Chapter 11: Managing session state and chat history

What you will learn in chapter 11 of Build Applications with Local AI Models on a Mac

The introduction identifies the context-window limit as a reason a long chatbot conversation may become slower and less coherent.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The introduction identifies the context-window limit as a reason a long chatbot conversation may become slower and less coherent.

Key terms: context window limit

11.1 The problem: LLMs are stateless

This section explains why a chat app must retain and resend conversation history while keeping that history within the model’s context window.

Key terms: stateless, ollama.chat(), conversation history, context window, st.session_state, application memory, model memory, visible chat history

11.2 Understanding Streamlit session state

This section introduces Streamlit session state and gives setup instructions for running the chapter’s examples.

Key terms: chatbot.py, streamlit run chatbot.py, virtual environment, interpreter

11.2.1 The script rerun problem

This section explains why Streamlit chat apps need session state to keep conversation history across user interactions.

Key terms: script rerun, regular Python variable, conversation history, session state, st.session_state

11.2.2 Session state in action

A counter demo shows how Streamlit session state keeps a value across script reruns and how the same initialization pattern preserves chatbot messages.

Key terms: st.session_state, initialization check, messages, counter_demo.py, streamlit run, Increment, Decrement

11.2.3 What you can store in session state

This subsection gives a rule for deciding which values to keep in Streamlit session state across app reruns.

Key terms: session state, st.session_state, reruns

11.2.4 When session state resets

This section explains when Streamlit session state resets and why separate users can have independent conversations.

Key terms: session state, browser tab, Streamlit server, independent session state, separate conversations

11.3 How LLMs use conversation history

This section explains how a stateless LLM appears to remember a chat because the app resends its message history, and why that becomes costly as the history grows.

Key terms: stateless, ollama.chat(), messages=st.session_state.messages, conversation history, Python lists, mutable, st.session_state.messages, appending, retained conversation history, tokens

11.4 Context window management

This section explains how a model’s token-limited context window constrains the amount of conversation it can process at once.

Key terms: context window, tokens, model capacities, active context, ollama ps, conversation history

11.4.1 What happens when you exceed the context window

This subsection explains how an overlong conversation can degrade a chatbot’s responses and why sending more context also slows it down.

Key terms: context window, earliest messages, response quality, large context windows, processing time, performance

11.4.2 Implementing history trimming

This section shows how to send only recent chat history to the model while keeping the full conversation visible to the user.

Key terms: max_history, completed exchanges, context window, message-count trimming, conversation statistics, total number of messages, approximate token count

11.4.3 The limited history tradeoff

This section explains the context lost when chat history is trimmed, why trimming is usually sufficient, and two ways to retain access to older information.

Key terms: history trimming, context, exchanges, Summarization, RAG, retrieval-augmented generation

11.5 Adding a reset button

This section adds a session-state confirmation step to Clear Chat so a user cannot erase a conversation with one click.

Key terms: Clear Chat, confirm_clear, session state, messages

11.6 Other useful session state patterns

The section shows two session-state enhancements: preserving a conversation start time across Streamlit reruns and detecting when the user changes models.

Key terms: conversation start time, session_state, model-change detection, model-selection widget, conversation state

11.7 The complete chat application data flow

This section traces a chat message through Streamlit, session state, Ollama, and the local LLM, then explains why streaming makes the response feel faster.

Key terms: st.chat_input(), session state, st.session_state.messages, max_history, ollama.chat(), local LLM, tokens, streaming, placeholder

11.8 Exercises

11.9 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 10: Building a voice-enabled AI chat application Chapter 12: Comparing and selecting LLM models →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.