Agentic AI

A chatbot in one script

The whole module in one Python file: a terminal chatbot with Gemini that keeps one history per conversation, trims what it sends, and streams its answers. Read the script to revise; press Explain on it for every step explained.

Concept + code Python 3.12 Needs GOOGLE_API_KEY

Lessons 7.1 to 7.4 built chains from runnables, served one, and gave a chatbot a memory and a budget. This lesson puts the chatbot in one script, with Gemini as the model, so it can be read, run and remembered as a whole; it is the page to come back to for the syntax.

Before you run it

  • GOOGLE_API_KEY in your .env file, created in Google AI Studio (lesson 6.1).
  • The packages of lesson 6.1's requirements.txt; the script uses langchain-google-genai and langchain-core.

The script

chatbot.pyPython
"""A terminal chatbot with Gemini: one history per conversation, trimmed to a budget, answers streamed."""
import sys
from operator import itemgetter

from dotenv import load_dotenv
from langchain_core.chat_history import BaseChatMessageHistory, InMemoryChatMessageHistory
from langchain_core.messages import HumanMessage, trim_messages
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_core.runnables import RunnablePassthrough
from langchain_core.runnables.history import RunnableWithMessageHistory
from langchain_google_genai import ChatGoogleGenerativeAI

load_dotenv()  # GOOGLE_API_KEY from .env

# 1. Model: messages in, an AIMessage out (lesson 6.1)
llm = ChatGoogleGenerativeAI(model="gemini-3.5-flash-lite")

# 2. Prompt: instructions, then the conversation so far (lesson 7.3)
prompt = ChatPromptTemplate.from_messages(
    [
        ("system", "You are a helpful assistant. Answer in {language}, in at most three sentences."),
        MessagesPlaceholder(variable_name="messages"),
    ]
)

# 3. Trimmer: the newest messages that fit about 1,000 tokens (lesson 7.4)
trimmer = trim_messages(
    max_tokens=1000,
    strategy="last",
    token_counter="approximate",
    include_system=True,
    start_on="human",
)

# 4. Chain: trim the messages, fill the prompt, call the model, keep the text (lesson 7.1)
chain = (
    RunnablePassthrough.assign(messages=itemgetter("messages") | trimmer)
    | prompt
    | llm
    | StrOutputParser()
)

# 5. Memory: one history per session ID, kept in this process (lesson 7.3)
store: dict[str, BaseChatMessageHistory] = {}


def get_session_history(session_id: str) -> BaseChatMessageHistory:
    if session_id not in store:
        store[session_id] = InMemoryChatMessageHistory()
    return store[session_id]


chatbot = RunnableWithMessageHistory(chain, get_session_history, input_messages_key="messages")


def main() -> None:
    language = sys.argv[1] if len(sys.argv) > 1 else "English"
    session_id = "default"
    print("Type /session NAME to switch conversations, /quit to stop.")
    print(f"{len(get_session_history(session_id).messages)} messages in {session_id!r}")
    while True:
        text = input(f"[{session_id}] You: ").strip()
        if text == "/quit":
            break
        if text.startswith("/session "):
            session_id = text.removeprefix("/session ").strip()
            print(f"{len(get_session_history(session_id).messages)} messages in {session_id!r}")
            continue
        if not text:
            continue
        config = {"configurable": {"session_id": session_id}}
        print("Bot: ", end="", flush=True)
        for chunk in chatbot.stream({"messages": [HumanMessage(content=text)], "language": language}, config=config):
            print(chunk, end="", flush=True)
        print()


if __name__ == "__main__":
    main()

The chat model name is Google's at the time of writing; check it in Google's model list VERIFY, since Google retires older models.

Run it

Terminalzsh
python chatbot.py
OutputIllustrative · the answers vary per run; the warnings' file path is shortened
sys:1: LangChainDeprecationWarning: RunnableWithMessageHistory is deprecated. Use LangGraph's built-in persistence instead.
Type /session NAME to switch conversations, /quit to stop.
chatbot.py:50: LangChainDeprecationWarning: The class `InMemoryChatMessageHistory` was deprecated in LangChain 1.6.4 and will be removed in 2.0.0 See the short-term memory documentation for recommended alternatives: https://docs.langchain.com/oss/python/langchain/short-term-memory
  store[session_id] = InMemoryChatMessageHistory()
0 messages in 'default'
[default] You: Hi, I'm Alice and I'm learning LangChain.
Bot: Nice to meet you, Alice! LangChain is a good place to start building apps on language models. What would you like to learn first?
[default] You: What's my name?
Bot: Your name is Alice.
[default] You: /session work
0 messages in 'work'
[work] You: What's my name?
Bot: I don't know your name yet: you haven't told me in this conversation.
[work] You: /session default
4 messages in 'default'
[default] You: /quit

The two warnings appear once each, when the wrapper is created and when the first history is. The work session knows nothing of default, and default still holds its four messages: two questions and two replies.

Swap one piece

Step In the script Alternatives
Model ChatGoogleGenerativeAI(...) ChatGroq(model="openai/gpt-oss-20b") (7.1), ChatOpenAI(model="gpt-4o") (6.1), ChatOllama(model="qwen3.5") (6.2)
Counting tokens token_counter="approximate" token_counter=llm, the provider's count; token_counter=len, a number of messages (7.4)
Shortening the history trim_messages A summary of older turns: SummarizationMiddleware (9.6)
Memory InMemoryChatMessageHistory in a dict A checkpointer with a thread_id: create_agent(..., checkpointer=...) (9.4), a LangGraph graph (11.5)
Interface A terminal loop A web API with LangServe or FastAPI (7.2); a Streamlit page (lesson 3.12)

Practice

Practice 1Start over. Add a /reset command that empties the current session's history, so the bot forgets the conversation without switching sessions.Show solution
chatbot.py (in the loop, before the empty-text check)Python
        if text == "/reset":
            get_session_history(session_id).clear()
            print(f"0 messages in {session_id!r}")
            continue

clear() empties the history object in the store; the next message starts a fresh conversation under the same session ID.

Practice 2Remember across runs. Save every session to histories.json when the user types /quit, and load the file when the script starts, so conversations survive a restart.Show solution
chatbot.py (changed parts)Python
import json
from pathlib import Path

from langchain_core.messages import messages_from_dict, messages_to_dict

HISTORY_FILE = Path("histories.json")


def load_store() -> None:
    if HISTORY_FILE.exists():
        for session_id, data in json.loads(HISTORY_FILE.read_text()).items():
            get_session_history(session_id).add_messages(messages_from_dict(data))


def save_store() -> None:
    data = {session_id: messages_to_dict(history.messages) for session_id, history in store.items()}
    HISTORY_FILE.write_text(json.dumps(data, indent=2))

# in main(): call load_store() before the loop, and save_store() before break on /quit

messages_to_dict turns messages into plain dicts that JSON can store, and messages_from_dict turns them back (lesson 7.3). A server would write to a database instead, after every turn rather than at the end.