future-engineer-7239
02/17/2026, 3:42 PMtall-airline-16756
02/17/2026, 3:42 PMtall-airline-16756
02/17/2026, 3:43 PMfuture-engineer-7239
02/17/2026, 3:47 PMtall-airline-16756
02/17/2026, 3:48 PMchat_ctx — a list of messages or a ChatContext object
â—¦ configuration: LLM instance, preemptive_generation flag, truncation/summarization policy
2) Add messages incrementally (only send new content)
• On new user input:
â—¦ session.chat_ctx.append({role: "user", content: new_user_message})
â—¦ (Do NOT rebuild full prompt string with all prior messages.)
3) Call the LLM with the session chat context and stream tokens
• Use the LLM chat API that accepts a chat context object (messages list) rather than a single prompt string.
• Stream tokens back directly to your client as they arrive.
4) Persist or mutate session context with the response
• When generation finishes (or during streaming), append the assistant reply to session.chat_ctx.
• Keep the session in memory while the conversation is active.
5) Manage history size
• Periodically trim or summarize old turns:
â—¦ If chat_ctx grows, either remove oldest messages, or replace chunks with a short summary message (role: system or assistant_summary).
â—¦ Summaries preserve context while keeping model input short.
Code outline (pseudo/structured) — adapt to your stack:
# 1. Create or fetch a session
session = get_or_create_session(session_id)
# session.chat_ctx is a list like: [{"role":"system","content":"..."},
# {"role":"user","content":"..."}]
# 2. On new user message
session.chat_ctx.append({"role": "user", "content": new_user_message})
# 3. Call LLM with the session chat context (streamed)
llm_instance = openai.LLM.with_azure(..., model=AZURE_OPENAI_MODEL, timeout=60)
stream = llm_instance.chat(chat_ctx=session.chat_ctx) # chat_ctx not a single prompt
response_content = ""
async for chunk in stream:
if chunk.delta and chunk.delta.content:
response_content += chunk.delta.content
# optionally forward tokens to the client
# 4. Append assistant reply to session context
session.chat_ctx.append({"role": "assistant", "content": response_content})
# 5. Trim or summarize if needed
if len(session.chat_ctx) > MAX_MESSAGES:
summary = summarize_old_messages(session.chat_ctx[:N_OLD])
# replace old messages with a short summary entry
session.chat_ctx = [summary_message] + session.chat_ctx[N_OLD:]
Key implementation details to reduce TTFT
• Use a chat API that accepts a messages/chat_ctx structure rather than a single combined prompt string. The model can process structured contexts more efficiently.
• Keep most of the session data in memory — avoid re-serializing and re-sending everything over the network each turn.
• Only include the minimal system/instruction messages needed for the model to behave correctly each turn.
• Trim or summarize history proactively; older details usually can be represented as a short summary message.
• Enable preemptive_generation if you have partial transcripts (reduces perceived latency by starting generation earlier).
• Co-locate your service and the Azure OpenAI endpoint in the same region to reduce network RTT.
When to persist session state vs. in-memory
• In-memory (fastest): good while the conversation is active.
• Persist to Redis or DB if you need fault tolerance, horizontal scaling, or recovery. When fetching persisted state, load into memory for the LLM call to avoid repeated DB reads per token.
Metrics and validation
• Measure TTFT before and after changes. Also track:
â—¦ average request payload size
â—¦ LLM TTFB and throughput
â—¦ token counts per request
• Iteratively tune the summary/truncation strategy until latency and quality are balanced.
Questions so I can give a tailored code sample 1) Do you already have a per-user or per-call session store (in memory, Redis, etc.)? 2) Is your input streaming partial ASR, or one full text message per turn? 3) Are you able to enable preemptive_generation in your environment?
Tell me answers to those and I’ll provide a concrete code example that matches your current async streaming pattern and how to integrate summarization/truncation.