Lesson 1 of 4 Conversational Systems and Prompt Flows

Storing and Managing Conversation Context

This lesson covers how conversational applications store history, decide what to resend and keep it within the context window, since FMs are stateless and each request must contain everything the model needs.

Learning outcomes

After completing this lesson, you will be able to:

  • Store conversation history in DynamoDB.
  • Fit history into the context window with windows and summaries.
  • Use managed memory options.

Storing conversation history in DynamoDB

Models are stateless: each request must include the conversation so far, so the application must store it between turns. Amazon DynamoDB is a common choice. The partition key is the session or conversation ID and the sort key is a timestamp or turn number, so a single Query returns a conversation's turns in order with single-digit millisecond latency, adding no noticeable delay to each request.

Each item holds the role, the content, token counts and metadata, such as the prompt version used, which helps later analysis. A time to live (TTL) attribute deletes sessions automatically after a retention period, which keeps the table small and supports privacy requirements that limit how long conversations are kept.

# Table: conversations  PK = session_id (S)  SK = turn (N)  TTL attribute = expires_at
history = table.query(KeyConditionExpression=Key("session_id").eq(sid), ScanIndexForward=False, Limit=10)["Items"]
messages = [{"role": i["role"], "content": [{"text": i["text"]}]} for i in reversed(history)]
messages.append({"role": "user", "content": [{"text": user_input}]})
r = brt.converse(modelId=model_id, system=system_prompt, messages=messages)

Fitting history into the context window

Sending the entire history raises cost and latency with every turn and eventually exceeds the context window. Strategies include a sliding window of the last N turns, a token budget that drops the oldest turns first, a running summary of older turns generated by a small model and kept with the recent turns, and retrieval over history, which embeds past turns and retrieves only those relevant to the current question. Important facts, such as the customer's account number, can be stored as structured session attributes rather than relying on them staying in the window.

The following diagram shows what each request in a long support chat contains after the prompt is bounded.

Bounding the prompt in a long support chat. Instead of sending every turn, the application sends the system prompt, a running summary of earlier turns, key facts such as order numbers stored as session attributes in DynamoDB, the last 8 turns verbatim and the new question. Prompt size stays bounded while early details remain available.
The turn counts come from the scenario above.
Match the pairs

History strategies

Exam skills 1.6.2

Match each strategy for long conversations with how it works.

Log in to try this activity Interactive activities are free with an account.

Managed memory options

Managed services reduce custom code. RetrieveAndGenerate keeps context across turns with a sessionId. Amazon Bedrock Agents maintain session state and can keep memory as session summaries across sessions for a retention period. Amazon Bedrock AgentCore Memory stores short-term events (raw turns) and extracts long-term memory with strategies for summaries, user preferences and semantic facts, which agents retrieve in later sessions. Agent frameworks such as Strands Agents provide conversation managers that apply sliding windows or summarisation automatically. The following table compares the options.

OptionKeepsChoose when
DynamoDB with TTLRaw turns you controlCustom applications needing full control and retention rules
RetrieveAndGenerate sessionIdContext for knowledge base Q&AManaged RAG chat
Bedrock Agents memorySession state and summaries across sessionsAgents built on Bedrock Agents
AgentCore MemoryShort-term events and long-term extracted memoriesAgents on any framework needing personalisation
Scenario questions

Choosing a memory option

Exam skills 1.6.2 · 2.1.1

Choose the memory option that fits each application.

Log in to try this activity Interactive activities are free with an account.
Worked example

Long support chats

A support assistant's cost per conversation grows with each turn, and after about 40 turns requests fail with a validation error about input length. Customers often refer back to details from early in the chat.

Sending all turns is the cause. Keeping the last 8 turns verbatim plus a running summary of earlier turns, and storing key facts such as order numbers as session attributes in DynamoDB, bounds the prompt size while keeping early details available.

Check yourself

Why must the application store conversation history?

Show answer

Foundation models are stateless, so the application must resend the relevant history with each request.

Exam Tips

Remember for the exam

  • FMs are stateless; the application resends relevant history.
  • DynamoDB keyed by session ID with TTL is a common history store.
  • Bound context with sliding windows, summaries or retrieval over history.
  • Cross-session personalization: AgentCore Memory long-term strategies.