ACAgentic Craft

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
Progress is stored locally in this browser.

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

typescript
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:

typescript
@Agent({
  name: 'support-desk',
  enableRAG: true,
  ragTopK: 5,
  maxSteps: 8,
})
export class SupportDesk { /* ... */ }

Indexing sketch (@hazeljs/rag)

typescript
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)

typescript
// 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

KnobStarting point
ragTopK3–8; measure faithfulness
Chunk size/overlapPer RAG docs splitters
Untrusted RAG textGuardrails / treat as untrusted
Transactional truthPrefer @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

Sources

Related