This prompt is for agent developers and infrastructure engineers who need to retrieve context from a shared memory store, not just by semantic similarity, but by balancing what is recent with what is relevant to the current task. The job-to-be-done is producing a ranked, scored, and justified list of memory entries that an agent can use as its working context. The ideal user is building a multi-agent system where agents share a blackboard, session state, or long-term fact store and must avoid the common failure mode of retrieving stale but semantically similar information over fresh, critical updates.
Prompt
Agent Memory Read with Recency and Relevance Scoring

When to Use This Prompt
Defines the job, ideal user, and constraints for the Agent Memory Read with Recency and Relevance Scoring prompt.
Use this prompt when your agent has a specific task or query and needs to pull a limited set of the most valuable entries from a larger memory store. It is appropriate when memory entries have associated timestamps and you need the model to apply a configurable recency weight. Do not use this prompt for simple top-K vector similarity search without scoring justification, for writing new memories, or for full database scans. It is also not a replacement for a deterministic retrieval pipeline; it is the scoring and ranking layer that sits on top of candidate retrieval, adding judgment that pure vector distance cannot provide.
The prompt requires you to provide a set of candidate memory entries, each with a unique ID, content, and a timestamp. You must also supply the agent's current task or query. The output is a structured list of entries with relevance scores, recency scores, a combined weighted score, and a concise justification for each entry's inclusion. Before deploying, you must validate the output schema strictly—missing justifications or malformed scores should trigger a retry or fallback. In high-stakes applications where memory retrieval drives autonomous action, always log the scored results for auditability and consider a human review step if the combined score falls below a defined confidence threshold. If your memory store is large, pre-filter candidates with a vector or keyword search before passing the top-N to this prompt to manage context window costs.
Use Case Fit
Where the Agent Memory Read prompt works, where it fails, and the operational preconditions required before wiring it into a production agent loop.
Good Fit: Context-Rich Agent Loops
Use when: an agent needs to pull the most relevant recent observations from a shared memory store before planning its next action. Guardrail: Pair this prompt with a strict retrieval budget (top-K limit) to prevent context window overflow from highly scored but low-value entries.
Bad Fit: Archival or Full-Text Search
Avoid when: the task requires exhaustive keyword matching or retrieving every document containing a term. Guardrail: This prompt is for semantic relevance and recency-weighted ranking. Offload exact-match and high-recall retrieval to a dedicated search index or vector database filter before invoking the scoring prompt.
Required Inputs
Risk: The prompt produces hallucinated scores or generic rankings if critical inputs are missing. Guardrail: Ensure the prompt always receives a [CURRENT_GOAL], a [MEMORY_CANDIDATES] list with timestamps, and a [RECENCY_WINDOW] parameter. Validate these inputs in the application harness before calling the model.
Operational Risk: Staleness Drift
Risk: A naive recency weight can bury a critical, older fact that contradicts newer but incorrect information. Guardrail: Implement a minimum relevance floor in the scoring schema. Add an eval check that verifies at least one high-importance, low-recency memory is retrieved when it directly contradicts a recent entry.
Operational Risk: Context Poisoning
Risk: A malicious or buggy upstream agent writes misleading entries with fabricated timestamps to manipulate the agent's recall. Guardrail: Never trust raw timestamps from unverified agents. The application harness should attach a server-side ingestion timestamp and filter or flag entries from low-trust sources before they reach the memory read prompt.
When to Escalate Instead
Avoid when: the memory store returns zero candidates or all scores fall below the relevance threshold. Guardrail: Define a clear [FALLBACK_ACTION] in the prompt contract. Instead of forcing a best-guess answer, the agent should escalate to a human or a broader retrieval tool with a structured "no relevant memory found" signal.
Copy-Ready Prompt Template
A reusable prompt for retrieving and ranking memory entries from a shared agent store using recency and relevance scoring.
This prompt template is designed to be inserted into your agent's memory retrieval pipeline. It instructs the model to act as a retrieval ranker, consuming a set of candidate memory entries and a query representing the agent's current information need. The model must score each entry on two axes—recency and relevance—and produce a final composite score with a concise justification. The output is structured JSON, making it suitable for direct consumption by a downstream agent or orchestrator without additional parsing.
textYou are a memory retrieval ranker for an autonomous agent system. Your task is to evaluate a set of candidate memory entries against the agent's current query and rank them by a composite score that balances recency and relevance. ## INPUT Query: [AGENT_QUERY] Current Timestamp: [CURRENT_TIMESTAMP] Candidate Memory Entries: [CANDIDATE_MEMORY_ENTRIES] ## SCORING RULES 1. For each entry, assign a `relevance_score` (0.0 to 1.0) based on how well the entry's content answers or informs the query. 2. Assign a `recency_score` (0.0 to 1.0) using exponential decay: `recency_score = exp(-[DECAY_RATE] * hours_since_timestamp)`. Use the provided `Current Timestamp` and the entry's `timestamp` to calculate `hours_since_timestamp`. 3. Compute a `composite_score` as `([RELEVANCE_WEIGHT] * relevance_score) + ([RECENCY_WEIGHT] * recency_score)`. Ensure the weights sum to 1.0. 4. If an entry's `relevance_score` is below [RELEVANCE_THRESHOLD], set its `composite_score` to 0.0 and mark it as `excluded: true`. 5. If an entry's `timestamp` is older than [STALENESS_CUTOFF_HOURS] hours, set its `composite_score` to 0.0 and mark it as `stale: true`. ## CONSTRAINTS - Do not fabricate information. Only use the content provided in each entry. - If no entries meet the threshold, return an empty `ranked_results` array. - Preserve the original `entry_id` for traceability. ## OUTPUT_SCHEMA Return a valid JSON object with this exact structure: { "query": "string", "ranked_results": [ { "entry_id": "string", "relevance_score": number, "recency_score": number, "composite_score": number, "justification": "string (1-2 sentences explaining the relevance assessment)", "excluded": boolean, "stale": boolean } ], "retrieval_metadata": { "total_candidates": number, "returned_count": number, "excluded_count": number, "stale_count": number } }
To adapt this template for your system, replace the square-bracket placeholders with values from your application context. [AGENT_QUERY] should be the current task or question the agent is trying to resolve. [CANDIDATE_MEMORY_ENTRIES] must be a pre-fetched list of objects, each containing at least an entry_id, timestamp (ISO 8601), and content field. Tune [DECAY_RATE], [RELEVANCE_WEIGHT], [RECENCY_WEIGHT], [RELEVANCE_THRESHOLD], and [STALENESS_CUTOFF_HOURS] based on your application's tolerance for old information. For high-stakes domains like healthcare or finance, always route the final ranked list through a human review step before the agent acts on it, and log the full prompt and response for auditability.
Before deploying, validate the output against the defined JSON schema. Common failure modes include the model returning a composite score for an entry it simultaneously marked as excluded: true, or miscalculating the recency decay. Implement a post-processing validator that checks for these inconsistencies and either corrects them programmatically or triggers a retry. Start with a RELEVANCE_WEIGHT of 0.7 and a RECENCY_WEIGHT of 0.3 for task-oriented agents, then adjust based on eval results for retrieval precision.
Prompt Variables
Required inputs for the Agent Memory Read prompt. Each placeholder must be populated before the prompt is assembled and sent to the model. Validation notes describe how to programmatically verify the input before execution.
| Placeholder | Purpose | Example | Validation Notes |
|---|---|---|---|
[MEMORY_STORE] | Serialized or structured representation of the agent's shared memory entries to search over | {"entries": [{"id": "mem-001", "content": "User prefers dark mode", "timestamp": "2025-01-15T10:00:00Z", "source_agent": "ui-agent"}]} | Must parse as valid JSON. Must contain an 'entries' array. Each entry must have 'id', 'content', and 'timestamp' fields. Reject if empty array or missing required fields. |
[QUERY] | The current information need expressed as a natural language question or search intent | What are the user's display preferences? | Must be a non-empty string between 1 and 500 characters. Reject null, empty, or whitespace-only strings. No validation on query quality required. |
[CURRENT_TIMESTAMP] | ISO-8601 timestamp representing 'now' for recency decay calculation | 2025-03-15T14:30:00Z | Must parse as valid ISO-8601 datetime. Must be within 24 hours of system clock unless testing. Reject future timestamps more than 1 hour ahead or timestamps older than session start. |
[MAX_RESULTS] | Integer cap on the number of scored memory entries to return | 5 | Must be a positive integer between 1 and 50. Default to 10 if not provided. Reject values outside range or non-integer types. |
[RECENCY_DECAY_FACTOR] | Float controlling how aggressively older entries are penalized in scoring | 0.9 | Must be a float between 0.0 and 1.0. Default to 0.85 if not provided. Values near 1.0 preserve older entries; values near 0.0 heavily favor recent entries. Reject out-of-range values. |
[RELEVANCE_THRESHOLD] | Minimum composite score an entry must achieve to be included in results | 0.3 | Must be a float between 0.0 and 1.0. Default to 0.2 if not provided. Set higher for high-precision retrieval, lower for high-recall. Reject out-of-range values. |
[SOURCE_AGENT_FILTER] | Optional list of agent IDs to restrict memory search to specific sources | ["ui-agent", "preferences-agent"] | Must be null or a JSON array of non-empty strings. If provided, only entries with matching source_agent values are scored. Reject if array contains empty strings or non-string elements. |
Implementation Harness Notes
How to wire the memory read prompt into a production agent loop with validation, retries, and observability.
The memory read prompt is not a standalone query; it is a retrieval step inside an agent's context assembly pipeline. Before the agent acts, it calls a memory store, passes the current task and recent observations as [QUERY_CONTEXT], and receives a ranked list of memory entries. The implementation harness must handle the full lifecycle: constructing the prompt with the correct variables, calling the model, validating the structured output, handling failures, and merging the result into the agent's working context window. This prompt is typically invoked at the start of a new task, after a tool call that changes state, or when the agent detects a knowledge gap.
Integration pattern. Wrap the prompt in a function read_memory(query_context, memory_store, top_k=10, recency_decay_days=7). The function should: (1) Retrieve candidate entries from the memory store using a hybrid search (vector similarity + keyword filters on session ID, agent ID, or task type). (2) Assemble the prompt by injecting [MEMORY_CANDIDATES] as a JSON array of objects with fields id, content, timestamp, source_agent, and access_count. (3) Inject [QUERY_CONTEXT] with the current task description, recent observations, and any active constraints. (4) Call the model with response_format set to the defined output schema. (5) Parse and validate the JSON response. (6) Filter entries below [RELEVANCE_THRESHOLD] (default 0.5) and return the scored list to the agent's context assembler.
Validation and retry logic. The output must conform to a strict schema: an array of objects with memory_id, relevance_score (float 0-1), recency_score (float 0-1), combined_score (float 0-1), and relevance_justification (string). Implement a post-processing validator that checks: all memory_id values exist in the input candidates, scores are within bounds, and combined_score is a weighted sum of relevance and recency (default 0.7 relevance, 0.3 recency). If validation fails, retry once with the error message appended to the prompt. If the retry also fails, fall back to a rule-based ranking using timestamp decay and keyword overlap, and log the failure for prompt debugging. For high-stakes agent workflows, add a human review step when the top result's combined_score is below 0.4 or when multiple entries have scores within 0.05 of each other, indicating ambiguity.
Observability and evals. Log every memory read call with: the query context hash, number of candidates, top-k results with scores, validation status, and latency. This data feeds two eval checks. First, retrieval precision: periodically sample memory reads and have a human or LLM judge rate whether the top-3 entries were actually relevant to the query context. Target precision above 0.85. Second, staleness detection: flag entries older than [STALENESS_THRESHOLD_DAYS] that receive high relevance scores, and verify whether the information is still accurate. If staleness is a recurring problem, adjust the recency decay weight upward or add a TTL field to memory entries. Wire these evals into your prompt regression test suite so that changes to the scoring prompt or weights are caught before deployment.
What to avoid. Do not call this prompt inside a tight agent loop without caching; repeated identical queries waste tokens and latency. Cache results keyed by (query_context_hash, memory_store_version) for the duration of a task. Do not pass the entire memory store as candidates; pre-filter to a manageable set (50-200 entries) using vector search and metadata filters. Do not skip validation; malformed JSON or out-of-bounds scores will break downstream context assembly. Finally, do not treat the model's relevance scores as ground truth; they are a ranking heuristic. Always preserve the raw memory content alongside the scores so the agent can reason over the evidence, not just the ranking.
Expected Output Contract
Defines the structure, types, and validation rules for the ranked memory entries returned by the Agent Memory Read prompt. Use this contract to parse, validate, and integrate the output into downstream agent workflows.
| Field or Element | Type or Format | Required | Validation Rule |
|---|---|---|---|
memory_entries | Array of objects | Must be a non-null array. If no relevant memories are found, return an empty array. | |
memory_entries[].id | String | Must match the unique identifier from the source memory store. Non-empty string. | |
memory_entries[].content | String | The full text of the memory entry. Must not be truncated unless explicitly requested by [MAX_CONTENT_LENGTH]. | |
memory_entries[].recency_score | Number (float) | Must be a value between 0.0 and 1.0, calculated from the provided [CURRENT_TIMESTAMP] and the entry's timestamp. Higher is more recent. | |
memory_entries[].relevance_score | Number (float) | Must be a value between 0.0 and 1.0, representing semantic similarity to the [QUERY]. Higher is more relevant. | |
memory_entries[].composite_score | Number (float) | Must be a value between 0.0 and 1.0, calculated as a weighted combination of recency and relevance scores based on [WEIGHTING_STRATEGY]. | |
memory_entries[].relevance_rationale | String | A brief, non-empty explanation of why this memory is relevant to the [QUERY]. Must reference specific concepts from both the query and the memory content. | |
memory_entries[].source_timestamp | String (ISO 8601) | The original timestamp of the memory entry. Must be a valid ISO 8601 date-time string. Used for audit and staleness checks. |
Common Failure Modes
When agents read from shared memory with recency and relevance scoring, these failures surface first. Each card identifies a specific breakage pattern and the guardrail that prevents it in production.
Recency Bias Overwhelms Relevance
What to watch: The scoring function weights recency so heavily that a recent but irrelevant entry outranks an older, critical fact. The agent acts on fresh noise instead of stale signal. Guardrail: Cap the recency weight multiplier and require a minimum relevance threshold before recency can boost an entry. Log cases where recency alone changed the top result.
Stale Memory Poisoning
What to watch: An entry that was true at write time is now incorrect, but the agent retrieves and acts on it because no expiration or invalidation exists. Guardrail: Attach a TTL or valid_until field to every memory entry. The retrieval prompt must check staleness and either discard expired entries or flag them with a staleness warning in the output.
Silent Retrieval Misses
What to watch: The retrieval returns an empty or low-confidence result set
Score Collision Without Tiebreaking
What to watch: Multiple memory entries receive identical composite scores. The model picks arbitrarily or hallucinates a ranking rationale, producing non-deterministic behavior across runs. Guardrail: Define a deterministic tiebreaking rule in the prompt—prefer higher source confidence, then older entry, then lexicographic key order. Include the tiebreak reason in the output justification.
Context Window Overload from Over-Retrieval
What to watch: The retrieval returns too many entries because the relevance floor is too low or the top-K is too large. The agent's context window fills with marginally relevant memory, crowding out instructions and tool schemas. Guardrail: Enforce a hard max_entries limit and a strict relevance floor. Add a budget check: if retrieved entries exceed N tokens, truncate to the top-scoring subset and log the drop.
Source Trust Contamination
What to watch: A memory entry from a low-confidence source or a known-unreliable agent is retrieved and treated as authoritative because the scoring function ignores source trust. Guardrail: Include a source_confidence multiplier in the composite score. Entries from unverified or low-trust sources must be flagged in the output and never used as the sole basis for high-risk actions without human review.
Evaluation Rubric
Use this rubric to evaluate the quality and reliability of the Agent Memory Read prompt before deploying it to production. Each criterion targets a specific failure mode in retrieval, ranking, or staleness handling. Run these tests against a golden dataset of known memory entries and queries.
| Criterion | Pass Standard | Failure Signal | Test Method |
|---|---|---|---|
Recency Weighting Accuracy | Entries created within the last hour appear in the top 3 results when relevance is equivalent | Older entries consistently outrank newer entries with similar semantic relevance scores | Inject 10 memory entries with identical relevance but staggered timestamps; verify that the returned ranking is monotonically decreasing by age |
Relevance Justification Quality | Every returned entry includes a non-empty [JUSTIFICATION] field that references specific terms from the query and the memory content | Justifications are missing, generic (e.g., 'relevant'), or hallucinate query terms not present | Parse the output for each entry; assert [JUSTIFICATION] length > 20 characters and contains at least one overlapping token from the query |
Staleness Flagging | Entries older than the [STALENESS_THRESHOLD_HOURS] are marked with | Stale entries appear above fresh entries, or the | Set [STALENESS_THRESHOLD_HOURS] to 1; insert one entry aged 2 hours with maximum relevance; confirm it is not in the top 3 and has |
Score Range Compliance | All returned entries have a composite score between 0.0 and 1.0 inclusive | Scores fall outside the 0-1 range, are null, or are non-numeric strings | Validate the [COMPOSITE_SCORE] field for every entry in the output array using a schema check: |
Output Schema Adherence | The response is valid JSON matching the expected [OUTPUT_SCHEMA] with all required fields present | JSON parsing fails, required fields like [ENTRY_ID] or [COMPOSITE_SCORE] are missing, or extra unexpected top-level keys appear | Validate the entire response body against the [OUTPUT_SCHEMA] using a JSON Schema validator; retry once on failure before flagging |
Deduplication Integrity | No two entries in the results list share the same [ENTRY_ID] | Duplicate [ENTRY_ID] values appear in the output array | Count unique [ENTRY_ID] values in the output; assert the count equals the length of the results array |
Query-Context Isolation | The response contains only entries from the provided [MEMORY_STORE] and does not invent external facts | The output includes a memory entry whose content was not present in the input [MEMORY_STORE] | Diff the set of [ENTRY_ID] values in the output against the [ENTRY_ID] values in the input; assert the output set is a subset of the input set |
Empty Store Handling | When [MEMORY_STORE] is an empty array, the prompt returns an empty results array without error | The model returns an error string, a non-empty array, or refuses to answer | Provide an empty [MEMORY_STORE] and a valid query; assert the response parses correctly and the results array length is 0 |
Enabling Efficiency, Speed & Accuracy
Intelligent Analysis, Decision & Execution
We build AI systems for teams that need search across company data, workflow automation across tools, or AI features inside products and internal software.
Talk to Us
Search across company data
Give teams answers from docs, tickets, runbooks, and product data with sources and permissions.
Useful when people spend too long searching or get different answers from different systems.

Automate internal workflows
Use AI to route work, draft outputs, trigger actions, and keep approvals and logs in place.
Useful when repetitive work moves across multiple tools and teams.

Add AI to products and internal tools
Build assistants, guided actions, or decision support into the software your team or customers already use.
Useful when AI needs to be part of the product, not a separate tool.
Adapt This Prompt
How to adapt
Start with the base prompt and a simple in-memory store (dict or list). Drop the scoring formula and use natural-language ranking: "Return the 5 most relevant and recent entries for [QUERY]." Skip strict output schema validation during early testing.
Watch for
- Recency bias drowning out high-relevance but older entries
- Model returning entries in arbitrary order without scores
- No handling of empty memory stores (model may hallucinate entries)

About the author
Prasad Kumkar
CEO & MD, Inference Systems
Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.
His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.
Partnered with leading AI, data, and software stack.
How We Work
Custom AI workflows for your Business
One-fit-all AI don't work for modern businesses. At Inferensys, we aim to understand your business & custom requirements; which we use to define most efficient agentic workflows, the data, and the tools for your business.
01
Review the use case
We understand the task, the users, and where AI can actually help.
Read more02
Pick the right approach
We define what needs search, automation, or product integration.
Read more03
Build the first useful version
We implement the part that proves the value first.
Read more04
Improve from there
We add the checks and visibility needed to keep it useful.
Read moreThe first call is a practical review of your use case and the right next step.
Talk to Us