A conversation thread is the running record of earlier user and agent messages. Because each model call starts without prior context, the application resends relevant history so the next response can interpret follow-up questions. section 1.4
A simple thread follows this loop:
- Start with an empty history. section 3.4
- Append each new user message and agent response. section 3.4
- Inject the accumulated history into the next prompt before calling the model. section 3.4
Remembering a name
After a user says, “Hi, I’m Andrea,” the application includes that exchange when the user later asks, “What’s my name?” The model can then answer “Andrea” because the prior turn accompanies the new request. section 1.4
import os
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI #A
from langchain_core.prompts import PromptTemplate #B
from langchain_core.messages import SystemMessage, HumanMessage #C
load_dotenv() #D
os.environ["OPENAI_API_KEY"] = "REPLACE_WITH_YOUR_KEY"
if not os.getenv("OPENAI_API_KEY"): #E
raise RuntimeError("Set OPENAI_API_KEY before running this script.")
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0.1) #F
system_msg = SystemMessage(content="Always reply in Pirate English.") #G
prompt = PromptTemplate(
input_variables=["message", "history"],
template="{history}\nUser: {message}\nAI: ",
) #H
def chat_loop() -> None: #I
"""Chat loop with manual history injection, matching Langflow’s wiring."""
history_lines: list[str] = []
try:
while True:
history_block = "\n".join(history_lines)
user_input = input("User: ")
rendered_prompt = prompt.format(message=user_input, history=history_block)
response = llm.invoke(
[system_msg, HumanMessage(content=rendered_prompt)]
) #J
print("AI:", response.content) #K
history_lines.extend(
[f"User: {user_input}", f"AI: {response.content}"]
) #L
except KeyboardInterrupt:
print("\nGoodbye, matey!")
if __name__ == "__main__": #M
chat_loop()
A minimal Python chat loop demonstrates a list-based history, prompt-template injection, and repeated model calls. section 3.4









