Knowledge Injection
Knowledge injection is the process of inserting retrieved semantic memory and episodic memory snippets into the working memory context before the LLM call. The position and formatting of injected knowledge significantly affects how the LLM uses it.
Injection Position
Injected knowledge appears between the system prompt and the message history. This position is deliberate — most LLMs pay more attention to content closer to the end of the context. Placing knowledge before the message history ensures it is read before the LLM processes the user's current message:
// Context assembly order (visual representation):
┌─────────────────────────────────────┐
│ [System Prompt] │ ← High LLM attention
├─────────────────────────────────────┤
│ [Procedure] (if matched) │
├─────────────────────────────────────┤
│ [Retrieved Knowledge] │ ← Injected here
│ Source: HR Policy 2025.pdf │
│ Content: "Annual leave entitlement..."│
│ --- │
│ Source: Leave Calculator Guide.pdf│
│ Content: "To calculate leave..." │
├─────────────────────────────────────┤
│ [Past Conversation Snippets] │
│ 2025-03-14: User asked about... │
├─────────────────────────────────────┤
│ [Message History - Turn 1] │
│ User: Hi, how many leave days... │
│ Assistant: You have 12 remaining..│
├─────────────────────────────────────┤
│ [Current User Message] │ ← Highest LLM attention
│ User: Can I take 5 days in June? │
└─────────────────────────────────────┘
Formatting Retrieved Knowledge
// How retrieved chunks are formatted for injection
private string FormatForContext(IReadOnlyList<MemoryRecord> records)
{
if (!records.Any()) return string.Empty;
var sb = new StringBuilder("[Retrieved Knowledge]\n");
sb.AppendLine("The following information is relevant to the user's question:");
sb.AppendLine();
foreach (var record in records)
{
sb.AppendLine($"Source: {record.Metadata.Source}");
if (record.Metadata.Category != null)
sb.AppendLine($"Category: {record.Metadata.Category}");
sb.AppendLine(record.Content);
sb.AppendLine("---");
}
sb.AppendLine("Use the above information to answer the user. If the answer is not in the provided information, say so.");
return sb.ToString();
}
Citation in Responses
To help users understand where information came from, the system prompt can instruct the LLM to cite sources when using retrieved knowledge:
// System prompt addition for citations
"When answering, if you use information from the [Retrieved Knowledge] section,
cite the source document at the end of your answer.
Example: (Source: HR Policy 2025.pdf)"
Semantic and episodic memory are retrieved fresh on every turn. This means if new documents are indexed while a conversation is active, they will appear in retrieval results on the next turn — no session restart needed.