context() method fetches the most relevant memories for a user before any message is exchanged, so you can inject them directly into your system prompt and give your agent instant, accurate personalization from the first response.
Basic Context Loading
Callclient.context() before building your system prompt. The returned ContextResult contains a memories list you can iterate over:
current_message, memories are ranked by a combination of importance and recency — the most significant and recently accessed memories surface first.
Smart Context with current_message
Pass the user’s first message as current_message to switch the ranking from importance/recency to semantic relevance. MemLayer uses it as a search query and returns memories most related to what the user is actually asking about.
- Without current_message
- With current_message
current_message, you might load 10 general facts. With it, you load the 10 most relevant facts for the specific conversation that’s about to happen.
Tuning top_k
The top_k parameter controls how many memories are returned (range: 1–50, default: 10).
| Value | When to use |
|---|---|
5–10 | Most applications — keeps system prompts short and focused |
15–25 | Agents that rely heavily on history (e.g., long-term coaching bots) |
30–50 | When you have large context windows and want maximum recall |
10. If your system prompts are growing too long or LLM latency increases, reduce top_k or raise min_score in recall() to filter lower-relevance results.
Full Conversation Pattern
Here is a complete pattern for a memory-enabled conversation turn: load context, build the system prompt, call the LLM, then save what was learned.Load context
Call
context() with current_message to retrieve the most relevant memories before the LLM sees anything.Build the system prompt
Format memories as a bullet list and prepend them to your existing system prompt. Keep it concise.
Context vs. Search
context() and recall() serve different purposes — use the right one at the right time.-
context()is for session start. It loads a broad snapshot of the most important or relevant memories to prime your system prompt before any conversation begins. -
recall()is for mid-conversation search. When the user asks something specific (“what did I tell you about my shipping address?”), userecall()with a targeted query to fetch the precise memories that answer it.
recall() at session start with a generic query, or using context() mid-conversation — reduces relevance and wastes tokens.