Intermediate~30 minHazelJS
RAG and Truncation Policies
Use enableRAG/ragTopK, RAG pipelines, and AIContextManager trim—without treating bigger windows as a strategy.
- Authors
- editorial-team
- Published
- Last reviewed
Direct answer
Retrieval quality beats window size. In HazelJS: enableRAG + ragTopK on @Agent / execute, RAGPipeline + chunk splitters for indexing, and conversation windows via memory helpers (maxConversationLength in the memory guide). AIContextManager auto-trims oldest turns under maxTokens / reserveTokens when you use @hazeljs/ai context management.
There is no separate product API named “truncation policy”—compose these knobs and document them.
Worked example — RAG on execute
await runtime.execute('support-desk', 'How do returns work?', {
enableRAG: true,
ragTopK: 5, // start 3–8; measure faithfulness
maxSteps: 6,
budget: { maxTokens: 30_000 },
});
Declare defaults on the agent when most calls need the same corpus:
@Agent({
name: 'support-desk',
enableRAG: true,
ragTopK: 5,
maxSteps: 8,
})
export class SupportDesk { /* ... */ }
Indexing sketch (@hazeljs/rag)
import { RecursiveTextSplitter } from '@hazeljs/rag'; // see package docs for exact exports
const splitter = new RecursiveTextSplitter({
chunkSize: 800,
chunkOverlap: 120,
});
const chunks = await splitter.splitText(articleMarkdown);
// upsert chunks into your vector store wired to the agent RAG peer
AIContextManager (when using @hazeljs/ai helpers)
// Pattern from AI package docs — trim oldest turns under a token budget
const manager = new AIContextManager({
maxTokens: 8_000,
reserveTokens: 1_000, // leave room for the model reply
});
Practical defaults
| Knob | Starting point |
|---|---|
ragTopK | 3–8; measure faithfulness |
| Chunk size/overlap | Per RAG docs splitters |
| Untrusted RAG text | Guardrails / treat as untrusted |
| Transactional truth | Prefer @Tool lookup over RAG alone |
Next
/patterns/retrieval-before-action · /learn/memory-and-state/memory-layers-in-hazeljs
Artifact: ragTopK + maxConversationLength settings checked into repo docs