Chapter 4: Building a Simple Chatbot with Memory
Chapter 4 Summary
In this chapter, you took a significant step toward building intelligent, user-friendly applications by creating a chatbot that doesn’t just respond — it remembers.
We began with the foundational structure of a chatbot using two powerful frameworks: Streamlit and Flask. Streamlit offered a rapid development environment with minimal setup, making it ideal for quick prototypes and internal tools. Flask, on the other hand, provided full control over HTML, CSS, and route management, allowing for deeper customization and future scalability. Regardless of your preferred tool, you now know how to create a fully functioning chatbot interface from scratch.
From there, we explored how to improve the user experience through thoughtful interface design. Using st.chat_message()
in Streamlit and custom HTML in Flask, we gave your chatbot a clean, conversational look — something users expect from modern applications. You learned how to enhance clarity with avatars, loading spinners, and markdown styling to make the interaction feel more human and less robotic.
But great design is only half the story — what makes a chatbot truly compelling is its ability to remember. In the second half of the chapter, you learned how to implement session-based memory using st.session_state
(in Streamlit) and the flask.session
object (in Flask). This means your chatbot can now recall what the user has said earlier in the conversation and respond in context — a fundamental leap toward creating a natural, flowing dialogue.
We also discussed best practices for managing token usage. GPT models, including GPT-4o, operate within a context window (up to 128k tokens), and allowing conversations to grow unchecked can lead to slower performance and higher costs. By limiting history to the most recent exchanges, you keep the interaction efficient and maintain coherence.
Most importantly, this chapter marked the transition from passive to active AI development. You’re no longer just consuming responses — you're now designing real-world tools that hold state, manage sessions, and deliver interactive value to your users.
Chapter 4 Summary
In this chapter, you took a significant step toward building intelligent, user-friendly applications by creating a chatbot that doesn’t just respond — it remembers.
We began with the foundational structure of a chatbot using two powerful frameworks: Streamlit and Flask. Streamlit offered a rapid development environment with minimal setup, making it ideal for quick prototypes and internal tools. Flask, on the other hand, provided full control over HTML, CSS, and route management, allowing for deeper customization and future scalability. Regardless of your preferred tool, you now know how to create a fully functioning chatbot interface from scratch.
From there, we explored how to improve the user experience through thoughtful interface design. Using st.chat_message()
in Streamlit and custom HTML in Flask, we gave your chatbot a clean, conversational look — something users expect from modern applications. You learned how to enhance clarity with avatars, loading spinners, and markdown styling to make the interaction feel more human and less robotic.
But great design is only half the story — what makes a chatbot truly compelling is its ability to remember. In the second half of the chapter, you learned how to implement session-based memory using st.session_state
(in Streamlit) and the flask.session
object (in Flask). This means your chatbot can now recall what the user has said earlier in the conversation and respond in context — a fundamental leap toward creating a natural, flowing dialogue.
We also discussed best practices for managing token usage. GPT models, including GPT-4o, operate within a context window (up to 128k tokens), and allowing conversations to grow unchecked can lead to slower performance and higher costs. By limiting history to the most recent exchanges, you keep the interaction efficient and maintain coherence.
Most importantly, this chapter marked the transition from passive to active AI development. You’re no longer just consuming responses — you're now designing real-world tools that hold state, manage sessions, and deliver interactive value to your users.
Chapter 4 Summary
In this chapter, you took a significant step toward building intelligent, user-friendly applications by creating a chatbot that doesn’t just respond — it remembers.
We began with the foundational structure of a chatbot using two powerful frameworks: Streamlit and Flask. Streamlit offered a rapid development environment with minimal setup, making it ideal for quick prototypes and internal tools. Flask, on the other hand, provided full control over HTML, CSS, and route management, allowing for deeper customization and future scalability. Regardless of your preferred tool, you now know how to create a fully functioning chatbot interface from scratch.
From there, we explored how to improve the user experience through thoughtful interface design. Using st.chat_message()
in Streamlit and custom HTML in Flask, we gave your chatbot a clean, conversational look — something users expect from modern applications. You learned how to enhance clarity with avatars, loading spinners, and markdown styling to make the interaction feel more human and less robotic.
But great design is only half the story — what makes a chatbot truly compelling is its ability to remember. In the second half of the chapter, you learned how to implement session-based memory using st.session_state
(in Streamlit) and the flask.session
object (in Flask). This means your chatbot can now recall what the user has said earlier in the conversation and respond in context — a fundamental leap toward creating a natural, flowing dialogue.
We also discussed best practices for managing token usage. GPT models, including GPT-4o, operate within a context window (up to 128k tokens), and allowing conversations to grow unchecked can lead to slower performance and higher costs. By limiting history to the most recent exchanges, you keep the interaction efficient and maintain coherence.
Most importantly, this chapter marked the transition from passive to active AI development. You’re no longer just consuming responses — you're now designing real-world tools that hold state, manage sessions, and deliver interactive value to your users.
Chapter 4 Summary
In this chapter, you took a significant step toward building intelligent, user-friendly applications by creating a chatbot that doesn’t just respond — it remembers.
We began with the foundational structure of a chatbot using two powerful frameworks: Streamlit and Flask. Streamlit offered a rapid development environment with minimal setup, making it ideal for quick prototypes and internal tools. Flask, on the other hand, provided full control over HTML, CSS, and route management, allowing for deeper customization and future scalability. Regardless of your preferred tool, you now know how to create a fully functioning chatbot interface from scratch.
From there, we explored how to improve the user experience through thoughtful interface design. Using st.chat_message()
in Streamlit and custom HTML in Flask, we gave your chatbot a clean, conversational look — something users expect from modern applications. You learned how to enhance clarity with avatars, loading spinners, and markdown styling to make the interaction feel more human and less robotic.
But great design is only half the story — what makes a chatbot truly compelling is its ability to remember. In the second half of the chapter, you learned how to implement session-based memory using st.session_state
(in Streamlit) and the flask.session
object (in Flask). This means your chatbot can now recall what the user has said earlier in the conversation and respond in context — a fundamental leap toward creating a natural, flowing dialogue.
We also discussed best practices for managing token usage. GPT models, including GPT-4o, operate within a context window (up to 128k tokens), and allowing conversations to grow unchecked can lead to slower performance and higher costs. By limiting history to the most recent exchanges, you keep the interaction efficient and maintain coherence.
Most importantly, this chapter marked the transition from passive to active AI development. You’re no longer just consuming responses — you're now designing real-world tools that hold state, manage sessions, and deliver interactive value to your users.