Skip to content

Strands session_manager: insert retrieved memory before user msg; rename <user_context> #191

@nakedible-p

Description

@nakedible-p

In AgentCoreMemorySessionManager.retrieve_customer_context, retrieved LTM context is appended as an assistant message AFTER the last user message:

Why change:

  • The last message in the prompt becomes the memory dump, not the user request (can hurt instruction-following).
  • Long-context positional bias: models over-weight info near the beginning/end; keep memory near the end but keep the USER message last. ("Lost in the Middle", Liu et al. 2023: https://arxiv.org/abs/2307.03172)
  • The tag <user_context> is misleading (this content is retrieved memory, not user-authored).

Proposed fix:

  1. Insert the retrieved memory message immediately BEFORE the last user message (so user stays last).
  2. Rename wrapper tag to <retrieved_memory> (or similar).

OpenAI prompt structuring best practice (instructions + context separation): https://help.openai.com/en/articles/6654000-best-practices-for-prompt-engineering-with-the-openai-api
Anthropic context engineering (hybrid context + retrieval): https://www.anthropic.com/engineering/effective-context-engineering-for-ai-agents

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions